[R] annotating faceted ggplot

efisio solazzo efisio.solazzo at jrc.ec.europa.eu
Fri Oct 23 14:25:21 CEST 2015


Dear, I have been reading several posts but still cannot work out how to 
annotate a faceted boxplot created from existing statistics using ggplot.

the dataframe is smthing like:

>dep_stat
   specie.sp. models min     max   q25    q50      q75
1        NO2   mod1   0   225.0   0.1   0.40    1.300
2        NO2   mod2   0   219.5   0.6   2.40    8.100
3         O3   mod1   0  2834.4  93.8 334.80  793.525
4         O3   mod2   0  1238.6   5.0 214.70  376.400
5       PM25   mod1   0 49948.0 123.5 438.10 1146.100
6       PM25   mod2   0  2466.1   9.2  24.55  184.300


from which I create the plot in the figure, where the bars go from min 
to the 75th percentile.

ggplot(dep_stat, aes(x=models, ymin=min, ymax=q75, lower=q25, 
middle=q50, upper=q75)) +
geom_boxplot(stat='identity', width=0.5) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1.5)) +
theme(axis.title.x = element_blank())+theme(axis.title.y = 
element_blank()) +
facet_wrap(~specie.sp., nrow=1, scales="free")

I'm trying to add (as text) at the top of each bar the maximum value, 
but the annotate command doesn't do the job right:

annotate('text', x=rep(seq(1:length(models)),3), y=q75+0.5, 
label=as.character(max), color="red", angle = 90, cex=5).

In this way three values text get added to each column...I have also 
tried with geom_text and grobTree, without success.

thanks for your help.





More information about the R-help mailing list