0

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))

enter image description here

I would prefer something similar to this, where the different race, sex, and language groups are grouped together on the x-axis.:

enter image description here

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: enter image description here

1
  • change to x = fill, fill = Group Commented Jan 25, 2024 at 18:00

2 Answers 2

0

Since race, sex, and language are different variables, it makes sense plot them as separate plots of as separate facets/panels of one plot.

You can do the latter by introducing facet_wrap(vars(fill), scales = "free_x") + to your ggplot()-layers.

Here is what that looks like in context:

library(tidyverse)

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) +
  # I don't have your professional_palette object, so I commented it out here.
  #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)) +
  facet_wrap(vars(fill), scales = "free_x") +
  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))

Sign up to request clarification or add additional context in comments.

Comments

0

Not quite sure what you are trying to achieve. Either the code given by @Till is correct or this is correct:

ggplot(plot_data_custom, 
             aes(x = fill, y = emmean, fill = fill, group = Group)) +
    geom_col(position = position_dodge(width = 0.9),
                     width = 0.9, color = "black") + 
    geom_text(aes(label = Group, y = 0),
                        position = position_dodge(width = 0.9),
                        vjust = 0, angle = 90, hjust=-0) + 
    guides(fill = 'none')

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.