我很难将图例文本对齐到左侧.
library(ggplot2)
library(reshape2)
o3<- rnorm(1827, 50, 10)
NO2<- rnorm(1827, 35, 7)
NOx<- rnorm(1827, 45, 10)
pm25<- rnorm(1827, 15, 4)
date<-seq(as.Date('2000-01-01'),as.Date('2004-12-31'),by = 1)
df<-data.frame(date,o3,NO2,NOx,pm25)
meltdf <- melt(df,id="date")
Run Code Online (Sandbox Code Playgroud)
使用此代码,对齐自动向左
ggplot(meltdf, aes(x = date, y = value, colour =variable)) + geom_smooth() + stat_smooth(method = "gam")
Run Code Online (Sandbox Code Playgroud)
然而,以下是alignemt到中心.
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])))
Run Code Online (Sandbox Code Playgroud)
我怎么能用最后一个脚本实现左对齐?
kon*_*vas 10
您需要指定legend.text.align
在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)
Run Code Online (Sandbox Code Playgroud)
或者,尝试使用bquote
而不是expression
,并且默认左对齐发生.我不知道为什么只是使用expression
更改右对齐...
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])))
Run Code Online (Sandbox Code Playgroud)