Adjusting spacing between two legends in ggplot2

2,936 views
Skip to first unread message

Deirdre Griffin

unread,
Aug 10, 2016, 1:55:38 PM8/10/16
to Davis R Users' Group
Hey all,

I am trying the spacing between to adjust two legends in the graph attached (fill = Biochar dose (%) and linetype = pH adjustment) so that they are closer together and the title are justified at the top. I've played around with a few things but am not having any luck. Anyone have any suggestions? 

Below is my code, if that helps.

Thanks so much!

Deirdre



ggplot(WS_all, aes(x = simpletrt, y = nodulenum_total))+ geom_boxplot(aes(fill = biochardose_pcnt, linetype = pH)) +
facet_grid(.~soil, labeller = soils_labeller) +
labs(y = "Nodule number per plant",
x = "Treatment",
fill = "Biochar dose (%)",
linetype = "pH Adjusted") +
scale_x_discrete(labels = c ("No amendment \ncontrol",
"pH-adjusted \ncontrol",
"WS biochar-\nlow dose",
"WS biochar-\nhigh dose")) +
theme_bw() +
theme(legend.position = c(1,1),
legend.box = "horizontal",
legend.justification = c(1,1),
legend.margin = unit(-0.4, "cm"),
legend.key.size = unit(4.5, "mm"),
axis.title.x = element_text(size = 11),
axis.title.y = element_text(size =11, vjust = 1),
axis.text.x = element_text(size = 9, angle = 45, hjust = 1),
axis.text.y = element_text(size = 9),
strip.text.x = element_text(size = 11),
legend.title = element_text(size = 9),
legend.text = element_text(size = 9),
panel.grid.minor = element_blank(), panel.grid.major = element_blank())
WS nodule number cowplot.pdf

Matt Espe

unread,
Aug 10, 2016, 10:09:16 PM8/10/16
to Davis R Users' Group
I believe you do it via guide_legend(label.vjust = ___).


Not 100% sure though - if it is a one time deal, you can also re-position elements in a pdf with Acrobat Pro or Inkscape. 

Matt  
Reply all
Reply to author
Forward
0 new messages