Reputation: 91
There is a data set that looks like this showing winners of each election from 1860-today (snippet provided):
year winner win_party ec_pct popular_pct
1860 Abraham Lincoln Rep. 0.59 0.4
1864 Abraham Lincoln Rep. 0.9 0.55
Using the full data set, the image below has been produced (x axis = popular_pct, y axis = ec_pct):
I'm trying to reproduce this in ggplot2 in R. Using the following code, I have reached the second image. How can I edit this code to (a) include the two grey lines in image one, (b) enlarge Donald Trump and Joe Biden (and include the years next to their names) and ensure that they are the only names not faded, (c) adjust the axes so that they match? (e.g. the first image has 40%, 50%, 60% whereas the second image also includes 45% and 55%). And (d) reduce the size of the names of the winners as they are smaller/clearer in image 1.
ggplot(data, aes(x = popular_pct, y = ec_pct)) +
geom_point(alpha = 0.3) + # Fades the dots
scale_y_continuous(labels = function(eldata) paste0(eldata*100, "%")) +
scale_x_continuous(labels = function(eldata) paste0(eldata*100, "%")) +
expand_limits(y = c(0.35, 1)) +
aes(colour = win_party) +
scale_color_manual(values = c("Rep." = "red", "Dem." = "blue")) +
geom_text_repel(
aes(label = winner),
alpha = 0.3, # Fades the winners
hjust = 0, vjust = 0
) +
theme_bw() + # Removes grid background
theme(legend.position = "none")
Upvotes: 0
Views: 55
Reputation: 125547
Using some random example data this could be achieved like so:
geom_h/vline
where I used the mean for the incterceptsbreaks
ifelse
and scale_size/alpha_manual
alpha
and the size
of the points.library(ggplot2)
library(ggrepel)
# Random Example data
set.seed(42)
data <- data.frame(
winner = LETTERS,
year = seq(1920, 2020, 4),
win_party = sample(c("Rep.", "Dem."), 26, replace = TRUE),
popular_pct = runif(26, .35, .65),
ec_pct = runif(26, .35, 1)
)
ggplot(data, aes(x = popular_pct, y = ec_pct)) +
geom_point(aes(size = ifelse(winner %in% c("B", "T"), "btp", "otherp"),
alpha = ifelse(winner %in% c("B", "T"), "bt", "other"))) + # Fades the dots
geom_vline(xintercept = mean(data$popular_pct), color = "grey") +
geom_hline(yintercept = mean(data$ec_pct), color = "grey") +
scale_y_continuous(breaks = c(.4, .6, .8, 1), labels = function(eldata) paste0(eldata*100, "%")) +
scale_x_continuous(breaks = c(.4, .5, .6), labels = function(eldata) paste0(eldata*100, "%")) +
expand_limits(y = c(0.35, 1)) +
aes(colour = win_party) +
scale_color_manual(values = c("Rep." = "red", "Dem." = "blue")) +
scale_size_manual(values = c("bt" = 12 / .pt, "other" = 8 / .pt, btp = 2, otherp = 1)) +
scale_alpha_manual(values = c("bt" = 1, "other" = .3)) +
geom_text_repel(
aes(label = ifelse(winner %in% c("B", "T"), paste(winner, year), winner),
size = ifelse(winner %in% c("B", "T"), "bt", "other"),
alpha = ifelse(winner %in% c("B", "T"), "bt", "other")),
hjust = 0, vjust = 0
) +
theme_bw() + # Removes grid background
theme(legend.position = "none")
Created on 2020-11-25 by the reprex package (v0.3.0)
Upvotes: 1