I have a dataset that looks like this:
conifer.abundance <- c(6,7,8,2,3,4,5,1,7,8,9,8,7,6,5,1)
lily.abundance <- c(5,5,5,5,4,4,4,4,6,7,8,2,3,4,5,1)
type <- c("Control","Control","Control","Control","Control","Control","Control","Control","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment","Treatment")
class <- c("City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural","City","Rural")
climate <- c("wet","wet","dry","dry","wet","wet","dry","dry","wet","wet","dry","dry","wet","wet","dry","dry")
all.abundance <- conifer.abundance + lily.abundance
dat88 <- data.frame(climate,type,class,conifer.abundance, lily.abundance,all.abundance)
This is a 2x2x2 design. I want to plot barplots such that the mean of all.abundance is represented as sum of mean conifer.abundance and mean lily.abundance (stacked) and it has a legend of its own. I tried following this code, but it seems like it using fill to stack the bars, but I need to use it for a different purpose here. Suppose, I have several more data points, I would also need to plot a bootstrapped confidence interval (as below). Any suggestions? Here is my current code for plotting the graph above.
pd <- position_dodge(0.82)
ggplot(dat88, aes(x=class, y=all.abundance, fill = climate)) +
theme_bw() +
stat_summary(geom="bar", fun.y=mean, position = "dodge") +
stat_summary(geom="errorbar", fun.data=mean_cl_boot,position = pd) +
ylab("Total Abundance") +
facet_grid(~type)
Please note that I have slightly changed the dataset to represent a more biologically fitting scenario.
If you want to stack the height values for female & male, you'll need to melt / gather them into a single variable.
The following two methods for manipulating the data frame are equivalent. Depends on which packages you are more familiar with:
Since this is a 2 x 2 x 2 design, I added a dimension to
facet_grid
to show both type and species. If that's not needed, simply revert tofacet_grid(~type)
: