Reputation: 75
I'm plotting a binomial variable (0/1) called "aborted" in the following script:
`ggplot(sab2, aes(x=locality,fill=factor(aborted))) + geom_bar() + scale_fill_manual() + scale_fill_grey(labels = c("aborted","alive")) + xlab("") + ylab("N empty fruits per plant") + guides(fill=guide_legend(title="Fruits vitality")) + facet_grid(~year) + theme_bw() + theme(legend.position = "bottom", panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "white"), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))`
and this was the result:
What could I change in my code if I want to plot only the % of aborted (the "0" level of the "aborted" factor)? I could obtain a plot similar to the follow (but with % of aborted):
thank you!
Upvotes: 3
Views: 3975
Reputation: 93761
Use stat_summary
to calculate the mean value of aborted
, which is just the percentage aborted when aborted
takes on values of 0 or 1. Then you can also use stat_summary
with mean_cl_boot
to get a bootstrapped 95% confidence interval. Here's an example with fake data:
library(scales)
set.seed(389)
sab2 = data.frame(locality=rep(1:6,each=100), aborted=sample(0:1, 600, replace=TRUE))
ggplot(sab2, aes(factor(locality), aborted)) +
stat_summary(fun.y=mean, geom="bar", fill="grey70") +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2) +
scale_y_continuous(labels=percent_format(), limits=c(0,1)) +
theme_bw()
Points might be better than a barplot here:
ggplot(sab2, aes(factor(locality), aborted)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2) +
stat_summary(fun.y=mean, geom="point", shape=21, fill="red", size=2) +
scale_y_continuous(labels=percent_format(), limits=c(0,1)) +
theme_bw()
Or use the percentage values as the point markers:
ggplot(sab2, aes(factor(locality), aborted)) +
stat_summary(fun.data=mean_cl_boot, geom="errorbar", width=0.2, colour="grey60") +
stat_summary(fun.y=mean, geom="text", size=3, colour="red",
aes(label=paste0(sprintf("%1.1f", ..y..*100),"%"))) +
scale_y_continuous(labels=percent_format(), limits=c(0,1)) +
theme_bw()
Upvotes: 4