问题
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?
回答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.
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.
来源:https://stackoverflow.com/questions/61242595/r-save-figures-in-the-zoomed-window-with-command