I am a brand new R user and this community has been extremely helpful in my learning process, so thanks!
I have a ggplot legend title that includes an italicized word and a superscript that I am trying to stack in three neat lines. It appears that the italicized word and the superscript take up extra space and therefore won't stack nicely. Ideally my title would be:
Previous Macrocystis Density m^2
With each word on its own line, but I am having trouble getting there...
Here is my code (sorry it is ugly, remember I am new!) and the resulting plot:
ggplot(mtcars,aes(x=wt,y=mpg, color = hp))+
geom_jitter(size = 16) +
scale_color_continuous(high="black", low="light grey") +
theme_bw() +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
legend.title=element_text(size=20),
legend.text=element_text(size =15),
legend.key = element_rect(size = 5),
legend.key.height=unit(3,"line"),
legend.key.size = unit(2.5, 'lines')) +
xlab("Weight") +
ylab("MPG") +
labs(color = expression(paste("Previous\n", italic("Macrocystis\n"),
"Density", " ", m^2), sep=" "))
Thanks for your help, and for being such a great resource!
Update: Thanks to @baptiste for getting me a little closer, but it is driving me a little crazy that I can't get the words in the right order. Here is my best draft so far:
ggplot(mtcars,aes(x=wt,y=mpg, color = hp))+
geom_jitter(size = 16) +
scale_color_continuous(high="black", low="light grey") +
theme_bw() +
theme(axis.text=element_text(size=20),
axis.title=element_text(size=20),
legend.title=element_text(size=20),
legend.text=element_text(size =15),
legend.key = element_rect(size = 5),
legend.key.height=unit(3,"line"),
legend.key.size = unit(2.5, 'lines')) +
xlab("Weight") +
ylab("MPG") +
labs(color = expression(atop(italic("Macrocystis\n"),
paste("Previous\nDensity", " ", m^2), sep=" ")))