# A tibble: 5 × 6
state total_vote r_points pct_trump party census
<chr> <dbl> <dbl> <dbl> <chr> <chr>
1 Oregon 2001336 -11.0 39.1 Democratic West
2 Massachusetts 3325046 -27.2 32.8 Democratic Northeast
3 Florida 9502747 1.19 48.6 Republican South
4 District of Columbia 311268 -86.8 4.09 Democratic South
5 Idaho 690433 31.8 59.2 Republican West
# Hex color codes for Dem Blue and Rep Redparty_colors <-c("#2E74C0", "#CB454A") p0 <-ggplot(data =subset(election, st %nin%"DC"),mapping =aes(x = r_points,y =reorder(state, r_points),color = party))p1 <- p0 +geom_vline(xintercept =0, color ="gray30") +geom_point(size =2)p2 <- p1 +scale_color_manual(values = party_colors)p3 <- p2 +scale_x_continuous(breaks =c(-30, -20, -10, 0, 10, 20, 30, 40),labels =c("30\n (Clinton)", "20", "10", "0","10", "20", "30", "40\n(Trump)"))p3 +facet_wrap(~ census, ncol=1, scales="free_y") +guides(color=FALSE) +labs(x ="Point Margin", y ="") +theme(axis.text=element_text(size=8))
Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
of ggplot2 3.3.4.
p0 <-ggplot(data =subset(us_states_elec, region %nin%"district of columbia"),aes(x = long, y = lat, group = group, fill = d_points))p1 <- p0 +geom_polygon(color ="gray90", size =0.1) +coord_map(projection ="albers", lat0 =39, lat1 =45) p2 <- p1 +scale_fill_gradient2(low ="red",mid = scales::muted("purple"),high ="blue") +labs(title ="Winning margins") p2 +theme_map() +labs(fill ="Percent")
Statebins Fix this using ggplot() and geom_statebins()
library(statebins)election %>%statebins(value_col ="pct_trump", name ="Percent Trump",palette ="Reds", direction =1) +theme_statebins(legend_position="top")
state_data <-filter(election, st %in%"DC")state_data
# A tibble: 1 × 23
state st fips total_vote vote_margin winner party pct_margin r_points
<chr> <chr> <dbl> <dbl> <dbl> <chr> <chr> <dbl> <dbl>
1 District … DC 11 311268 270107 Clint… Demo… 0.868 -86.8
# ℹ 14 more variables: d_points <dbl>, pct_clinton <dbl>, pct_trump <dbl>,
# pct_johnson <dbl>, pct_other <dbl>, clinton_vote <dbl>, trump_vote <dbl>,
# johnson_vote <dbl>, other_vote <dbl>, ev_dem <dbl>, ev_rep <dbl>,
# ev_oth <dbl>, census <chr>, region <chr>
election %>%statebins(value_col ="pct_clinton", name ="Percent Clinton",palette ="Blues", direction =1) +theme_statebins(legend_position="top")