Meso
Meso

Reputation: 1435

ggplot2: plotting error bars for groups without overlap

I wish to show the effect of two pollutants on the same outcome and was happy with the plot when there are no groups. Now when I want to plot the same data for all-year and stratified by season, I either get overlaps of error bars or three separate panels which are not optimal for my need.
Sample data could be accessed from here: https://drive.google.com/file/d/0B_4NdfcEvU7LV2RrMjVyUmpoSDg/edit?usp=sharing

As an example with the following code I create a plot for all-year:

ally<-subset(df, seas=="allyear")
ggplot(ally,aes(x = set, y = pinc,ymin = lcinc, ymax =ucinc,color=pair,shape=pair)) +
geom_point(position=position_dodge(width=0.5) ,size = 2.5) + 
geom_linerange(position=position_dodge(width=0.5), size =0.5) +  theme_bw() +
geom_hline(aes(yintercept = 0))  + 
labs(colour="Pollutant", shape="Pollutant", y="Percent Increase", x="") + 
scale_x_discrete(labels=c(NO2=expression(NO[2]), 
                               NOx=expression(NO[x]), 
                               Coarse= expression(Coarse),
                               PM25=expression(PM[2.5]),
                               PM10=expression(PM[10])))  + 
theme(plot.title = element_text(size = 12,face="bold" )) + 
theme(axis.title=element_text(size="12") ,axis.text=element_text(size=12)) 

But when I add facet_grid(. ~ seas) I will have three separate panels. How can I display this data for all year and divided by seasons in one panel?

Upvotes: 3

Views: 1460

Answers (1)

fabians
fabians

Reputation: 3473

Either color or shape needs to be used to represent season, not pollutant.

Then this should come close to what you want:

library(ggplot2)
ggplot(df, aes(x = set, y = pinc,ymin = lcinc, ymax =ucinc, 
               color=seas, shape=pair)) +
    geom_point(position=position_dodge(width=0.5), size = 2.5) + 
    geom_linerange(position=position_dodge(width=0.5), size =0.5) +  theme_bw() +
    geom_hline(aes(yintercept = 0))  + 
    labs(colour="Season", shape="Pollutant", y="Percent Increase", x="") + 
    scale_x_discrete(labels=c(NO2=expression(NO[2]), 
                              NOx=expression(NO[x]), 
                              Coarse= expression(Coarse),
                              PM25=expression(PM[2.5]),
                              PM10=expression(PM[10])))  + 
    theme(plot.title = element_text(size = 12,face="bold" )) + 
    theme(axis.title=element_text(size="12") ,axis.text=element_text(size=12)) 

enter image description here

I do think that facetting gives you better graphs here --

if you want to focus attention on the comparison between seasons for each pollutant, use this (facet_grid(~pair, labeller=label_both)):

enter image description here

if you want to focus attention on the comparison between pollutants for each season, use this (facet_grid(~seas, labeller=label_both)): enter image description here

Upvotes: 3

Related Questions