4.1 Category 2 Variable 1: Median Age

4.1.1 Map of Median Age in the United States

## create color palette used by map
pal <- colorBin("BrBG", metadataGIS$median_age, n = 9, reverse=TRUE)

leaflet(metadataGIS, options = leafletOptions(crsClass = "L.CRS.EPSG3857"), width="100%") %>%
  addPolygons(weight = 0.5, color = "gray", opacity = 0.7,
    fillColor = ~pal(median_age), fillOpacity = 1, smoothFactor = 0.5,
    label = popupPlus("Median Age", metadataGIS$median_age, "number"),
    labelOptions = labelOptions(direction = "auto")) %>%
    addPolygons(data = stateGIS,fill = FALSE,color="black",weight = 1) %>%
    addLegend(pal = pal,values = ~metadataGIS$median_age, opacity = 1, title = "Median Age",position = "bottomright")

4.1.2 Scatter plot of Vote for GOP(%) over Median Age

ggplot(metadata) +
  geom_point(aes(median_age, pctGOP*100), color = "#666666", alpha = 0.2) +
  geom_histogram(aes(median_age, ..count../50), alpha = 0.2, color = "#33A02B", fill ="#B2DF8A" ) +
  geom_smooth(aes(median_age, pctGOP*100, color = "Linear Regression", fill = "Linear Regression"), method = "lm" ) +
  geom_smooth(aes(median_age, pctGOP*100, color = "Local Polynomial Regression", fill = "Local Polynomial Regression"), method = "loess") +
  labs(title="Regression line median age on percentage vote for GOP", x= "Median Age", y= "Vote for GOP(%)", caption = "Histogram of the variable at the bottom, not scaled to the y axis.")+
  scale_colour_manual(name="Model Fit", values=c("blue", "red"))+
  scale_fill_manual(name="Model Fit", values=c("#A6CEE3", "#F89F9E"))+
  theme_bw() +
  theme(legend.position = "bottom")

4.1.3 Summary statistics for regression of Vote for GOP(%) over Median Age

model2.1 <- lm(pctGOP~median_age,data=metadata)
stargazer(model2.1, 
          type = "html", 
          report=('vc*p'),
          keep.stat = c("n","rsq","adj.rsq"), 
          notes = "<em>&#42;p&lt;0.1;&#42;&#42;p&lt;0.05;&#42;&#42;&#42;p&lt;0.01</em>", 
          notes.append = FALSE)
Dependent variable:
pctGOP
median_age 0.007***
p = 0.000
Constant 0.353***
p = 0.000
Observations 3,108
R2 0.060
Adjusted R2 0.060
Note: *p<0.1;**p<0.05;***p<0.01