In ggplot, how can I plot data as a line graph?

限于喜欢 提交于 2020-01-25 08:17:18

问题


I apologize that I am a beginner in R. I am trying to make the graph like the below picture.

This is what I did in code. But it does not work : unemp <- read.csv("unemployment.csv", stringsAsFactors = FALSE)

# adding background colors for different presidents
name <- c("Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon",
      "Ford", "Carter", "Reagan", "Bush I", "Clinton", "Bush II",
      "Obama")
start <- as.Date(c("1948-01-01", "1953-01-20", "1961-01-20", "1963-11-22",
               "1969-01-20", "1974-08-09", "1977-01-20", "1981-01-20",
               "1989-01-20", "1993-01-20", "2001-01-20", "2009-01-20"))
end <- c(start[-1], as.Date("2016-10-01"))
party <- c("D", "R", "D", "D", "R", "R", "D", "R", "R", "D", "R", "D")
pres <- data.frame(name, start, end, party, stringsAsFactors = FALSE)
head(unemp)

p <- ggplot(unemp) +
geom_rect(data = pres,
aes(xmin = start, xmax = end, fill = party),
ymin = -Inf, ymax = Inf, alpha = 0.2) +
geom_vline(aes(data = pres, xintercept = as.numeric(start)), colour = "grey50", alpha = 0.5) +
geom_text(data = pres, aes(x = start, y = 2500, label = name), size = 3, vjust = 0, hjust = 0, nudge_x = 50, check_overlap = TRUE) +
geom_line(data = pres aes(date, unemp)) + geom_rect(data = pres, aes(xmin = start, xmax = end),
ymin = 10000, ymax = Inf, alpha = 0.4, fill = "chartreuse")

Also, the used csv file("unemployment.csv") is like below

    date   uempmed
  <date>  <dbl>    
1 1948-01-01 4.5    
2 1948-02-01 4.7     
3 1948-03-01 4.6     
4 1948-04-01 4.9    
5 1948-05-01 4.7     
6 1948-06-01 4.8

What do I do for making the above picture?


回答1:


Okay, here's a shot.

I slightly rewrote your pres data to fit a tidyverse style, and I created some random unemp data, since you didn't give us any (please do, in the future, as noted in the comments). I got HEX codes from here, which appear to match the ones you show.

Also, note that I'm using scales::label_percent(), which is from the newest scales1.3 release, so you may have to update your scales. Likewise, I don't know what scale your percentage data is on, and you may have to change the scale parameter to label_percent().

With that said, here goes:

library(glue)
library(lubridate)
library(tidyverse)

name <- c("Truman", "Eisenhower", "Kennedy", "Johnson", "Nixon",
          "Ford", "Carter", "Reagan", "Bush I", "Clinton", "Bush II",
          "Obama")
start <- as_date(c("1948-01-01", "1953-01-20", "1961-01-20", "1963-11-22",
                   "1969-01-20", "1974-08-09", "1977-01-20", "1981-01-20",
                   "1989-01-20", "1993-01-20", "2001-01-20", "2009-01-20"))
end <- c(start[-1], as_date("2016-10-01"))
party <- c("D", "R", "D", "D", "R", "R", "D", "R", "R", "D", "R", "D")
pres <- tibble(name, start, end, party)

unemp <- expand_grid(year = 1948:2016, month = 1:12) %>%
  transmute(date = as_date(glue("{year}-{month}-01")),
            unemployment = rnorm(n(), 5, 0.1) + rep(1:3, each = 100, length.out = n()))

min_unemp <- min(unemp$unemployment)
max_unemp <- max(unemp$unemployment)

ggplot(unemp, 
       aes(x = date,
           y = unemployment)) +
  geom_line() +
  geom_vline(data = pres, 
             mapping = aes(xintercept = start), 
             colour = "grey50",
             linetype = "dashed") +
  geom_text(data = pres, 
            mapping = aes(x = start, 
                          y = max_unemp + 0.25,
                          label = name),
            angle = 90,
            vjust = 1) +
  geom_rect(data = pres,
            mapping = aes(xmin = start,
                          xmax = end,
                          ymin = min_unemp,
                          ymax = max_unemp + 0.75,
                          fill = party),
            inherit.aes = FALSE,
            alpha = 0.25) +
  coord_cartesian(expand = FALSE) +
  scale_y_continuous(labels = scales::label_percent(scale = 1)) +
  scale_fill_manual(name = "Party of President", 
                    labels = c("Democratic", "Republican"),
                    values = c("#0015bc", "#ff0000")) +
  labs(x = "Date",
       y = "Unemplyment Rate") +
  theme_minimal() +
  theme(legend.position = "bottom")

Created on 2019-11-30 by the reprex package (v0.3.0)



来源:https://stackoverflow.com/questions/59119580/in-ggplot-how-can-i-plot-data-as-a-line-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!