# Load necessary libraries
library(ggplot2)
library(dplyr)
# Load the CSV file
<- 'genius_hip_hop_lyrics.csv'
file_path <- read.csv(file_path, encoding = 'latin1')
df
# Define candidate colors
<- c(
candidate_colors 'Hillary Clinton' = '#1E90FF', # Dodger Blue
'Jeb Bush' = '#FF6347', # Tomato
'Chris Christie' = '#D3D3D3', # Light Gray
'Mike Huckabee' = '#FFFF00', # Yellow
'Bernie Sanders' = '#32CD32', # Lime Green
'Ben Carson' = '#8A2BE2', # Blue Violet
'Ted Cruz' = '#FF69B4', # Hot Pink
'Donald Trump' = '#FFA500' # Orange
)
# Plot the data
ggplot(df, aes(x = as.numeric(album_release_date), fill = candidate)) +
geom_dotplot(binwidth = 1, stackdir = 'up', dotsize = 0.8, method = 'dotdensity') +
scale_fill_manual(values = candidate_colors) +
scale_x_continuous(breaks=seq(1990,2020,by=5)) +
theme_minimal() +
labs(
title = 'Every mention of 2016 primary candidates in hip-hop songs',
x = 'Year',
y = 'Mentions',
fill = 'Candidate'
+
) theme(
plot.title = element_text(size = 16, hjust = 0.5),
axis.title = element_text(size = 14),
legend.title = element_text(size = 13),
legend.text = element_text(size = 11),
legend.position='top',
)
Presentation Exercise
Data Source
Article URL: https://projects.fivethirtyeight.com/clinton-trump-hip-hop-lyrics/ Dataset: https://github.com/fivethirtyeight/data/tree/master/hip-hop-candidate-lyrics
AI Prompts
- use the csv file to create a graph like the png file using R
- Using the same CSV file, use the R package gt to create a nice table
- add some pretty formatting to this table
Original Visualization from FiveThirtyEight
# Load necessary libraries
library(dplyr)
library(gt)
# Load the CSV file
<- 'genius_hip_hop_lyrics.csv'
file_path <- read.csv(file_path, encoding = 'latin1')
df
# Create a summary table
<- df %>%
summary_table group_by(candidate, album_release_date) %>%
summarise(mentions = n()) %>%
ungroup() %>%
arrange(desc(mentions))
# Create a nice table using gt
<- gt(summary_table) %>%
gt_table tab_header(
title = 'Mentions of 2016 Primary Candidates in Hip-Hop Songs',
subtitle = 'Summary by Candidate and Year'
%>%
) cols_label(
candidate = 'Candidate',
album_release_date = 'Year',
mentions = 'Number of Mentions'
%>%
) fmt_number(
columns = vars(mentions),
decimals = 0
%>%
) tab_options(
table.font.size = 'medium',
heading.title.font.size = 'large',
heading.subtitle.font.size = 'medium',
column_labels.font.size = 'medium',
table.border.top.color = 'gray',
table.border.bottom.color = 'gray',
table_body.border.top.color = 'gray',
table_body.border.bottom.color = 'gray',
column_labels.border.top.color = 'gray',
column_labels.border.bottom.color = 'gray',
data_row.padding = px(5)
%>%
) tab_style(
style = list(
cell_fill(color = 'lightblue'),
cell_borders(sides = 'all', color = 'white', weight = px(2)),
cell_text(weight = 'bold')
),locations = cells_body(
columns = vars(candidate),
rows = TRUE
)%>%
) tab_style(
style = list(
cell_fill(color = 'lightgray'),
cell_borders(sides = 'all', color = 'white', weight = px(2))
),locations = cells_body(
columns = vars(album_release_date, mentions),
rows = TRUE
)
)
# Print the table
gt_table
Mentions of 2016 Primary Candidates in Hip-Hop Songs | ||
Summary by Candidate and Year | ||
Candidate | Year | Number of Mentions |
---|---|---|
Donald Trump | 2013 | 33 |
Donald Trump | 2015 | 32 |
Donald Trump | 2016 | 27 |
Donald Trump | 2014 | 22 |
Donald Trump | 2012 | 17 |
Hillary Clinton | 2008 | 17 |
Donald Trump | 2011 | 16 |
Donald Trump | 2006 | 12 |
Donald Trump | 2010 | 12 |
Hillary Clinton | 2012 | 11 |
Donald Trump | 2000 | 9 |
Donald Trump | 2002 | 9 |
Donald Trump | 2008 | 9 |
Donald Trump | 2005 | 8 |
Hillary Clinton | 2013 | 8 |
Donald Trump | 1998 | 7 |
Donald Trump | 2007 | 7 |
Donald Trump | 2009 | 7 |
Donald Trump | 1999 | 6 |
Hillary Clinton | 2015 | 6 |
Hillary Clinton | 2009 | 5 |
Hillary Clinton | 2010 | 5 |
Donald Trump | 1992 | 4 |
Donald Trump | 1995 | 4 |
Donald Trump | 2003 | 4 |
Donald Trump | 2004 | 4 |
Hillary Clinton | 2007 | 4 |
Hillary Clinton | 2011 | 4 |
Hillary Clinton | 2014 | 4 |
Donald Trump | 1989 | 3 |
Donald Trump | 1994 | 3 |
Donald Trump | 1996 | 3 |
Donald Trump | 2001 | 3 |
Hillary Clinton | 1999 | 3 |
Hillary Clinton | 2000 | 3 |
Hillary Clinton | 2001 | 3 |
Hillary Clinton | 2016 | 3 |
Jeb Bush | 2005 | 3 |
Bernie Sanders | 2015 | 2 |
Donald Trump | 1990 | 2 |
Donald Trump | 1991 | 2 |
Donald Trump | 1997 | 2 |
Hillary Clinton | 1993 | 2 |
Hillary Clinton | 1994 | 2 |
Hillary Clinton | 1996 | 2 |
Hillary Clinton | 1997 | 2 |
Hillary Clinton | 1998 | 2 |
Hillary Clinton | 2004 | 2 |
Hillary Clinton | 2005 | 2 |
Jeb Bush | 2006 | 2 |
Ben Carson | 2011 | 1 |
Chris Christie | 2014 | 1 |
Chris Christie | 2016 | 1 |
Donald Trump | 1993 | 1 |
Hillary Clinton | 2002 | 1 |
Hillary Clinton | 2003 | 1 |
Jeb Bush | 2001 | 1 |
Jeb Bush | 2007 | 1 |
Jeb Bush | 2012 | 1 |
Jeb Bush | 2013 | 1 |
Mike Huckabee | 2011 | 1 |
Mike Huckabee | 2012 | 1 |
Ted Cruz | 2015 | 1 |