【问题标题】:Force RStudio to plot in Viewer强制 RStudio 在查看器中绘图
【发布时间】:2019-01-04 16:32:25
【问题描述】:

我的绘图选项卡不起作用 - 所以我试图在查看器选项卡中显示 ggplot 的输出。

这已为打印 HTML 表格完成:Force rstudio to use browser instead of viewer。这可以用于地块吗?

library(ggplot2)
gg.plot <- ggplot(mtcars, aes(mpg, wt)) + geom_point()

有没有办法打印到查看器?

【问题讨论】:

  • 如果绘图窗格不起作用,您可以使用library(ggplot2) data(iris) x11() ggplot(data = iris, aes(Petal.Length)) + geom_histogram() 中的 x11() 函数将绘图定向到外部窗口
  • 运行那段代码时出现错误Error: unexpected symbol in "x11() ggplot。我可以打印到 PDF,但这不是一个理想的解决方案。
  • x11()ggplot(...) 是独立的命令,需要在不同的行上运行。

标签: r rstudio


【解决方案1】:

我将此作为评论发布,但无法正确设置格式,因此我将其作为答案。此代码将创建一个新窗口来放置您的绘图。

library(ggplot2)
data(iris)
x11() # creates the new window
ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
dev.off() # closes the window when you're done with it

【讨论】:

  • 我无法让x11() 函数工作。你知道输出到 Viewer 的方法吗?
  • 对不起,我没有。我猜你需要保存图像然后在查看器中打开它,但我没有太多的基础。
【解决方案2】:

不幸的是,我认为查看器窗格的设计目的并非如此。来自 Ian Pylvainen 的 This 文章建议此窗格专门用于 Web 内容。除非您尝试在 HTML 实例中可视化您的绘图(由 Markdown、Shiny、htmlwidgets 等生成),否则我不会推荐 Rstudio 的 Viewer 作为可行的解决方案。

要继续 @Joseph Clark John 的建议,使用 R 提供的各种其他设备可能对您有用。他们对使用x11() 的建议是针对Linux 发行版的,因此如果使用Windows 或MacO,您可以分别使用windows()quartz() 命令生成另一个窗口(请参阅所有相关devices 的文档)。

这篇文章涉及到这一点: Make R Studio plots only show up in new window

【讨论】:

    【解决方案3】:

    或者,您可以使用plotly::ggplotly() 在查看器窗格中显示 ggplot:

    library(ggplot2)
    library(plotly)
    data(iris)
    plotObj <- ggplot(data = iris, aes(Petal.Length)) + geom_histogram()
    plotly::ggplotly(plotObj)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-28
      • 2021-02-08
      • 2016-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      相关资源
      最近更新 更多