Code by student (Brad Jackson)

Recreating Graphic

1. Importing and Tidying

library(tidyverse)

# Import data
sports<-read.csv("https://raw.githubusercontent.com/kitadasmalley/FA2020_DataViz/main/data/NFL_fandom_data.csv",
header=TRUE)
# Tidy the data
## Use gather to create:
### column for sport (categorical variable)
### Column for search interest (numeric - percent)
sportsT<-sports%>%
gather("sport", "searchInterest",-c(DMA, PctTrumpVote))

# Level the sport variable so that its in the right order
sportsT$sport<-factor(sportsT$sport,
 level=c("NBA", "MLB", "NHL", "NFL", "CBB", "NASCAR", "CFB"))

2. Basic Geometries

ggplot(sportsT, aes(x=PctTrumpVote, y=searchInterest))+
  geom_point(alpha=0.15)+
  geom_smooth(se=FALSE, method=lm)+
  facet_grid(~sport)
## `geom_smooth()` using formula 'y ~ x'

3. Fixing Data and Labels

#create color associations
color_table<-matrix(c("NBA", "Blue",
                 "MLB", "Blue",
                 "NHL", "Blue",
                 "NFL", "Purple",
                 "CBB", "Red",
                 "NASCAR", "Red",
                 "CFB", "Red"), nrow=7, byrow=TRUE)
colnames(color_table)<-c("Sport", "Color")

color_table<-as.data.frame(color_table)


#join to Sports
sportsTJ<-sportsT%>%
  left_join(color_table, by = c("sport" = "Sport"))

sportsTR<-sportsTJ%>%
  mutate(sport = str_replace_all(sport, "CBB","College Basketball"), sport = str_replace_all(sport, "CFB","College Football"))%>%
  mutate(PctTrump100 = PctTrumpVote*100, PctSearch = searchInterest*100)


# Level the sport variable so that its in the right order
sportsTR$sport<-factor(sportsTR$sport,
 level=c("NBA", "MLB", "NHL", "NFL", "College Basketball", "NASCAR", "College Football"))

ggplot(sportsTR, aes(x=PctTrump100, y=PctSearch))+
  geom_point(alpha=0.15)+
  geom_smooth(se=FALSE, method=lm)+
  facet_grid(~sport)
## `geom_smooth()` using formula 'y ~ x'

4. Polishing

ggplot(sportsTR, aes(x=PctTrump100, y=PctSearch))+
  geom_point(alpha=0.15)+
  geom_smooth(se=FALSE, method=lm, aes(color=Color))+
  scale_color_manual(values=c("deepskyblue3", "darkorchid4", "firebrick2"))+
  facet_grid(~sport,labeller= labeller(sport = label_wrap_gen(width=10)))+
  scale_x_continuous(name="Trump vote share", limits=c(20,80), breaks=c(20,80))+
  scale_y_continuous(name="Search interest", limits=c(0,50), breaks=c(0,50))+
  
  theme_minimal()+
  theme(plot.background = element_rect("grey90"))+
  coord_fixed(ratio=1.5)+
  
  labs(title= "The NFL has apppeal everywhere",
       subtitle = "Donald Trump's 2016 vote share compared with search interest for \n seven major sports, by media market",
       caption="Search interest based on Google Trends data from 2012 to 2017")+

  
  theme(title = element_text(size=12, face="bold", hjust=0),
        plot.subtitle = element_text(size=11, face="plain", hjust=0),
        plot.caption = element_text(size=10, color="darkgrey", face="plain", hjust=0))+
  

  theme(axis.title.y = element_text(size=10, face="bold", hjust=0.5))+
  theme(axis.title.x =element_text(size=10, face="bold", hjust=0.5))+
  theme(axis.line.x.bottom = element_line(color="black"))+
  theme(panel.grid = element_line(color="darkgrey"))+
  theme(strip.text = element_text(size=8, face="bold"))+
  theme(legend.position = "none")
## `geom_smooth()` using formula 'y ~ x'
## Warning: Removed 18 rows containing non-finite values (stat_smooth).
## Warning: Removed 18 rows containing missing values (geom_point).