3

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=" "))

legend label with italicized Mac but weird spacing over two lines

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=" ")))

Mac previous density instead of previous mac density

eipi10
  • 91,525
  • 24
  • 209
  • 285
SheUrchin
  • 33
  • 5
  • 2
    plotmath and newlines are incompatible. There are [(ugly) workarounds, e.g. using atop etc.](http://stackoverflow.com/q/13223846/471093) – baptiste Dec 12 '16 at 20:41
  • It's easier to help if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data so we can also run the code to try it out. Please make sure to post only code directly related to your problem. – MrFlick Dec 12 '16 at 20:41

2 Answers2

2

This is a little hacky because this isn't really supported, but if you use nested atop()s and scriptstyle() for some formatting:

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(scriptstyle("Previous"), atop(paste(italic("       Macrocystis")),
" Density m" ^2 ), sep=" ")))

enter image description here

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
Irosenthal
  • 46
  • 5
0

using the tableGrob hack to split expressions into multiple lines,

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294