简体   繁体   中英

How to adjust current scatter plot code to match target scatter plot in ggplot2 (R)?

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? (eg 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")

在此处输入图像描述

Using some random example data this could be achieved like so:

  1. The grey lines could be added using geom_h/vline where I used the mean for the inctercepts
  2. The axis tick labels could be set via breaks
  3. Addjusting the label size and alpha and adding the year for specific points you could make use of ifelse and scale_size/alpha_manual
  4. Additonally I used the same idea to set the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM