30:00
7 Application 2: Plotting
8 Plotting in the real world
Much as with our data manipulation applications, examples in textbooks are different from real life. Hopefully any data you are plotting you have already tidied, but this can still lead to problems. In this application, we will practice some of the your plotting skills on the datasets you already analysed in the previous applications.
If you remember, you performed analysis of 3 different issues using Kenya’s 2019 census data:
Breakdown of Kenya by religious belief
Share of people who are migrants in each county
Average working hours for men and women in rural vs. urban areas.
8.0.1 Visualising Religious Breakdown
- Using the religion_overall dataframe, create a bar chart (
geom_col
orgeom_bar(stat="identity"))
showing the share of each religion_label. Mapreligion_label
to the x-axis andshare
to the y-axis. - Add appropriate labels using
labs()
for the title, x-axis, and y-axis. - Format the y-axis to display percentages using
scale_y_continuous(labels = scales::label_percent())
. - Apply a theme, for example
theme_minimal()
. - Change the colour palette using
scale_fill_brewer()
(you’ll need to map religion_label to thefill
aesthetic as well).
8.0.2 Visualising Migration Proportion by County
- Using the migration_prop dataframe created in step 6, create a bar chart showing the migration_prop for each county. Map county to the y-axis and migration_prop to the x-axis for better readability (use geom_col).
- Format the x-axis (which represents the proportion) to display percentages.
- Add appropriate labels using
labs()
. - Apply a theme like
theme_bw()
. - Order the counties by migration proportion.
8.0.3 Visualising Average Working Hours
- Using the working_hours_mean_adult dataframe created in step 10, create a grouped bar chart (
geom_col
) showing the mean_hours_worked_if_work (average hours for those who work). Map area_type (Rural/Urban) to the x-axis and mean_hours_worked_if_work to the y-axis. Map sex_label (Male/Female) to the fill aesthetic to get different coloured bars for men and women. - Use
position = position_dodge()
withingeom_col()
to place the bars for men and women side-by-side within each area type. - Add appropriate labels using
labs()
for the title, axes, and fill legend. - Apply a theme.
- Use
scale_fill_brewer()
to choose a different colour palette for the fill.
8.0.4 Saving a Plot
Finally, practice saving one of the plots you created. Assign one of your plots to a variable. Use ggsave()
to save this plot to a file. Include today’s date in the filename using Sys.Date()
. For example, save it as “migration_plot_YYYY-MM-DD.png”.
Solution: Visualising Religious Breakdown
<- religion_overall |>
religion_overall mutate(religion_lab = case_when(religion==1 ~ "Catholic",
==2 ~ "Protestant",
religion==3 ~ "Evangelical churches",
religion==4 ~ "African Instituted churches",
religion==5 ~ "Orthodox",
religion==6 ~ "Other christian",
religion==7 ~ "Islam",
religion==8 ~ "Hindu",
religion==9 ~ "Traditionalists",
religion==10 ~ "Other religion",
religion==11 ~ "No religion",
religionTRUE ~ NA
|>
)) ggplot(aes(x = religion_lab, y = share, fill = religion_lab)) +
geom_bar(stat = "identity") +
theme_minimal()+
scale_y_continuous(labels = scales::label_percent()) +
labs(x = "religion", y = "share", title = "Religions of Kenya")
Solution: Visualising Migration Proportion by County
<- ggplot(data = migration_prop, aes(x = migration_prop, y = county)) +
migration_plot geom_col() + # Manually set a fill colour
scale_x_continuous(labels = scales::label_percent()) +
labs(
title = "Share of Migrants by County (2019 Census)",
x = "Proportion of Residents Born Elsewhere",
y = "County"
+
) theme_bw()
Solution: Visualising Average Working Hours
ggplot(working_hours_mean_adult, aes(x = area_type, y = mean_hours_worked_if_work, fill = sex_label)) +
geom_col(position = position_dodge()) +
scale_fill_brewer(palette = "Paired") + # Optional: Choose a palette
labs(
title = "Average Weekly Hours Worked by Area and Sex (Adults, 2019 Census)",
fill = "Sex" # Renames the legend
+
) theme_light()
Solution: Saving a Plot
ggsave(filename = paste0("migration_plot\_", Sys.Date(), ".png"), plot = migration_plot, width = 8, height = 10) \# Adjust width/height as needed