I'm getting an error while trying to plot a boxplot. It says I haven't specified the group variable but as you can see, I have. But I'm not sure why this error keeps popping up. Any ideas?
ggplot(fl_n_net, mapping = aes(x = Season, fill = Habitat, group = interaction(Season, Habitat),
lower = mean(Modularity) - sd(Modularity),
upper = mean(Modularity) sd(Modularity), middle = mean(Modularity),
ymin = mean(Modularity) - 3 * sd(Modularity), ymax = mean(Modularity) 3 * sd(Modularity)))
geom_boxplot(stat = "identity", position = position_dodge(width = 0.1))
Error: Can't draw more than one boxplot per group. Did you forget aes(group = ...)?
This is what my data looks like:
CodePudding user response:
The problem here is that you are trying to summarize per group inside aes
, which you cannot do. When you do, for example middle = mean(Modularity)
, this is not calculated for each of your boxes, but simply the grand mean is applied to all your boxes. Instead of trying to do everything inside ggplot, simply group and summarize your data en route to ggplot using group_by
and summarize
:
library(tidyverse)
fl_n_net %>%
group_by(Season, Habitat) %>%
summarize(ymin = mean(Modularity) - 3 * sd(Modularity),
ymax = mean(Modularity) 3 * sd(Modularity),
lower = mean(Modularity) - sd(Modularity),
upper = mean(Modularity) sd(Modularity),
middle = mean(Modularity)) %>%
ggplot(aes(x = Season, fill = Habitat))
geom_boxplot(stat = 'identity',
aes(fill = Habitat, ymin = ymin, ymax = ymax,
lower = lower, middle = middle, upper = upper))
Data used
Obviously, we do not have your data in a format we can use, so I created a sample dataset for the above example. You obviously do not require this part:
set.seed(1)
fl_n_net <- data.frame(
Season = rep(c('Spring', 'Summer', 'Autmun', 'Winter'), each = 15),
Habitat = rep(rep(c('Desert', 'Tundra', 'Rainforest'), each = 5), 4))
fl_n_net <- within(fl_n_net,
Modularity <- rnorm(60, as.numeric(factor(Season)) *
as.numeric(factor(Habitat))))