I'm trying to create a grouped bar plot showing the mean values for the data that I am inputting, and at the same time I want to make it easily reproducible so that I can swap in other data sets that are arranged similarly. I don't want to have to calculate the mean manually and input it in a data frame - is there a way to do this?
My data consist of numerical values between 100 - 2000 grouped into 3 different drug treatment groups, which are then subdivided into 3 groups (based on their anatomical location in an organism, termed "Inner", "Middle", "Outer"). The final plot should be 3 groups of 3 bars (each representing the mean values of cell survival in each of the 3 locations). So far I have managed to make individual barplots, but I want to combine them.
Here is some code that I have, and below that is a small excerpt from the data set:
ggplot(Survival, aes(Treatment, Inner)) +
stat_summary(fun.y = mean,
geom = "bar",
position = "dodge")
Treatment Inner Middle Outer
RAD 317 373 354
RAD 323 217 174
RAD 236 255 261
HUTS 1411 1844 1978
HUTS 1922 1756 1856
HUTS 1478 1711 1433
RGD 1433 1489 1633
RGD 1400 1500 1544
RGD 1222 1333 1444
Here's two options: one uses group_by
and summarise
from dplyr
to calculate means, then pipes into ggplot
to make dodged bars. The second uses stat_summary
like your example code does. Both require gather
ing your data into a long format.
library(tidyverse)
df %>%
gather(key = group, value = value, -Treatment) %>%
group_by(Treatment, group) %>%
summarise(mean_val = mean(value, na.rm = T)) %>%
ggplot(aes(x = Treatment, y = mean_val, fill = group)) +
geom_col(position = position_dodge())
df %>%
gather(key = group, value = value, -Treatment) %>%
ggplot(aes(x = Treatment, y = value, fill = group)) +
stat_summary(fun.y = mean, geom = "col", position = position_dodge())