【问题标题】:R - Plot Only TextR - 仅绘制文本
【发布时间】:2013-11-24 00:00:31
【问题描述】:

很好奇如何创建仅包含文本信息的情节。这基本上是绘图窗口的“打印”。

到目前为止,我发现的最佳选择如下:

  library(RGraphics)
  library(gridExtra)

    text = paste("\n   The following is text that'll appear in a plot window.\n",
           "       As you can see, it's in the plot window",
           "       One might imagine useful informaiton here")
    grid.arrange(splitTextGrob(text))



然而,一个人无法控制(据我所知)字体类型、大小、对齐方式等。

【问题讨论】:

  • 你不需要grid.arrangegridExtra,只需grid.draw()

标签: string r text plot


【解决方案1】:

你可以在ggplot2 中使用annotate 喜欢

library(ggplot2)
text = paste("\n   The following is text that'll appear in a plot window.\n",
         "       As you can see, it's in the plot window\n",
         "       One might imagine useful information here")
ggplot() + 
  annotate("text", x = 4, y = 25, size=8, label = text) + 
  theme_void()

您当然可以删除绘图边距、轴等以仅显示文本

【讨论】:

    【解决方案2】:

    这里也有一个方便的例子:

    par(mar = c(0,0,0,0))
    plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
    
    text(x = 0.34, y = 0.9, paste("This is a plot without a plot."), 
         cex = 1.5, col = "black", family="serif", font=2, adj=0.5)
    
    text(x = 0.34, y = 0.6, paste("    Perhpas you'll:"), 
         cex = 1.2, col = "gray30", family="sans", font=1, adj=1)
    text(x = 0.35, y = 0.6, paste("Find it helpful"), 
         cex = 1.2, col = "black", family="mono", font=3, adj=0)
    

    【讨论】:

      【解决方案3】:

      阅读?par。通过familyfont 参数选择字体类型的能力有限。

      【讨论】:

        【解决方案4】:

        您可以使用基本图形来做到这一点。首先,您需要从绘图窗口中删除所有边距:

        par(mar = c(0,0,0,0))
        

        然后你将绘制一个空图:

        plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
        

        这里是这里发生的事情的指南(使用 ?plot.default?par 了解更多详细信息):

        • ann - 显示注释(设置为 FALSE)
        • bty - 边框类型(无)
        • type - 绘图类型(不产生点或线)
        • xaxt - x 轴类型(无)
        • yaxt - y 轴类型(无)

        现在绘制文本。我去掉了多余的空间,因为它们似乎没有必要。

        text(x = 0.5, y = 0.5, paste("The following is text that'll appear in a plot window.\n",
                                     "As you can see, it's in the plot window\n",
                                     "One might imagine useful informaiton here"), 
             cex = 1.6, col = "black")
        

        现在恢复默认设置

        par(mar = c(5, 4, 4, 2) + 0.1)
        

        希望对你有帮助!

        【讨论】:

        • 也很有帮助,这里还有一个链接,指向在此处实现不同字体/字体的提示:statmethods.net/advgraphs/parameters.html - 感谢您的提示!
        • 另外,在 text(...) 函数中添加左右对齐信息: "adj = 1" => left justify, "adj = 0" => right justify, "adj = NULL" (默认) => center
        • 重新居中调整,我上面弄错了。中心调整为“adj = 0.5”
        猜你喜欢
        • 2017-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-21
        • 2020-11-30
        • 2015-01-09
        • 1970-01-01
        相关资源
        最近更新 更多