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?
TIA
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")
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"
)
)