admin管理员组

文章数量:1410712

I have ggplot2 code that produces a grouped bar chart:

  ggimg3 <-ggplot( f.fam_chart3, aes(x =as.factor(FAM_TYPE), y
=EST_PCT, fill = NAME)) +
    geom_bar(stat ="identity", position = "dodge", color ="black") + 
    geom_text(aes(x =as.factor(FAM_TYPE), y =EST_PCT, label =percent(EST_PCT), group = NAME), 
              position = position_dodge(width =1.0),
              vjust =-0.8,  size =4) +
    scale_fill_manual(values =barCol) +
    scale_y_continuous(limits = c(0, maxLim), label =percent, expand = c(0, 0))

which produces the chart below. (I kept the labeling code out of the snippet; it's not relevant)

My question is, how do I add another variable to this chart? My data includes "CHILDREN," a binary-coded variable that identifies whether children are present in a household. I want to have separate bars for the presence and absence of children for each "CSBG Entity" within each Family Type. How would one do this?

See below for a data sample.

TIA

I have ggplot2 code that produces a grouped bar chart:

  ggimg3 <-ggplot( f.fam_chart3, aes(x =as.factor(FAM_TYPE), y
=EST_PCT, fill = NAME)) +
    geom_bar(stat ="identity", position = "dodge", color ="black") + 
    geom_text(aes(x =as.factor(FAM_TYPE), y =EST_PCT, label =percent(EST_PCT), group = NAME), 
              position = position_dodge(width =1.0),
              vjust =-0.8,  size =4) +
    scale_fill_manual(values =barCol) +
    scale_y_continuous(limits = c(0, maxLim), label =percent, expand = c(0, 0))

which produces the chart below. (I kept the labeling code out of the snippet; it's not relevant)

My question is, how do I add another variable to this chart? My data includes "CHILDREN," a binary-coded variable that identifies whether children are present in a household. I want to have separate bars for the presence and absence of children for each "CSBG Entity" within each Family Type. How would one do this?

See below for a data sample.

TIA

Share Improve this question edited Mar 24 at 3:13 zephryl 17.7k4 gold badges16 silver badges34 bronze badges asked Mar 23 at 20:21 ABickfordABickford 1131 silver badge5 bronze badges 2
  • 2 It would be easier to help you if you provide a minimal reproducible example including a snippet of your input data shared in a reproducible format using e.g. dput(). – stefan Commented Mar 23 at 20:30
  • 2 Also, please do not add data as an image for these reasons. As stefan stated, use dput() instead. – L Tyrone Commented Mar 23 at 21:46
Add a comment  | 

2 Answers 2

Reset to default 2

One solution could be to use an interaction between Name & Children as variable group for the fill-color, stacking the bars on top and taking a lighter color palette for the noChildren parts.

df$group <- paste(df$NAME, df$CHILDREN)

location_colors <- c(
  "United States Children Present" = "#469A7D",      
  "United States No Children Present" = "#7BC3A9",
  "Colorado Children Present" = "#6A3A5E",           
  "Colorado No Children Present" = "#AB6599",
  "Balance of State Children Present" = "#2C7BE1",    
  "Balance of State No Children Present" = "#8AB5EE" 
)

ggplot(df, aes(x = NAME, y = EST_PCT, fill = group)) +
  geom_bar(stat = "identity", position = "stack", color = "black") + 
  geom_text(aes(label = round(EST_PCT, 1)), 
            position = position_stack(vjust = 0.5), 
            size = 3, color = "white") +
  scale_fill_manual(values = location_colors,
                    name = "Location & Children Status") +
  facet_grid(. ~ FAM_TYPE, scales = "free_x", space = "free") +
  labs(x = "Location", y = "Percentage", 
       title = "CSBG Comparison") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text = element_text(size = 10, face = "bold"),
        legend.position = "bottom")

Sample data

library(ggplot2)
library(scales)

df <- data.frame(
  NAME = c(
    "United States", "United States", 
    "Colorado", "Colorado", 
    "Balance of State", "Balance of State", 
    "United States", "United States", 
    "Colorado", "Colorado", 
    "Balance of State", "Balance of State"
  ),
  FAM_TYPE = c(
    "Female HouseholderNo Spouse Present", "Female HouseholderNo Spouse Present",
    "Female HouseholderNo Spouse Present", "Female HouseholderNo Spouse Present",
    "Female HouseholderNo Spouse Present", "Female HouseholderNo Spouse Present",
    "Male HouseholderNo Spouse Present", "Male HouseholderNo Spouse Present",
    "Male HouseholderNo Spouse Present", "Male HouseholderNo Spouse Present",
    "Male HouseholderNo Spouse Present", "Male HouseholderNo Spouse Present"
  ),
  EST_PCT = c(
    60.40511952, 39.59480048, 
    61.88538765, 38.11461235, 
    64.57358762, 35.42641238, 
    53.35791492, 46.64208508, 
    56.71264888, 43.28735112, 
    44.71842719, 55.28157281
  ),
  MOE_PCT = c(
    0.078581894, 0.148918529,
    0.799885358, 0.901272286,
    6.452904629, 4.340354389,
    0.049500921, 0.261532654,
    1.528096247, 1.111109424,
    4.362719255, 11.23949139
  ),
  CHILDREN = c(
    "Children Present", "No Children Present",
    "Children Present", "No Children Present",
    "Children Present", "No Children Present",
    "Children Present", "No Children Present",
    "Children Present", "No Children Present",
    "Children Present", "No Children Present"
  )
)

You can use facet_wrap(), remove spacing between panels, and move strip labels to the bottom to add what looks like another nested level to your horizontal axis:

library(ggplot2)

df$EST_PCT <- df$EST_PCT / 100

barCol <- c("#469A7D", "#2C7BE1")

ggplot(df, aes(x =NAME, y
=EST_PCT, fill = CHILDREN)) +
    geom_bar(stat ="identity", position = "dodge", color ="black") + 
    geom_text(aes(y =EST_PCT, label =percent(EST_PCT), group =CHILDREN), 
              position = position_dodge(width =1.0),
              vjust =-0.8,  size =4) +
    facet_wrap(vars(FAM_TYPE), strip.position = "bottom") +
    scale_fill_manual(values = barCol) +
    scale_y_continuous(limits = c(0, 1), label = percent, expand = c(0, 0)) +
    labs(x = NULL, y = "Percentage", fill = NULL) +
    theme_classic() +
    theme(
      legend.position = "bottom",
      strip.placement = "outside",
      strip.text = element_text(face = "bold"),
      strip.background = element_blank(),
      panel.spacing = unit(0, "lines")
    )

(Data and colors from @TimG’s answer.)

本文标签: rAdding a variable to a grouped var chart in ggplotStack Overflow