Explanatory graphs: annotate geom_bar

How do you create an explanatory graph?

  1. geom_segment using no aesthetics to avoid double legends;
  2.  annotate to add text where you want inside the graph space;

gap_expl

Complete code:
ggplot(dat,aes(Scenario,value,fill=Type,width=.5,color=Type,order = as.numeric(Type)))+
geom_bar(stat="identity") +
#set your own colors
scale_fill_manual(values=c("magenta","green", "lightblue","lightblue"),breaks=c("Observed","Competing Preferences","Involuntary factors","Recent change in Preferences"))+
#since I am using borders you have to add also their colors
scale_colour_manual(values=c("magenta","green", "lightblue","blue"),breaks=c("Observed","Competing Preferences","Involuntary factors","Recent change in Preferences"))+
# x axis breaks
scale_x_discrete(limits = c(1,2,3))+
#no name for the y axis
ylab("")+
# specify where to put the segments and annotations
geom_segment(x = 0.7, y = 0.4, xend = 1.3, yend = 0.4, colour = "black")+
annotate("text",x=0.6,y=0.4,label="Observed = Simulated")+
annotate("text",x=1,y=0.7,label="Involuntary Factors")+
geom_segment(x = 1.7, y = 0.4, xend = 2.3, yend = 0.4, colour = "black")+
geom_segment(x = 1.7, y = 0.8, xend = 2.3, yend = 0.8, colour = "black")+
annotate("text",x=1.6,y=0.35,label="Observed")+
annotate("text",x=1.6,y=0.75,label="Simulated")+
annotate("text",x=2,y=0.9,label="Involuntary \nFactors")+
annotate("text",x=2,y=0.6,label="Competing Preferences")+
geom_segment(x = 2.7, y = 0.4, xend = 3.3, yend = 0.4, colour = "black")+
geom_segment(x = 2.7, y = 0.8, xend = 3.3, yend = 0.8, colour = "black")+
annotate("text",x=2.6,y=0.35,label="Simulated")+
annotate("text",x=2.6,y=0.75,label="Observed")+
annotate("text",x=3,y=0.6,label="Recent change in Preferences")+
annotate("text",x=3,y=0.9,label="Involuntary \nFactors")+
geom_hline(yintercept=1,col="red",lty=2)+
#move legend to bottom
theme(plot.margin=unit(c(1,1,1,1),"cm"),legend.position="bottom",legend.direction="horizontal",legend.title=element_blank())+
#have legend on 2 rows
guides(fill=guide_legend(nrow=2,byrow=T))+
#flip axis
coord_flip()

Here you can find the dput data I have used:
dat <- structure(list(Scenario = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L,
1L, 2L, 3L), Type = structure(c(3L, 3L, 3L, 4L, 4L, 4L, 1L, 1L,
1L, 2L, 2L, 2L), .Label = c("Competing Preferences", "Involuntary factors",
"Observed", "Recent change in Preferences"), class = "factor"),
value = c(0.4, 0.4, 0.4, 0, 0, 0.4, 0, 0.4, 0, 0.6, 0.2,
0.2)), .Names = c("Scenario", "Type", "value"), class = "data.frame", row.names = c(NA,
-12L))

Author: acarioli

is a researcher at the Geography and Environment department of the University of Southampton, WorldPop project team. She is also affiliated researcher at CED, UAB and Dondena Centre. Her interests include spatial econometrics and modeling, bayesian methods, machine learning processes, forecasting, micro-data simulation, and data visualization. Demo-traveler, Mac enthusiast, R zealot and Rladies member.