Instantly share code, notes, and snippets.
Created
December 7, 2022 20:57
-
Save sysilviakim/a383d011ed59a8a39d72932802be4277 to your computer and use it in GitHub Desktop.
Social Media Usage by Demographic Groups
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(Kmisc) | |
## https://www.pewresearch.org/internet/fact-sheet/social-media/ | |
# Read tables quickly ========================================================== | |
pew_01 <- read.table( | |
text = "Facebook Instagram LinkedIn | |
Total 69% 40% 28% | |
Men 61% 36% 31% | |
Women 77% 44% 26% | |
Ages 18-29 70% 71% 30% | |
30-49 77% 48% 36% | |
50-64 73% 29% 33% | |
65+ 50% 13% 11% | |
White 67% 35% 29% | |
Black 74% 49% 27% | |
Hispanic 72% 52% 19% | |
Less than $30K 70% 35% 12% | |
$30K-$49,999 76% 45% 21% | |
$50K-$74,999 61% 39% 21% | |
More than $75K 70% 47% 50% | |
High school or less 64% 30% 10% | |
Some college 71% 44% 28% | |
College graduate 73% 49% 51% | |
Urban 70% 45% 30% | |
Suburban 70% 41% 33% | |
Rural 67% 25% 15%", | |
fill = TRUE, | |
sep = "\t" | |
) | |
pew_02 <- read.table( | |
text = "Twitter Pinterest Snapchat | |
Total 23% 31% 25% | |
Men 25% 16% 22% | |
Women 22% 46% 28% | |
Ages 18-29 42% 32% 65% | |
30-49 27% 34% 24% | |
50-64 18% 38% 12% | |
65+ 7% 18% 2% | |
White 22% 34% 23% | |
Black 29% 35% 26% | |
Hispanic 23% 18% 31% | |
Less than $30K 12% 21% 25% | |
$30K-$49,999 29% 33% 27% | |
$50K-$74,999 22% 29% 29% | |
More than $75K 34% 40% 28% | |
High school or less 14% 22% 21% | |
Some college 26% 36% 32% | |
College graduate 33% 37% 23% | |
Urban 27% 30% 28% | |
Suburban 23% 32% 25% | |
Rural 18% 34% 18% | |
", | |
fill = TRUE, | |
sep = "\t" | |
) | |
pew_03 <- read.table( | |
text = "YouTube WhatsApp Reddit | |
Total 81% 23% 18% | |
Men 82% 26% 23% | |
Women 80% 21% 12% | |
Ages 18-29 95% 24% 36% | |
30-49 91% 30% 22% | |
50-64 83% 23% 10% | |
65+ 49% 10% 3% | |
White 79% 16% 17% | |
Black 84% 23% 17% | |
Hispanic 85% 46% 14% | |
Less than $30K 75% 23% 10% | |
$30K-$49,999 83% 20% 17% | |
$50K-$74,999 79% 19% 20% | |
More than $75K 90% 29% 26% | |
High school or less 70% 20% 9% | |
Some college 86% 16% 20% | |
College graduate 89% 33% 26% | |
Urban 84% 28% 18% | |
Suburban 81% 23% 21% | |
Rural 74% 9% 10%", | |
fill = TRUE, | |
sep = "\t" | |
) | |
pew_04 <- read.table( | |
text = "TikTok Nextdoor | |
Total 21% 13% | |
Men 17% 10% | |
Women 24% 16% | |
Ages 18-29 48% 5% | |
30-49 22% 17% | |
50-64 14% 16% | |
65+ 4% 8% | |
White 18% 15% | |
Black 30% 10% | |
Hispanic 31% 8% | |
Less than $30K 22% 6% | |
$30K-$49,999 29% 11% | |
$50K-$74,999 20% 12% | |
More than $75K 20% 20% | |
High school or less 21% 4% | |
Some college 24% 12% | |
College graduate 19% 24% | |
Urban 24% 17% | |
Suburban 20% 14% | |
Rural 16% 2%", | |
fill = TRUE, | |
sep = "\t" | |
) | |
# Merge than get row names into column ========================================= | |
pew <- bind_cols(pew_01, pew_02, pew_03, pew_04) %>% | |
mutate(across(everything(), ~ as.numeric(gsub("%", "", .x)))) %>% | |
rownames_to_column(var = "type") %>% | |
filter(type != "Total") ## %>% | |
## select(-Nextdoor, -Snapchat, -Reddit) ## -WhatsApp, | |
## Quickly | |
pew$category <- c( | |
rep("Gender", 2), rep("Age", 4), rep("Race", 3), rep("Income", 4), | |
rep("Education", 3), rep("Urbanism", 3) | |
) | |
pew <- pew %>% | |
pivot_longer( | |
!type & !category, | |
names_to = "Platform", values_to = "Proportion" | |
) %>% | |
mutate( | |
Platform = factor( | |
Platform, | |
levels = c( | |
"YouTube", "Facebook", "Instagram", "Pinterest", "LinkedIn", | |
"Twitter", "TikTok", "WhatsApp", "Reddit", "Snapchat", "Nextdoor" | |
) | |
) | |
) | |
# ggplot2 ====================================================================== | |
p1 <- ggplot(pew %>% filter(category == "Gender")) + | |
geom_col( | |
aes(x = Platform, y = Proportion, colour = NULL, fill = Platform) | |
) + | |
facet_wrap(~type, ncol = 2) + | |
scale_fill_viridis_d() | |
pdf("pew_social_media_gender.pdf", width = 6, height = 3) | |
print( | |
plot_nolegend(pdf_default(p1)) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
) | |
dev.off() | |
p2 <- ggplot(pew %>% filter(category == "Age")) + | |
geom_col( | |
aes(x = Platform, y = Proportion, colour = NULL, fill = Platform) | |
) + | |
facet_wrap(~type, ncol = 2) + | |
scale_fill_viridis_d() | |
pdf("pew_social_media_age.pdf", width = 6, height = 6) | |
print( | |
plot_nolegend(pdf_default(p2)) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
) | |
dev.off() | |
p3 <- ggplot(pew %>% filter(category == "Race")) + | |
geom_col( | |
aes(x = Platform, y = Proportion, colour = NULL, fill = Platform) | |
) + | |
facet_wrap(~type, ncol = 2) + | |
scale_fill_viridis_d() | |
pdf("pew_social_media_race.pdf", width = 6, height = 6) | |
print( | |
plot_nolegend(pdf_default(p3)) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
) | |
dev.off() | |
p4 <- ggplot(pew %>% filter(category == "Income")) + | |
geom_col( | |
aes(x = Platform, y = Proportion, colour = NULL, fill = Platform) | |
) + | |
facet_wrap(~type, ncol = 2) + | |
scale_fill_viridis_d() | |
pdf("pew_social_media_income.pdf", width = 6, height = 6) | |
print( | |
plot_nolegend(pdf_default(p4)) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
) | |
dev.off() | |
p5 <- ggplot(pew %>% filter(category == "Education")) + | |
geom_col( | |
aes(x = Platform, y = Proportion, colour = NULL, fill = Platform) | |
) + | |
facet_wrap(~type, ncol = 2) + | |
scale_fill_viridis_d() | |
pdf("pew_social_media_education.pdf", width = 6, height = 6) | |
print( | |
plot_nolegend(pdf_default(p5)) + | |
theme(axis.text.x = element_text(angle = 45, hjust = 1)) | |
) | |
dev.off() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment