Help making a complex bar plot with ggplot2

Hi so I am trying to created a bar plot using ggplot2 but I am not sure how to go about doing it. I used the following code to get the data I am trying to graph:

MouseProjectOne %>% 
  filter(protein %in% c("pERK_N","ERK_N"))%>%
  group_by(Genotype, Behavior, Treatment, protein)%>%
  summarise(AverageExpression = mean(expression, na.rm = TRUE))->MouseNew

Through that, I was able to get the average protein expressions that I would like to graph. I am trying to use the average expression to make the following graph, but I am not sure how to go about doing it/if it is possible:

Please help

Not exactly what you want, but pretty close. I might have gotten the groupings wrong, but try something like.

MouseNew %>%  
  tidyr::unite("Behavior_Treatment",c(Behavior, Treatment), remove=FALSE) %>%
  ggplot(aes(x = Behavior_Treatment, y = AverageExpression, fill=protein)) +
  facet_wrap(~Genotype, ncol=2) +

To explain a bit, I created a new variable with unite() which is just concatenating Behaviour and Treatment. Use the new variable to make the bar plot.


Can you post a reproducible example of your code? The screenshot isn't that useful.

1 Like

Something like this? (Edited)

ggplot(df, aes(Treatment, AverageExpression, fill = protein)) +
  geom_col(position = "dodge", stat="identity") + facet_grid(~Genotype + Behavior) +
  labs(x = "Treatment", y = "Average Expression", fill = NULL) + 
  geom_text(aes(label=round(AverageExpression, 2), group=protein), position=position_dodge(width=1), size=4)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.