【问题标题】:Add additional text below the legend (R + ggplot) [duplicate]在图例下方添加附加文本(R + ggplot)[重复]
【发布时间】:2014-11-28 00:52:35
【问题描述】:

假设我们有这个简单的情节:

ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
    geom_point(aes(color = vore)) + 
    theme(legend.position="bottom")

是否可以在图例项下方添加文本?我知道如何在图例上添加标题(上方)。但是假设我想要一些其他的一般信息写在下面。

很高兴找到解决方案。

谢谢! 马丁

【问题讨论】:

  • @hrbrmstr 我认为这是重复的,但不是您发布的内容。它是相关的,但那是关于一张桌子的。我认为这是骗子:stackoverflow.com/a/10346760/1000343

标签: r ggplot2 legend


【解决方案1】:

这是一种可能的方法:

library(gridExtra)
library(grid)

p <- ggplot(data = msleep, aes(x = log(bodywt), y = sleep_total)) + 
    geom_point(aes(color = vore)) + 
    theme(legend.position="bottom", plot.margin = unit(c(1,1,3,1),"lines")) + 
    annotation_custom(grob = textGrob("Extra text.  Read all about it"),  
        xmin = 2, xmax = 2, ymin = -4.5, ymax = -4.55)


gt <- ggplot_gtable(ggplot_build(p))
gt$layout$clip[gt$layout$name=="panel"] <- "off"
grid.draw(gt)

【讨论】:

  • 我想这已经被覆盖,所以我搜索了。这是一个非常相关的帖子,其中包含更多信息:stackoverflow.com/a/10346760/1000343
  • 在运行此代码时,我在底部看不到任何文字??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-21
  • 2021-01-05
相关资源
最近更新 更多