Search code examples
rggplot2legendlegend-properties

Adjusting legend's spacing to main plot


This topic covers almost all attributes of legend in R. I'm wondering how we can increase the spacing between the legend and the main plot.


Solution

  • Following the example in the link you provided:

    library(ggplot2)
    xy <- data.frame(x=1:10, y=10:1, type = rep(LETTERS[1:2], each=5))
    plot <- ggplot(data = xy)+
    geom_point(aes(x = x, y = y, color=type))
    

    enter image description here

    plot <- plot + theme(legend.box.margin = margin(0, 0, 0, 20))
    

    enter image description here