R:使用命令将图形保存在缩放的窗口中? [英] R: save figures in the zoomed window with command?

查看:60
本文介绍了R:使用命令将图形保存在缩放的窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要绘制和保存多个图.我想保存在缩放窗口中弹出的图形.我所做的如下(这是正常保存):

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() 
}

这样,保存的文件与在Rstudio中单击导出"->另存为PDF"相同.如果我使用 ggsave 函数并更改 dpi 参数,那么它并没有太大帮助.

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.

我想使用R命令保存图形,就像单击缩放"按钮,右键单击图片,然后单击将图像另存为"一样.可以在R中完成吗?

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?

推荐答案

dpi 参数不适用于矢量图像,例如pdf和svg,您可以通过设置参数height来调整图形的大小和宽度,您可以任意放大或缩小图形.

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.

虽然通过单击缩放"然后将图像另存为"保存图像,但是图像的分辨率等于屏幕的分辨率,例如1920 * 1080,而dpi为72.图像的宽度和高度分别为< screen_width>/72 < screen_height>/72 ,其中< screen_width> < screen_height> 表示屏幕分辨率的宽度和高度.我不知道您是否可以在R中获得屏幕分辨率,也许您必须对其进行设置,请参见

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.

然后您可以保存图像:

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.

这篇关于R:使用命令将图形保存在缩放的窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆