1

I have several plots to draw and save. I want to save the graph popped up in the zoomed window. What I did is as follows (which is normal save):

SaveName <- c("A.pdf", "B.pdf")
Path <- "~"
GroupName <- c("A", "B")

for (i in seq_along(Group)) {      
      pdf(file = paste(Path, SaveName[i], sep = ""), width = 8, height = 6)
      plotA <- ggplot(df %>% filter(Group == GroupName[i]), aes(x, y)) +
            geom_point() +               
      print(plotA)      
      dev.off() 
}

In this way, the files saved are the same as that by clicking "Export" -> "Save as PDF" in Rstudio. If I use the ggsave function and changes the dpi argument, it does not help much.

I want to save the graphs with R commands as if I click "Zoom" button, right-click on the picture, and then "Save image as". Can this be done in R?

Lin Jing
  • 143
  • 1
  • 6
  • 1
    `dpi` argument does not work for the vector image, such as **pdf** and **svg** , and you can resize the figure by setting arguments `height` and `width`, and you can zoom in or zoom out the figure arbitrarily. – yang Apr 16 '20 at 09:16
  • Thanks for your comment. Yes, you are right. However, what I want is to save the plot in R script as if I am saving by clicking "zoom" --- "save image as". – Lin Jing Apr 16 '20 at 12:38
  • 1
    The resolution of the image is equal to the resolution of your screen after you zoom the image, such as 1920 * 1080, and the dpi is 72. Thus, the width and height of the image is `screen_width/72 in` and `screen_height/72 in`, and you can save the image using `ggsave(, , width = screen_width/72, height = screen_height/72, dpi = 72)`. However, IMO, it doesn’t make sense to save images like this, since the dpi is fixed as `72` while the image is saving by clicking "zoom" and then "save image as". – yang Apr 16 '20 at 13:44
  • Thanks for the comments. I do this because I need to drop several plots and I would like to do them in a for loop, and save them in R script. This sounds to be a viable solution, but there's an error that ```can't find the object 'screen_width'```. – Lin Jing Apr 17 '20 at 06:36
  • 1
    Sorry for the confusing. `` and `` represent the resolution of your screen, which means if your screen resolution is 1920*1080, the `screen_width` is 1920 and `screen_height` is 1080. I don't know whether you can get the screen resolution in R, maybe you must set it manullay, see [here](https://stackoverflow.com/questions/7305244/how-can-i-get-the-screen-resolution-in-r). – yang Apr 17 '20 at 08:17
  • 1
    Furthemore, I don't recommend save the image by clicking "zoom" and then "save image as". See [here](https://ggplot2-book.org/polishing.html#saving) for how to save your plot. – yang Apr 17 '20 at 08:26
  • Thanks for your patient explanation and suggestions. After some tries with different parameters, the following one works the best for me ```ggsave("test.pdf", width = 14, height = 8, dpi = 72)```. BTW, if I want the best display effect for printed copies, should I change the dpi to 300? – Lin Jing Apr 17 '20 at 14:34
  • Could you please briefly add your comments as a solution to my question below? Then I can pick your solution and close this question. Thanks a lot! – Lin Jing Apr 17 '20 at 14:36
  • And I just realized that ```dpi = ``` is a redundant argument for ```pdf``` a vector image. Thanks a lot. – Lin Jing Apr 17 '20 at 14:58
  • of course, you can set a higher dpi to increase the resulation of your image for a better dispaly – yang Apr 19 '20 at 04:24

1 Answers1

2

dpi argument does not work for the vector image, such as pdf and svg , and you can resize the figure by setting arguments height and width, and you can zoom in or zoom out the figure arbitrarily.

While the image is saved by clicking "zoom" and then "save image as", the resolution of the image is equal to the resolution of your screen, such as 1920 * 1080, and the dpi is 72. Thus, the width and height of the image is <screen_width>/72 in and <screen_height>/72 in, where <screen_width> and <screen_height> represent the width and height of your screen resolution. I don't know whether you can get the screen resolution in R, maybe you must set it manullay, see here.

Then you can save the image:

ggsave(<filename.png>, <plot>, width = <screen_width>/72, height = <screen_height>/72, dpi = 72)

Furthemore, I don't recommend save the image by clicking "zoom" and then "save image as". See here for how to save your plot.

yang
  • 719
  • 3
  • 11