[英]Mismatched linetypes in plot and legend
如前所述,您只需要修改geom_line
函数中linetype
位置geom_line
。
进一步添加图例和颜色以区分yp和yi,请使用:
p = ggplot(data = df) + geom_line(aes(x=x,y=yp,colour="darkblue"), linetype="dotted", show.legend = TRUE) +
+ geom_line(aes(x=x,y=yi,colour="red"), linetype="solid", show.legend = TRUE) + facet_wrap(~s) + labs(y="y") +scale_color_discrete(name = "Y series", labels = c("yp", "yi"))
这是您的另一种解决方案:
library(reshape2)
library(ggplot2)
data <- structure(list(x = c(0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L), yp = c(0.469718933105469,
0.00809860229492188, 0.469718933105469, 0.041229248046875, 0.469718933105469,
0.12957763671875, 0.469718933105469, 0.284187316894531), yi = c(0.00809860229492188,
0.0212535858154297, 0.041229248046875, 0.1033935546875, 0.12957763671875,
0.28466796875, 0.284187316894531, 0.469718933105469), s = c("q1",
"q1", "q2", "q2", "q3", "q3", "q4", "q4")), .Names = c("x", "yp",
"yi", "s"), row.names = c(NA, -8L), class = c("tbl_df", "tbl",
"data.frame"))
data <- melt(data,id=c("x","s"))
ggplot(data,aes(x=x,y=value,linetype=variable)) + geom_line() + scale_linetype_discrete(labels=c("solid","dashed")) + facet_wrap(~s)
首先,我借助reshape2
软件包融化了数据,并进一步在ggplot()
使用了它。 我还使用了scale_linetype_discrete()
,它具有一个参数labels =
以将图例文本更改为实线和虚线,而不是yp和yi。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.