I have a dataframe similar to this:
> dput(plot_data_custom)
structure(list(Group = structure(c(1L, 5L, 2L, 3L, 4L, 6L, 7L,
8L, 9L), levels = c("White", "NA", "Other Race", "Two Races",
"Asian", "Female", "Male", "Mandarin", "Spanish"), class = "factor"),
emmean = c(1.8, 2.1, 1.5, 1.9, 1.6, 1.7, 2, 1.4, 1.9), SE = c(0.4,
0.5, 0.3, 0.5, 0.4, 0.4, 0.5, 0.4, 0.5), df = c(22483, 22483,
22483, 22483, 22483, 22483, 22483, 22483, 22483), lower.CL = c(1.3,
1.2, 1, 1.5, 1.2, 1.3, 1.6, 1.1, 1.5), upper.CL = c(2.3,
2.4, 1.8, 2.3, 1.9, 2, 2.5, 1.8, 2.3), Measure = c("Measure",
"Measure", "Measure", "Measure", "Measure", "Measure", "Measure",
"Measure", "Measure"), fill = c("Race", "Race", "Race", "Race",
"Race", "Sex", "Sex", "Language", "Language")), row.names = c(6L,
7L, 8L, 9L, 10L, 13L, 14L, 17L, 18L), class = "data.frame")
and I am trying to create a grouped barplot (grouped by Race, Sex, and Language), but end up with this:
ggplot(plot_data_custom, aes(x = Group, y = emmean, fill = fill)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7), width = 0.7, color = "black") +
geom_errorbar(aes(ymin = lower.CL, ymax = upper.CL), position = position_dodge(width = 0.7), width = 0.2, color = "black") +
labs(title = "Measure", y = "Adjusted Value", x = NULL) +
scale_fill_manual(values = professional_palette, limits = legend_order) +
scale_y_continuous(limits = c(0, 3.5), breaks = seq(0, 3.5, by = 0.5)) +
theme_bw() +
theme(legend.position = "bottom",
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid.major = element_line(color = "lightgray", size = 0.2),
panel.grid.minor = element_blank(),
axis.line = element_line(color = "black"),
plot.title = element_text(face = "bold", size = 14),
legend.text = element_text(size = 10),
legend.key.size = unit(1.5, "lines"),
plot.margin = unit(c(1, 1, 1, 1), "cm")) +
guides(fill = guide_legend(title = NULL))
I would prefer something similar to this, where the different race, sex, and language groups are grouped together on the x-axis.:
Also, if possible, can I include an asterisks on the significant variable (between Asian and White, as White was the reference group), something like this:





x = fill, fill = Group