Cook R诗翔的R语言学习之路R语言与统计分析

【r<-方案|绘图】如何只绘制文本?

2019-05-21  本文已影响14人  王诗翔

如果想要只在图上写文本,这还是需要先创建一个空白的图形,然后将文本加上去。

使用base plot

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

ggplot2

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 informaiton here")
ggplot() + 
  annotate("text", x = 4, y = 25, size=8, label = text) + 
  theme_bw() +
  theme(panel.grid.major=element_blank(),
    panel.grid.minor=element_blank())

原文出处:https://stackoverflow.com/questions/19918985/r-plot-only-text

上一篇下一篇

猜你喜欢

热点阅读