[英]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.