我正在尝试制作三个线图的方面图,并且在每个线图中有三条线。我想使对应于“oirf”的行成为实线,并且对应于“upperCI”和“lowerCI”的两行都是虚线,要么是相同类型的虚线,要么看起来非常相似,就像这样:
但是我收到了一个我无法解决的错误。
这是我的数据:
"countrySpellId","iso","country","step","indicator","vals"
38,"BLR","Belarus",0,"oirf",-0.19979745478058
38,"BLR","Belarus",1,"oirf",-0.182586795026907
38,"BLR","Belarus",2,"oirf",-0.242312010909111
106,"GEO","Georgia",0,"oirf",-0.154580915298088
106,"GEO","Georgia",1,"oirf",-0.0572862343086547
106,"GEO","Georgia",2,"oirf",-0.345457860190889
167,"KGZ","Kyrgyzstan",0,"oirf",0.960777168532119
167,"KGZ","Kyrgyzstan",1,"oirf",0.458003383067036
167,"KGZ","Kyrgyzstan",2,"oirf",0.190725669245905
38,"BLR","Belarus",0,"lowerCI",-0.357909781851253
38,"BLR","Belarus",1,"lowerCI",-0.411483619567094
38,"BLR","Belarus",2,"lowerCI",-0.514508124910321
106,"GEO","Georgia",0,"lowerCI",-0.323219475085121
106,"GEO","Georgia",1,"lowerCI",-0.236286319570866
106,"GEO","Georgia",2,"lowerCI",-0.540228716700013
167,"KGZ","Kyrgyzstan",0,"lowerCI",0.448913075973564
167,"KGZ","Kyrgyzstan",1,"lowerCI",0.0581860926615476
167,"KGZ","Kyrgyzstan",2,"lowerCI",-0.235580302805703
38,"BLR","Belarus",0,"upperCI",-0.0416851277099078
38,"BLR","Belarus",1,"upperCI",0.0463100295132811
38,"BLR","Belarus",2,"upperCI",0.0298841030920997
106,"GEO","Georgia",0,"upperCI",0.0140576444889454
106,"GEO","Georgia",1,"upperCI",0.121713850953557
106,"GEO","Georgia",2,"upperCI",-0.150687003681766
167,"KGZ","Kyrgyzstan",0,"upperCI",1.47264126109067
167,"KGZ","Kyrgyzstan",1,"upperCI",0.857820673472524
167,"KGZ","Kyrgyzstan",2,"upperCI",0.617031641297513
以下是我试图制作这些情节的方法:
ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, group = countrySpellId,
stat = "identity")) +
geom_line(aes(linetype = indicator)) +
xlab("Month") + ylab("Percent change") +
theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
scale_linetype_discrete(name ="indicator",
breaks=c("lowerCI", "oirf","upperCI")) +
facet_wrap( ~ country, scales = "free_y", nrow = 3 )
但后来我得到了这个错误,这与我认为的aes(linetype = indicator)
有某种关系。
错误:geom_path:如果使用虚线或虚线,颜色,大小和线型必须在线上保持不变
我做错了什么?
答案 0 :(得分:1)
正如Richard指出的那样,删除group=countrySpellId
将消除错误,因为您试图在两个不同的变量上应用一个组两次(linetype
参数基本上与{{1}相同它只是意味着不同的行也会有不同的线型)。国家/地区的分组将在group
稍后进行。
这样做会让你获得ggplot自动为你选择的三种不同的线型,但由于你特别关注这些线应该是什么样的,你要使用facet_wrap
,这允许您可以指定ggplot为每个因子级别分配的线型。您使用scale_linetype_discrete进入了正确的道路!
删除错误的scale_linetype_manual
参数并替换我们拥有的group
:
scale_lientype_discrete