I want to produce a grouped bar graph from my data which contains seven different conditions (two factors) with three replicates per treatment. The reference group ('Water') is ungrouped, because it contains only one conditition and is shown as a single bar, the other treatments are grouped. I used position_dodge2 with preserve = "single" to keep all bars at the same width, but then the single bar has a lot of space around it which I would like to get rid of while keeping the same width as the other bars. How is that possible?
I tried to add width and padding in position_dodge2, but it did not work for me. I would like to avoid facetting, and keep the width of the other bars and the "lonely" one centred on its x-axis label.
My data (data2):
structure(list(glue = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 3L,
3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Methylcellulose",
"Water", "Xanthan"), class = "factor"), sug = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 2L,
2L, 3L, 3L, 3L), .Label = c("Control", "Mannitol", "Sorbitol"
), class = "factor"), sg = c(85, 91.76470588, 89.79591837, 69.76744186,
93.82716049, 92.59259259, 52.94117647, 77.21518987, 84.81012658,
87.5, 87.05882353, 83.13253012, 68.6746988, 55.55555556, 73,
68.6746988, 82.71604938, 93.75, 34.52380952, 83.52941176, 86.9047619
)), class = "data.frame", row.names = c(NA, -21L))
And this is my plot after sorting the levels in the desired order:
ggplot(data = data2, aes(x =glue, y = sg, fill = sug)) +
stat_summary(geom = "bar", position = position_dodge2(width = 2, preserve = "single"), color = "black") +
stat_summary(fun.data = mean_se, geom = "errorbar", position = position_dodge2( preserve = "single", padding = 0.7)) +
theme_classic() +
ylab("Germination rate [%]") +
theme(legend.position = "bottom",
text = element_text(size = 12, color = "black"),
axis.text = element_text(size = 12, color = "black"),
axis.title = element_text(face = "bold", vjust = 3),
legend.title = element_text(face = "bold"),
axis.line = element_line(size=1),
axis.ticks.x = element_line (size = 1)) +
scale_x_discrete("Seed coat") +
scale_fill_manual(name = "Sugar",values = c("White", "LightGrey", "#838B8B")) +
scale_y_continuous(limits = c(0,100))
Thanks!