── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
Code
## New packages we'll uselibrary(ggrepel) #<<library(scales) #<<
Attaching package: 'scales'
The following object is masked from 'package:purrr':
discard
The following object is masked from 'package:readr':
col_factor
Text in Plots
Code
elections_historic
# A tibble: 49 × 19
election year winner win_p…¹ ec_pct popul…² popul…³ votes margin runne…⁴
<int> <int> <chr> <chr> <dbl> <dbl> <dbl> <int> <int> <chr>
1 10 1824 John Qui… D.-R. 0.322 0.309 -0.104 1.13e5 -38221 Andrew…
2 11 1828 Andrew J… Dem. 0.682 0.559 0.122 6.43e5 140839 John Q…
3 12 1832 Andrew J… Dem. 0.766 0.547 0.178 7.03e5 228628 Henry …
4 13 1836 Martin V… Dem. 0.578 0.508 0.142 7.63e5 213384 Willia…
5 14 1840 William … Whig 0.796 0.529 0.0605 1.28e6 145938 Martin…
6 15 1844 James Po… Dem. 0.618 0.495 0.0145 1.34e6 39413 Henry …
7 16 1848 Zachary … Whig 0.562 0.473 0.0479 1.36e6 137882 Lewis …
8 17 1852 Franklin… Dem. 0.858 0.508 0.0695 1.61e6 219525 Winfie…
9 18 1856 James Bu… Dem. 0.588 0.453 0.122 1.84e6 494472 John F…
10 19 1860 Abraham … Rep. 0.594 0.396 0.101 1.86e6 474049 John B…
# … with 39 more rows, 9 more variables: ru_part <chr>, turnout_pct <dbl>,
# winner_lname <chr>, winner_label <chr>, ru_lname <chr>, ru_label <chr>,
# two_term <lgl>, ec_votes <dbl>, ec_denom <dbl>, and abbreviated variable
# names ¹win_party, ²popular_pct, ³popular_margin, ⁴runner_up
Code
## Set up long strings as objects, for neatness.p_title <-"Presidential Elections: Popular & Electoral College Margins"p_subtitle <-"1824-2016"p_caption <-"Data for 2016 are provisional."x_label <-"Winner's share of Popular Vote"y_label <-"Winner's share of Electoral College Votes"
Code
p <-ggplot(data = elections_historic, mapping =aes(x = popular_pct, y = ec_pct,label = winner_label))p +geom_hline(yintercept =0.5, size =1.4, color ="gray80") +geom_vline(xintercept =0.5, size =1.4, color ="gray80") +geom_point()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
Code
p <-ggplot(data = elections_historic, mapping =aes(x = popular_pct, y = ec_pct,label = winner_label))p +geom_hline(yintercept =0.5, linewidth =1.4, color ="gray80") +geom_vline(xintercept =0.5, linewidth =1.4, color ="gray80") +geom_point() +geom_text_repel()
Warning: ggrepel: 19 unlabeled data points (too many overlaps). Consider
increasing max.overlaps
Resize it with fig.width and fig.height in the chunk options:
Code
p <-ggplot(data = elections_historic, mapping =aes(x = popular_pct, y = ec_pct,label = winner_label))p_out <- p +geom_hline(yintercept =0.5, size =1.4, color ="gray80") +geom_vline(xintercept =0.5, linewidth =1.4, color ="gray80") +geom_point() +geom_text_repel() +#<<scale_x_continuous(labels =label_percent()) +scale_y_continuous(labels =label_percent()) +labs(x = x_label, y = y_label, #<<title = p_title, subtitle = p_subtitle,caption = p_caption) p_out