0
votes

I'm trying to make a facet plot of three line plots, and in each line plot there are three lines. I'd like to make the line corresponding to "oirf" to be a solid line, and both lines corresponding to "upperCI" and "lowerCI" to be dashed, either the same kind of dashed line or something very similar looking, like so:

hand-drawn line plots

But I'm getting an error I can't resolve.

Here's my data:

"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

Here's how I'm trying to do the plots:

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 )

But then I get this error, which is somehow related to the aes(linetype = indicator) I think.

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

What am I doing wrong?

1
Image isn't showing up - Richard Telford
delete the group from your aes (and the stat while you are at it). - Richard Telford

1 Answers

1
votes

As Richard pointed out, deleting the group=countrySpellId will eliminate the error, because you are trying to apply a group twice on two different variables (the linetype argument essentially does the same thing as group, it just means that that the different lines will also have different linetypes). The grouping on country will happen later in the facet_wrap.

Just doing that will get you three different linetypes that ggplot automatically chooses for you, but since you're particular about how those lines should look, you'll want to use scale_linetype_manual, which allows you to specify what linetype ggplot assigns to each factor level. You were on the right tract with scale_linetype_discrete!

Deleting the bad group argument and replacing the scale_lientype_discrete we have:

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 )