eFF eFF - 19 days ago 5
R Question

geom_errorbar and geom_facet

The following code does not display properly the error bars:

rf.imp<- read.csv("importances_byaggregations.csv",head=TRUE,sep=",") #Changes when handling the data

rf.imp$flux <- as.character(rf.imp$flux)
rf.imp$flux<-factor(rf.imp$flux,levels=unique(rf.imp$flux))
rf.imp$aggregation <- as.character(rf.imp$aggregation)
rf.imp$aggregation<-factor(rf.imp$aggregation,levels=unique(rf.imp$aggregation))

cbbPalette <- c("#F0E442", "#CC79A7","#E69F00","#56B4E9", "#009E73") # Mimicking Python colors

rf.imp$rel.influence<-rf.imp$rel.influence*100
rf.imp$SD<-rf.imp$SD*100

limits <- aes(ymax = rf.imp$rel.influence + rf.imp$SD, ymin=rf.imp$rel.influence - rf.imp$SD)

ggplot(rf.imp, aes(variable,rel.influence,fill=variable)) +
geom_bar(stat="identity",position="dodge") + scale_fill_manual(values=cbbPalette)+
theme_bw(base_size = 32, base_family = "Helvetica")+
xlab("")+
ylab("Variable importance (%)")+
facet_grid(aggregation~flux)+
geom_errorbar(limits, width=0.5)+
scale_y_continuous(limits=c(-10,90))+
theme(legend.position="none",
strip.text.x = element_blank(),
strip.text.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
strip.background = element_blank(),
panel.border = element_rect(colour = "black"),
panel.border = element_rect(colour = "black", fill=NA, size=1))


I would like to obtaine the following figure, but with the geom_facets swaped.

enter image description here

However, I get something like this:

enter image description here

Am I doing something wrong?

Thanks!

Answer

Your minimal example is a little too long for me to dig into, but I strongly suspect that your problem comes from using absolute (rf.imp$...) references in your error bar limits. If you use

geom_errorbar(aes(ymax=rel.influence+SD,
                  ymin=rel.influence-SD), width=0.5)

I think that will fix the problem.

Comments