0

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

ThePresident
  • 301
  • 6
  • 16

1 Answers1

1

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

Jamel
  • 11
  • 1
  • For the future reference, this works as well and saves the plots in the directory where the script has been executed: `png(filename="MyPlot.png") p dev.off()` – ThePresident Jun 28 '18 at 04:01