R - 仅绘制文本

问题描述 投票:0回答:4

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

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

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


enter image description here


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

string r text plot
4个回答
53
投票

您可以使用基础图形来完成此操作。首先,您需要去掉绘图窗口中的所有边距:

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")

enter image description here

现在恢复默认设置

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

希望有帮助!


30
投票

您可以在

annotate
中使用
ggplot2
,例如

library(ggplot2)
blurb = 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 = blurb) + 
  theme_void()

enter image description here

当然,您可以删除绘图边距、轴等,只保留文本


8
投票

这里还有一个方便使用的示例:

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)

enter image description here


0
投票

阅读

?par
。 通过
family
font
参数选择字体类型的能力有限。

© www.soinside.com 2019 - 2024. All rights reserved.