【问题标题】:Show ggplot graphs in new window by default with X11默认情况下使用 X11 在新窗口中显示 ggplot 图形
【发布时间】:2021-05-21 22:12:45
【问题描述】:

我在 Ubuntu 18.04 上使用 RStudio 1.4。我使用外部设备 X11 来查看图形,因为我经常需要一次显示多个绘图。我想让 ggplot 默认在新设备中显示每个绘图,就好像 ggplot 调用总是在调用 dev.new() 之前一样。我不必做一堆手动关闭窗口。有没有办法拥有这种默认行为?下面是一个 MWE。

library(tidyverse)
options(device = "X11")

# regular default behavior, where a new ggplot call will overwrite the plot in the active device (if any)
mtcars %>% 
  ggplot(aes(x = cyl, y = mpg)) + 
  geom_point()

# desired default behavior, where a new device is created with the ggplot call
dev.new(); mtcars %>% 
  ggplot(aes(x = cyl, y = mpg)) + 
  geom_point()

【问题讨论】:

    标签: r ggplot2 graphics window x11


    【解决方案1】:

    我找到了一个简单的方法,覆盖了ggplot的打印方法:

    print.ggplot <- function(...) {
      dev.new()  
      ggplot2:::print.ggplot(...)
    }
    

    您可以将旧方法保存在变量中以在当前会话中恢复它,或者重新启动 R 会话以恢复默认值。类似的方法应该适用于许多其他图形包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-18
      • 1970-01-01
      • 2015-07-08
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 2016-11-30
      • 1970-01-01
      相关资源
      最近更新 更多