ThePresident
ThePresident

Reputation: 311

How to instruct R to save ggplot in the same directory where the script is executed?

I am running a Rscript where I plot some X-Y data, and I want that plot to be saved in the same directory from where the script has been executed. I've found many solutions to this for bash, but couldn't figure it out for R.

# Raw data plot
p <- ggplot(data, aes(X,Y)) + theme_bw()
  dev.print(file="Rplot.png", device=png, width=2800, res = 300)

I tried putting "./Rplot.png" but without success. I also though I could set the working directory as the current directory with setwd but actually couldn't figure out how to define the current directory.

This R script will be called within a bash script and it would be nice to have the graph in the same directory without having to define it every time.

Many thanks. TP

Upvotes: 0

Views: 3710

Answers (1)

Jamel
Jamel

Reputation: 11

Use getwd() to see where you're working.

Try

ggsave(p, filename = "Rplot.png", width = 2800)

to save the file where you have your working directory set to. You can see all of the options with ?ggsave

Upvotes: 1

Related Questions