Mean Wage, by Sex and Sector
From the CPS85 data set in the mosaicData package:
MeanWages <-
mosaicData::CPS85 %>%
group_by(sector, sex) %>%
summarise(meanWage = mean(wage, na.rm = TRUE))Here’s the table:
| sector | sex | meanWage |
|---|---|---|
| clerical | F | 7.404210 |
| clerical | M | 7.489048 |
| const | M | 9.502000 |
| manag | F | 11.056190 |
| manag | M | 13.721765 |
| manuf | F | 5.713750 |
| manuf | M | 9.302727 |
| other | F | 5.801667 |
| other | M | 8.761774 |
| prof | F | 11.105000 |
| prof | M | 12.773962 |
| sales | F | 5.241765 |
| sales | M | 9.495714 |
| service | F | 6.059388 |
| service | M | 7.226471 |
The Lollypop Plot: a Graphical Approach
There isn’t much data in the table, so if we insist on making a graph it should have a very light footprint. Here’s one that I call a lollypop plot:
MeanWages %>%
ggplot() +
geom_point(aes(x = sector,
y = meanWage,
color = sex),
size = 2.5,
position = position_dodge(width = 0.7)) +
geom_bar(aes(x = sector, y = meanWage, fill = sex),
width = 0.1, stat = "identity",
position = position_dodge(width = 0.7)) +
labs(x = "Sector of Employment",
y = "mean wage ($/hr)",
title = "Males made more than females\nin almost every sector!") +
theme(panel.grid.major.x = element_blank())Priority
I discovered that lollypop plots have already been investigated in ggplot2 by Yan Holtz. See his excellent R Graph Gallery.
Modufying Yan’s ideas a bit, we might create a double-pop lollypop plot as follows:
MeanWages %>%
ggplot(aes(x = sector, y = meanWage, group = sector)) +
geom_point(aes(color = sex), size = 2) +
scale_color_manual(values = c("blue", "red")) +
labs(x = NULL, y = "mean wage ($/hr)") +
geom_line() +
coord_flip() +
theme_light() +
theme(panel.border = element_blank())Let’s consider re-ordering the sectors by the value male mean wage:
MeanWages %>%
inner_join(MeanWages %>%
ungroup() %>%
filter(sex == "M") %>%
rename(maleMean = meanWage) %>%
select(-sex),
by = "sector") %>%
ggplot(aes(x = forcats::fct_reorder(sector, maleMean),
y = meanWage, group = sector)) +
geom_point(aes(color = sex), size = 2) +
scale_color_manual(values = c("blue", "red")) +
geom_line() +
labs(x = NULL, y = "mean wage ($/hr)") +
coord_flip() +
theme_light() +
theme(panel.border = element_blank())