【问题标题】:ggplot error: Making line graphs of three variables, then making a facet plot of thoseggplot错误:制作三个变量的折线图,然后制作这些变量的平面图
【发布时间】:2016-08-12 17:14:42
【问题描述】:

我正在尝试制作三个线图的侧面图,并且在每个线图中都有三条线。我想让与“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:如果您使用的是虚线或虚线,则颜色、大小和线型必须在整个线上保持不变

我做错了什么?

【问题讨论】:

  • 图片未显示
  • 从您的aes 中删除group(以及在您使用它时的stat)。

标签: r ggplot2


【解决方案1】:

正如理查德指出的那样,删除 group=countrySpellId 将消除错误,因为您试图在两个不同的变量上应用一个组两次(linetype 参数本质上与 group 做同样的事情,它只是意味着不同的线也将具有不同的线型)。国家/地区分组稍后将在facet_wrap 中进行。

这样做会让你得到 ggplot 自动为你选择的三种不同的线型,但由于你特别关注这些线的外观,你会想要使用scale_linetype_manual,它允许你指定 ggplot 的线型分配给每个因子水平。您在 scale_linetype_discrete 的正确路径上!

删除错误的group 参数并替换我们拥有的scale_lientype_discrete

ggplot(data = oirfsFacetPlot2, aes(x = step, y = vals, stat = "identity"))  +
    geom_line(aes(linetype = indicator)) +
    xlab("Month") + ylab("Percent change") +
    theme_bw() + scale_x_continuous(breaks = seq(0,3,1)) +
    scale_linetype_manual(name = "indicator", values = c(2,1,2))  +
    facet_wrap( ~ country, scales = "free_y", nrow = 3 )  

【讨论】:

  • 非常清楚,非常感谢。我还注意到,我可以按照 R Graphics Cookbook link987654321@
猜你喜欢
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 1970-01-01
  • 2020-07-09
  • 2018-10-07
相关资源
最近更新 更多