我有这样的数据框。
df <- data.frame(date = c('2015-11-23','2015-11-24','2015-11-25','2015-11-23','2015-11-24','2015-11-25'),
variable = c('LCNB', 'LCNB','LCNB','LCDEF','LCDEF','LCDEF'),
value = c(1,2,3,3,2,1))
我想在同一个图中绘制两条线,使用不同的颜色和线条类型。我目前的代码是:
library(scales)
ggplot(df, aes(x=as.Date(date), y=value, color=variable)) + geom_line(size=1.07) +
scale_color_manual(labels = c("Nb",'Def'), values = c("#E69F00", "#0072B2")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+labs(x="Dates",y="%") +
theme_bw()+
theme( panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"),plot.margin=unit(c(0,1,0.3,1), "cm")) +
labs(colour = "LC") + theme(legend.position = c(0.95,0.85))
到目前为止,我的代码只使两行颜色不同,我怎样才能使它们成为不同的行类型。
感谢您的帮助,
答案 0 :(得分:2)
您只需要使用与颜色相同的线条类型,但对于linetype
:
ggplot(df, aes(x=date, y=value, color=variable, linetype = variable)) +
geom_line(size=1.07,) +
scale_color_manual(
labels = c("Nb",'Def'),
values = c("#E69F00", "#0072B2")
) +
scale_linetype(labels = c("Nb", "Def")) +
scale_x_date(labels = date_format("%Y-%m-%d"), breaks = date_breaks("2.8 month")) +
theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1)) +
labs(x="Dates",y="%", colour = "LC", linetype = "LC") +
theme_bw()+
theme(
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
plot.margin=unit(c(0,1,0.3,1), "cm"),
legend.position = c(0.95,0.85))