Search code examples
rggplot2

ggplot2: How to place plot legend below caption


I want to place the plot legend below my plot AND my caption. While I'm able to change most stylistic aspects of the caption - for instance, using plot.caption - I haven't found a way to change the positioning of the caption.

Here's what I have so far:

ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) + 
      + geom_point(size=6) + labs(caption = "Note: Dots are closer than they appear") + 
      theme_minimal() + theme(legend.position = c("bottom"))

Is there a way to do this or some work around?


Solution

  • You can reposition the caption over the legend

    Maybe you could try using lemon::reposition_legend.

    https://github.com/stefanedwards/lemon

    A comparison of the two against a default is given below:

    library(ggplot2)
    
    library(patchwork)
    
    p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
      geom_point(size =6) +
      labs(caption = "Note: Dots are closer than they appear") +
      theme_minimal() +
      theme(legend.position = "bottom")
    
    p + labs(title = "Before") +
    
      p + theme(plot.caption.position = "plot",
            plot.caption = element_text(vjust = 40)) +
      labs(title = "Vjust") +
      
      lemon::reposition_legend(
        p + labs(title = "Lemon"),
        position = "bottom")
    

    enter image description here