Created
September 1, 2021 16:55
-
-
Save k5cents/57c103dcd191c4d302319643d075f69d to your computer and use it in GitHub Desktop.
Plotting Ipsos' Understanding Society survey on music preference by generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
library(tabulizer) | |
library(janitor) | |
library(scales) | |
library(fs) | |
ipsos_pdf <- file_temp(ext = "pdf") | |
download.file( | |
"https://www.ipsos.com/sites/default/files/ct/news/documents/2021-09/Understanding%20Society%20Wave%2018%20Topline_083121.pdf", | |
destfile = ipsos_pdf | |
) | |
genre1 <- extract_tables( | |
file = ipsos_pdf, | |
pages = list(4), | |
method = "lattice", | |
area = list(c(top = 150, left = 50, bottom = 350, right = 570)) | |
) | |
period1 <- extract_tables( | |
file = ipsos_pdf, | |
pages = list(4), | |
method = "lattice", | |
area = list(c(top = 380, left = 50, bottom = 570, right = 570)) | |
) | |
# ------------------------------------------------------------------------- | |
genre2 <- as_tibble(genre1[[1]]) %>% | |
row_to_names(row_number = 1) %>% | |
rename(Genre = 1, `Baby Boomer` = 6) %>% | |
type_convert( | |
na = c("*", "-"), | |
col_types = cols( | |
.default = col_number(), | |
total = col_skip(), | |
Genre = col_character() | |
) | |
) | |
period2 <- as_tibble(period1[[1]]) %>% | |
row_to_names(row_number = 1) %>% | |
rename(Period = 1, `Baby Boomer` = 6) %>% | |
type_convert( | |
na = c("*", "-"), | |
col_types = cols( | |
.default = col_number(), | |
Period = col_character() | |
) | |
) | |
# ------------------------------------------------------------------------- | |
genre2$Genre[genre2$Genre == "EDM/House music"] <- "EDM" | |
genre2$Genre[genre2$Genre == "Alternative rock"] <- "Alt. Rock" | |
genre2$Genre[genre2$Genre == "None of the above"] <- "None Above" | |
# genre2$Genre <- factor( | |
# x = genre2$Genre, | |
# levels = rev(genre2$Genre[order(genre2$Total)]) | |
# ) | |
genre3 <- pivot_longer( | |
data = genre2, | |
cols = -Genre, | |
names_to = "Generation", | |
values_to = "Percent" | |
) | |
genre3$Generation <- factor( | |
x = genre3$Generation, | |
levels = rev(unique(genre3$Generation)) | |
) | |
period2$Period <- str_replace(period2$Period, "’", "'") | |
# period2$Period[period2$Period == "Skipped"] <- NA | |
period2$Period[period2$Period == "Sometime before the 1920's"] <- "Pre 1920's" | |
period2$Period[period2$Period == "Current decade"] <- "Now" | |
period2$Period <- str_remove(period2$Period, "19|20") | |
period3 <- pivot_longer( | |
data = period2, | |
cols = -Period, | |
names_to = "Generation", | |
values_to = "Percent" | |
) | |
period3$Period <- factor( | |
x = period3$Period, | |
levels = c("Skipped", "Pre 20's", "20's", "30's", "40's", "50's", "60's", | |
"70's", "80's", "90's", "00's", "10's", "Now") | |
) | |
period3$Generation <- factor( | |
x = period3$Generation, | |
levels = rev(unique(period3$Generation)) | |
) | |
# ------------------------------------------------------------------------- | |
period_plot <- period3 %>% | |
mutate(across(Percent, `/`, 100)) %>% | |
ggplot(aes(x = Period, y = Percent)) + | |
geom_col(aes(fill = Period)) + | |
facet_wrap(~Generation, ncol = 1) + | |
scale_fill_discrete(guide = "none") + | |
scale_y_continuous(labels = label_percent()) + theme( | |
axis.title.x = element_text(margin = margin(10, 0, 0, 0)), | |
axis.title.y = element_text(margin = margin(0, 10, 0, 0)) | |
) + | |
labs( | |
title = "How America's generations view music by period and genre", | |
subtitle = "In your opinion, which decades produced the best music? Select up to three.", | |
caption = "" | |
) | |
ggsave( | |
filename = "~/Pictures/ipsos_period.png", | |
plot = period_plot, | |
width = 8, | |
height = 9, | |
dpi = "retina" | |
) | |
# ------------------------------------------------------------------------- | |
genre_plot <- genre3 %>% | |
filter(Genre != "Skipped") %>% | |
mutate(across(Percent, `/`, 100)) %>% | |
ggplot(aes(x = reorder(Genre, Percent), y = Percent)) + | |
geom_col(aes(fill = Genre)) + | |
facet_wrap(~Generation, ncol = 1) + | |
scale_fill_discrete(guide = "none") + | |
scale_y_continuous(labels = label_percent()) + | |
theme( | |
axis.title.x = element_text(margin = margin(10, 0, 0, 0)), | |
axis.title.y = element_blank() | |
) + | |
labs( | |
title = "", | |
subtitle = "What are your preferred genres of music? Select up to three.", | |
caption = "Source: Ipsos's Understanding Society (Wave 18)", | |
x = "Genre", | |
y = "" | |
) | |
ggsave( | |
filename = "~/Pictures/ipsos_genre.png", | |
plot = genre_plot, | |
width = 8, | |
height = 9, | |
dpi = "retina" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment