## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tibble' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3

In September 2019, YouGov survey asked 1,639 GB adults the following question:

In hindsight, do you think Britain was right/wrong to vote to leave EU?

The data from the survey are in data/brexit.csv.

brexit <- read_csv("data/brexit.csv")

In the course video we made the following visualisation.

brexit <- brexit %>%
  mutate(
    region = fct_relevel(region, "london", "rest_of_south", "midlands_wales", "north", "scot"),
    region = fct_recode(region, London = "london", `Rest of South` = "rest_of_south", `Midlands / Wales` = "midlands_wales", North = "north", Scotland = "scot")
  )

ggplot(brexit, aes(y = opinion, fill = opinion)) +
  geom_bar() +
  facet_wrap(~region, nrow = 1, labeller = label_wrap_gen(width = 12)) +
  guides(fill = FALSE) +
  labs(
    title = "Was Britain right/wrong to vote to leave EU?",
    subtitle = "YouGov Survey Results, 2-3 September 2019",
    caption = "Source: bit.ly/2lCJZVg",
    x = NULL, y = NULL
  ) +
  scale_fill_manual(values = c(
    "Wrong" = "#ef8a62",
    "Right" = "#67a9cf",
    "Don't know" = "gray"
  )) +
  theme_minimal()
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

In this application exercise we tell different stories with the same data.

Exercise 1 - Free scales

Add scales = "free_x" as an argument to the facet_wrap() function. How does the visualisation change? How is the story this visualisation telling different than the story the original plot tells?

# code goes here
ggplot(brexit, aes(y = opinion, fill = opinion)) +
  geom_bar() +
  facet_wrap(~region,
    nrow = 1, labeller = label_wrap_gen(width = 12),
    # ___
  ) +
  guides(fill = FALSE) +
  labs(
    title = "Was Britain right/wrong to vote to leave EU?",
    subtitle = "YouGov Survey Results, 2-3 September 2019",
    caption = "Source: bit.ly/2lCJZVg",
    x = NULL, y = NULL
  ) +
  scale_fill_manual(values = c(
    "Wrong" = "#FFD1DC",
    "Right" = "#67a9cf",
    "Don't know" = "gray"
  )) +
  theme_minimal()

Exercise 2 - Comparing proportions across facets

First, calculate the proportion of wrong, right, and don’t know answers in each category and then plot these proportions (rather than the counts) and then improve axis labeling. How is the story this visualisation telling different than the story the original plot tells? Hint: You’ll need the scales package to improve axis labeling, which means you’ll need to load it on top of the document as well.

# code goes here
# Load necessary packages
library(tidyverse)
library(scales)
## Warning: package 'scales' was built under R version 4.3.3
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
# Calculate proportions
brexit_prop <- brexit %>%
  group_by(region, opinion) %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(region) %>%
  mutate(proportion = count / sum(count))

# Create the faceted bar plot with proportions
ggplot(brexit_prop, aes(x = opinion, y = proportion, fill = opinion)) +
  geom_col() +
  facet_wrap(~region, nrow = 1, labeller = label_wrap_gen(width = 12)) +  # Facet per region
  scale_y_continuous(labels = percent_format()) +  # Display y-axis in percentage format
  labs(
    title = "Was Britain right/wrong to vote to leave EU?",
    subtitle = "YouGov Survey Results, 2-3 September 2019",
    caption = "Source: bit.ly/2lCJZVg",
    x = "Opinion",
    y = "Proportion"
  ) +
  scale_fill_manual(values = c(
    "Wrong" = "#FFD1DC",
    "Right" = "#67a9cf",
    "Don't know" = "gray"
  )) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 10, angle = 45, hjust = 1),  # Rotate x-axis labels
    strip.text = element_text(size = 12, face = "bold")  # Improve facet titles
  )

Exercise 3 - Comparing proportions across bars

Recreate the same visualisation from the previous exercise, this time dodging the bars for opinion proportions for each region, rather than faceting by region and then improve the legend. How is the story this visualisation telling different than the story the previous plot tells?

# code goes here
# Load necessary packages
library(tidyverse)
library(scales)

# Calculate proportions
brexit_prop <- brexit %>%
  group_by(region, opinion) %>%
  summarise(count = n(), .groups = "drop") %>%
  group_by(region) %>%
  mutate(proportion = count / sum(count))

# Create the dodged bar plot (without faceting)
ggplot(brexit_prop, aes(x = region, y = proportion, fill = opinion)) +
  geom_col(position = "dodge") +  # Dodge the bars instead of stacking
  scale_y_continuous(labels = percent_format()) +  # Convert y-axis to percentages
  labs(
    title = "Was Britain right/wrong to vote to leave EU?",
    subtitle = "YouGov Survey Results, 2-3 September 2019",
    caption = "Source: bit.ly/2lCJZVg",
    x = "Region",
    y = "Proportion",
    fill = "Opinion"  # Improve legend label
  ) +
  scale_fill_manual(values = c(
    "Wrong" = "#FFD1DC",
    "Right" = "#67a9cf",
    "Don't know" = "gray"
  )) +
  theme_minimal() +
  theme(
    axis.text.x = element_text(size = 8, angle = 45, hjust = 1),  # Rotate x-axis labels for readability
    legend.position = "bottom",  # Move legend to bottom for better layout
    legend.title = element_text(size = 10, face = "bold")  # Improve legend title readability
  )