创建特定尺寸的绘图窗口

发布于 2024-08-19 00:56:51 字数 41 浏览 3 评论 0原文

如何创建具有特定宽度和高度(以像素等为单位)的新屏幕 R 绘图窗口?

How can I create a new on-screen R plot window with a particular width and height (in pixels, etc.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

离旧人 2024-08-26 00:56:52

使用 dev.new()。 (请参阅此相关问题。)

plot(1:10)
dev.new(width=5, height=4)
plot(1:20)

要更具体地说明哪些单位是使用:

dev.new(width=5, height=4, unit="in")
plot(1:20)
dev.new(width = 550, height = 330, unit = "px")
plot(1:15)

编辑 Rstudio 的附加参数(2020 年 5 月),(感谢 用户 Soren Havelund Welling< /a>)

对于 Rstudio,添加 dev.new(width=5,height=4,noRStudioGD = TRUE)

Use dev.new(). (See this related question.)

plot(1:10)
dev.new(width=5, height=4)
plot(1:20)

To be more specific which units are used:

dev.new(width=5, height=4, unit="in")
plot(1:20)
dev.new(width = 550, height = 330, unit = "px")
plot(1:15)

edit additional argument for Rstudio (May 2020), (thanks user Soren Havelund Welling)

For Rstudio, add dev.new(width=5,height=4,noRStudioGD = TRUE)

厌味 2024-08-26 00:56:52

这取决于您使用的设备。如果您使用的是 pdf 设备,您可以这样做:

pdf( "mygraph.pdf", width = 11, height = 8 )
plot( x, y )

然后您可以使用 mfrow 参数来划分 pdf 中的空间,如下所示:

par( mfrow = c(2,2) )

这使得具有四个面板的 pdf 可用于绘图。不幸的是,某些设备采用的单位与其他设备不同。例如,我认为 X11 使用像素,而我确定 pdf 使用英寸。如果您只想创建多个设备并向它们绘制不同的内容,您可以使用 dev.new()、dev.list() 和 dev.next()

其他可能有用的设备包括:

这里列出了所有设备此处

This will depend on the device you're using. If you're using a pdf device, you can do this:

pdf( "mygraph.pdf", width = 11, height = 8 )
plot( x, y )

You can then divide up the space in the pdf using the mfrow parameter like this:

par( mfrow = c(2,2) )

That makes a pdf with four panels available for plotting. Unfortunately, some of the devices take different units than others. For example, I think that X11 uses pixels, while I'm certain that pdf uses inches. If you'd just like to create several devices and plot different things to them, you can use dev.new(), dev.list(), and dev.next().

Other devices that might be useful include:

There's a list of all of the devices here.

晚风撩人 2024-08-26 00:56:52

保存绘图的一个方便的函数是 ggsave(),它可以根据文件扩展名自动猜测设备类型,并平滑设备之间的差异。您可以使用特定的大小和单位进行保存,如下所示:

ggsave("mtcars.png", width = 20, height = 20, units = "cm")

在 R markdown 中,图形大小可以通过以下方式指定块

```{r, fig.width=6, fig.height=4}  
plot(1:5)
```

A convenient function for saving plots is ggsave(), which can automatically guess the device type based on the file extension, and smooths over differences between devices. You save with a certain size and units like this:

ggsave("mtcars.png", width = 20, height = 20, units = "cm")

In R markdown, figure size can be specified by chunk:

```{r, fig.width=6, fig.height=4}  
plot(1:5)
```
十年九夏 2024-08-26 00:56:52

由于 RStudio 不支持 @Shane 的公认解决方案(请参阅 此处)截至目前(2015 年 9 月),我想向 @James Thompson 回答有关工作流程的建议添加一条建议:

如果您使用 SumatraPDF 作为查看器,您无需在对其进行更改之前关闭 PDF 文件。苏门答腊不会将打开的文件设置为只读,因此不会阻止其被覆盖。因此,一旦您使用 Sumatra 打开 PDF 文件,RStudio(或任何其他 R IDE)中的更改将立即显示在 Sumatra 中。

As the accepted solution of @Shane is not supported in RStudio (see here) as of now (Sep 2015), I would like to add an advice to @James Thompson answer regarding workflow:

If you use SumatraPDF as viewer you do not need to close the PDF file before making changes to it. Sumatra does not put a opened file in read-only and thus does not prevent it from being overwritten. Therefore, once you opened your PDF file with Sumatra, changes out of RStudio (or any other R IDE) are immediately displayed in Sumatra.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文