好奇如何创建仅包含文本信息的绘图。这本质上是绘图窗口的“打印”。
到目前为止我发现的最佳选择如下:
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))
然而,据我所知,人们无法控制字体类型、大小、对齐方式等。
您可以使用基础图形来完成此操作。首先,您需要去掉绘图窗口中的所有边距:
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
了解更多详细信息):
现在绘制文本。我去掉了多余的空格,因为它们似乎没有必要。
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)
希望有帮助!
您可以在
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()
当然,您可以删除绘图边距、轴等,只保留文本
这里还有一个方便使用的示例:
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)
阅读
?par
。 通过 family
和 font
参数选择字体类型的能力有限。