EBrewe
EBrewe

Reputation: 133

here + ggplot2 particularly ggsave

I have been trying to clean up my project workflow, and have been using the here package, but have am perplexed at some of the utility.

I set up an Rstudio project in the folder ~\ProjFolder. Within this folder I added a Plots folder ~\ProjFolder\Plots.

But when I try to use ggsave to save a plot into the Plots folder it instead places it in the ProjFolder.

library(here)
library(ggplot2)
xdat = rnorm(10)
ydat = rnorm(10)
df = data.frame(xdat,ydat)
ggplot(data = df, aes(x = xdat, y = ydat)) + geom_point()
here("Plots", ggsave("ScatterPlot.jpg"))

Any help? Or am I just using the here package ineffectively?

Upvotes: 5

Views: 1722

Answers (2)

atiretoo
atiretoo

Reputation: 1902

The problem is that you are composing here() and ggsave() in the wrong order. You want the first argument to ggsave() to be the full path, so

ggsave(here("Plots", "ScatterPlot.jpg"))

Does what you want.

Upvotes: 3

Calum You
Calum You

Reputation: 15072

You should do ggsave(here("Plots", "ScatterPlot.jpg")). here::here is just a way to provide the right file path, you put it in as a replacement for the path argument in functions that take one.

Upvotes: 5

Related Questions