简体   繁体   English

情节和图例中的线型不匹配

[英]Mismatched linetypes in plot and legend

I plot this data using the following code 我使用以下代码绘制此数据

df = read.csv(file = "data.csv");
p = ggplot(data = df) + geom_line(aes(x=x,y=yp,linetype="dashed")) + 
geom_line(aes(x=x,y=yi,linetype="solid")) + facet_wrap(~s) + labs(y="y");

I am getting the following plot . 我正在得到以下情节

The code says to draw dashed line using yp variable and to draw solid line using yi variable. 代码说使用yp变量绘制虚线,并使用yi变量绘制实线。 But what we see in the plot is the other way round (if you take a look at the data). 但是我们在图中看到的却是相反的情况(如果您看一下数据)。 Linetypes in the legend are also mismatched. 图例中的线型也不匹配。 Is there a way to correct this? 有没有办法纠正这个问题?

As mentioned, you just need to modify placement of linetype inside geom_line function. 如前所述,您只需要修改geom_line函数中linetype位置geom_line

Further to add legend and colors to distinguish yp and yi, use : 进一步添加图例和颜色以区分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"))

Result 结果 快照1

Here is another solution for you: 这是您的另一种解决方案:

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)

at first i have melted the data with help of reshape2 package, and further used it in ggplot() . 首先,我借助reshape2软件包融化了数据,并进一步在ggplot()使用了它。 I have used as well scale_linetype_discrete() which has an argument labels = to change the legend text to solid and dashed, instead of yp and yi. 我还使用了scale_linetype_discrete() ,它具有一个参数labels =以将图例文本更改为实线和虚线,而不是yp和yi。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM