7  Application 2: Plotting

30:00

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:

  1. Breakdown of Kenya by religious belief

  2. Share of people who are migrants in each county

  3. Average working hours for men and women in rural vs. urban areas.

8.0.1 Visualising Religious Breakdown

  1. Using the religion_overall dataframe, create a bar chart (geom_col or geom_bar(stat="identity")) showing the share of each religion_label. Map religion_label to the x-axis and share to the y-axis.
  2. Add appropriate labels using labs() for the title, x-axis, and y-axis.
  3. Format the y-axis to display percentages using scale_y_continuous(labels = scales::label_percent()).
  4. Apply a theme, for example theme_minimal().
  5. Change the colour palette using scale_fill_brewer() (you’ll need to map religion_label to the fill aesthetic as well).

8.0.2 Visualising Migration Proportion by County

  1. 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).
  2. Format the x-axis (which represents the proportion) to display percentages.
  3. Add appropriate labels using labs().
  4. Apply a theme like theme_bw().
  5. Order the counties by migration proportion.

8.0.3 Visualising Average Working Hours

  1. 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.
  2. Use position = position_dodge() within geom_col() to place the bars for men and women side-by-side within each area type.
  3. Add appropriate labels using labs()for the title, axes, and fill legend.
  4. Apply a theme.
  5. 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",
                                  religion==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",
                                  TRUE ~ 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
migration_plot <- ggplot(data = migration_prop, aes(x = migration_prop, y = county)) +
  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