Align legend text in ggplot

前端 未结 1 1430
长情又很酷
长情又很酷 2020-12-11 16:48

I have difficulty in aligning the legend text to the left.

library(ggplot2)
library(reshape2)
o3<- rnorm(1827, 50, 10)
NO2<- rnorm(1827, 35, 7)
NOx<         


        
相关标签:
1条回答
  • 2020-12-11 17:16

    You need to specify legend.text.align in theme():

    ggplot(meltdf, aes(x = date, y = value, colour =variable)) + 
    geom_smooth() + 
    stat_smooth(method = "gam") +
    scale_color_discrete(name="Pollutant", 
        labels = c(expression(O[3]),
                   expression(NO[2]),
                   expression(NO[x]),
                   expression(PM[2.5]))) +
    theme(legend.text.align = 0)
    

    Alternatively, try using bquote instead of expression, and default left alignment takes place. I don't know why just using expression changes the alignment to the right...

    ggplot(meltdf, aes(x = date, y = value, colour =variable)) + 
    geom_smooth() + 
    stat_smooth(method = "gam") +
    scale_color_discrete(name="Pollutant", 
        labels = c(bquote(O[3]),
                   bquote(NO[2]),
                   bquote(NO[x]),
                   bquote(PM[2.5]))) 
    
    0 讨论(0)
提交回复
热议问题