Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align two line legend title to the left with expression()

Tags:

r

legend

ggplot2

Using this script

df <- data.frame(x = 1:5, y = 1:5, color = letters[1:5])
ggplot(df, aes(x, y, fill = color)) +
  geom_point(size = 4, pch = 21) + 
  guides(fill = guide_legend(
     title = expression(atop(Median~Nitrate-Nitrogen~(NO[3]^{textstyle("-")}-N), ".\n Concentration"~(mg~L^{textstyle("-")})))))

I got this figure

enter image description here

Any suggestions how to align the second line of the legend title to the left?

like image 617
shiny Avatar asked Feb 10 '17 08:02

shiny


1 Answers

quick and ugly answer but it does the job until someone post better...

ggplot(df, aes(x, y, fill = color)) +
  geom_point(size = 4, pch = 21) + 
  guides(fill = guide_legend(
    title = expression(atop(Median~Nitrate-Nitrogen~(NO[3]^{textstyle("-")}-N), "Concentration"~(mg~L^{textstyle("-")})~phantom (1000000)~phantom (1000000)))))
like image 155
Kitty Avatar answered Nov 08 '22 19:11

Kitty