Data: the changing childhood project by UNICEF
Today’s data comes from The Changing Childhood Project (conducted by UNICEF). The New York Times published an intresting article based on this dataset.
Import the CSV file into R and call it df.child; for simplicity, create a smaller dataset df.child.small containing only three countries and a few variables.
COUNTRYNEW: respondent’s country.AGE: respondent’s age group (the survey compares ages 15-24 with ages 40 and older).WP22110: minimum acceptable age for men to get married. 97 means 97 or older; 98 is “Don’t know”; 99 is “Refused”.WP22109: minimum acceptable age for women to get married. 97 means 97 or older; 98 is “Don’t know”; 99 is “Refused”.WP30: satisfaction with standard of living: 1 = satisfied, 2 = dissatisfied, 3 = “Don’t know”, 4 = refused.WP22091: which group the respondent most identifies with: 1 = city or area where they live, 2 = their country, 3 = the world, 8 = “Don’t know”, 9 = refused.
The decimal point is 1 digit(s) to the right of the |
1 | 003444
1 | 55555555555555555555556666666666666666666666666666666666666666666666+1468
2 | 00000000000000000000000000000000000000000000000000000000000000000000+628
2 | 55555555555555555555555555555555555555555555555555555555555555555555+573
3 | 00000000000000000000000000000000000000000000000000000000000000000000+249
3 | 555555555555555555555555555555688
4 | 000
4 |
5 | 0
5 |
6 |
6 |
7 |
7 |
8 |
8 | 7
9 |
9 | 888888888888888888888888888888888888888888888999
Comment: with base R, plotting can be fast, but it’s not very easy to incooperate other steps (like removing invalid values 97, 98, 99). Adding layers (e.g., a reference line) is also not very convenient.
| Feature | ggplot2 | Base R plot functions |
|---|---|---|
| Syntax | Unified, intuitive syntax (the “grammar of graphics”) | Syntax depends on the plot type, such as plot() or hist() |
| Storing plots | Plots can be stored as ggplot objects | Plots are drawn directly rather than stored as R objects |
| Adding layers | Add layers to an existing plot with + |
Add to the current plot with additional plotting commands |
Use the same code for example 2, but change geom_boxplot to geom_violin.
Scatterplots can be hard to read when both variables are categorical. Jitter separates overlapping responses.
Change geom_point() to geom_jitter()
df.child.small %>%
filter(WP22110 < 97, WP22109 < 97) %>%
group_by(COUNTRYNEW) %>%
summarise(mean_male_age=mean(WP22110),
mean_female_age=mean(WP22109),
.groups="drop") %>%
ggplot(aes(x=mean_female_age, y=mean_male_age, label=COUNTRYNEW)) +
geom_text(size=0.5) +
labs(x="mean min age for women", y="mean min age for men")Comment: it is very easy to combine data manipulation steps with ggplot.
ggplot offers a very convenient syntax (+) to add elements and layers
Comment: in this example, the same variable (WP22110) is mapped to both the x-axis and the color aesthetic. This is not recommended as it is redundant.
WP22091 records whether respondents identify most with their city or area (1), their country (2), or the world (3). WP30 records satisfaction with standard of living: satisfied (1) or dissatisfied (2).
df.child %>%
filter(COUNTRYNEW %in% c("Germany", "United States"),
WP22091 %in% 1:3, WP30 %in% 1:2) %>%
ggplot(aes(x = as.factor(WP22091), y = as.factor(WP30))) +
geom_jitter() +
theme_bw() +
facet_wrap(~COUNTRYNEW) +
ggtitle("Identity and standard-of-living satisfaction") +
labs(
x = "Identity (1 = city/area, 2 = country, 3 = world)",
y = "Standard of living (1 = satisfied, 2 = dissatisfied)"
)Comment: ggplot works very well with tidyverse functions; as you can see in this example, we actually do not need to use the smaller dataset, since it’s easy to filter information from the large dataset before visualization.
WP22071: whether digital technology helps children with education.WP22072: whether digital technology helps children have fun.1 = a lot, 2 = a little, and 3 = not at all. Other response codes are excluded.Try to reproduce the following graph. You can also use other variables of your interest (refer to the codebook).
df.child %>%
filter(WP22071 %in% 1:3, WP22072 %in% 1:3) %>%
group_by(COUNTRYNEW) %>%
summarise(
mean_education = mean(WP22071),
mean_fun = mean(WP22072),
.groups = "drop"
) %>%
ggplot(aes(x = mean_education, y = mean_fun, label = COUNTRYNEW)) +
geom_abline(slope=1, intercept=0, linetype="dashed") +
geom_label() +
theme_bw() +
labs(
title = "Average views on digital technology by country",
x = "Not helpful with education",
y = "Not helpful with fun"
)Comment: filter is a function in the dplyr package, but it can be used without the pipe operator, just as any other R function.
plot_ly(data=filter(df.child, WP22110 < 97, WP22109 < 97, WP22091 < 4),
x=~WP22110, y=~WP22109,
z=~WP22091, text=~COUNTRYNEW,
color=~COUNTRYNEW, size=I(4)) %>%
layout(scene=list(xaxis=list(title="Minimum acceptable age for men"),
yaxis=list(title="Minimum acceptable age for women"),
zaxis=list(title="Identity (1-3)")))p1 <- df.child %>%
filter(COUNTRYNEW %in% c("Germany", "United States"),
WP22091 %in% 1:3, WP30 %in% 1:2) %>%
ggplot(aes(x = as.factor(WP22091), y = as.factor(WP30))) +
geom_jitter() +
theme_bw() +
facet_wrap(~COUNTRYNEW) +
ggtitle("Identity and standard-of-living satisfaction") +
labs(
x = "Identity (1 = city/area, 2 = country, 3 = world)",
y = "Standard of living (1 = satisfied, 2 = dissatisfied)"
)
ggplotly(p1)