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.
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))
plot <- plot + theme(legend.box.margin = margin(0, 0, 0, 20))