2

Assume we have this simple plot:

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

enter image description here

Is it possible to add a text below the legend items? I know how to add a title (above) the legend. But lets say I want some other general information written below.

Would be very pleased to find a solution to that.

Thanks! Martin

1
  • 1
    @hrbrmstr I think this is a duplicate but not of what you posted. It's related but that's about a table. I think this is the dupe: stackoverflow.com/a/10346760/1000343 Commented Nov 28, 2014 at 1:42

1 Answer 1

6

This is one possible approach:

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)

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I figured this has been covered before so I searched. Here's a pretty related post with a ton more info: stackoverflow.com/a/10346760/1000343
on running this code I do not see any text at the bottom ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.