r - adding geom_hline+ Sample Size to a boxplot -
using ggplot
, trying
- add horizontal line boxplot
- add sample size x axis.
i have following dataset:
site, aluminum_dissolved, federal_guideline m1, 0.1, 0.4 m1, 0.2, 0.4 m1, 0.5, 0.4 m2, 0.6, 0.4 m2, 0.4, 0.4 m2, 0.3, 0.4
adding horizontal line
#make boxplot horizontal error bars ggplot(exampledata, aes(x = site,y = aluminum_dissolved))+ stat_boxplot(geom='errorbar', linetype=1)+ geom_boxplot(fill="pink") #now want add guideline value @ 0.4 corresponding "federal guideline" in legend, tried: geom_hline(0.4)
and following error:
error in get(x, envir = this, inherits = inh)(this, ...) : mapping should list of unevaluated mappings created aes or aes_string
i tried adding data in string i.e., geom_hline("exampledata$federal_guideline)
same error above.
adding sample size (n= x axis):
finally, want add n label of x-axis (i.e., m2 (n=3)
). able in regular r, following code: names=paste(b$names, "(n=", b$n,")"))
, b
=boxplot
function, can't figure out how in ggplot2
you need explicitly name arguments in geom_hline
, otherwise doesn't know 0.4
referring.
so
ggplot(exampledata, aes(x = site,y = aluminum_dissolved))+ stat_boxplot(geom='errorbar', linetype=1)+ geom_boxplot(fill="pink") + geom_hline(yintercept = 0.4)
will produce required horizontal line.
to change labels on x-axis, use scale_x_discrete
change labels
you can precompute these using like
library(plyr) xlabels <- ddply(exampledata, .(site), summarize, xlabels = paste(unique(site), '\n(n = ', length(site),')')) ggplot(exampledata, aes(x = site,y = aluminum_dissolved))+ stat_boxplot(geom='errorbar', linetype=1)+ geom_boxplot(fill="pink") + geom_hline(yintercept = 0.4) + scale_x_discrete(labels = xlabels[['xlabels']])
Comments
Post a Comment