在 ggplot 中,如何绘制像这张图这样的时间序列数据?
In ggplot, how can I plot time series data like this picture?
我正在尝试制作如下图所示的图表。
这也是我在代码中使用数据所做的。
unemp <- read.csv("unemployment.csv", stringsAsFactors = FALSE)
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")
而且,这是来自 "unemp"
的已用数据示例
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
7 1948-07-01 0.036
8 1948-08-01 0.039
9 1948-09-01 0.038
10 1948-10-01 0.037
但我可以得到这张照片。依旧是下图
而且,我还添加了 dput 函数制作的整个结构:
structure(list(date = c("1948-01-01", "1948-02-01", "1948-03-01", "1948-04-01", "1948-05-01", "1948-06-01", "1948-07-01", "1948-08-01",
"1948-09-01", "1948-10-01", "1948-11-01", "1948-12-01", "1949-01-01",
"1949-02-01", "1949-03-01", "1949-04-01", "1949-05-01", "1949-06-01",
"1949-07-01", "1949-08-01", "1949-09-01", "1949-10-01", "1949-11-01",
"1949-12-01", "1950-01-01", "1950-02-01", "1950-03-01", "1950-04-01",
"1950-05-01", "1950-06-01", "1950-07-01", "1950-08-01", "1950-09-01",
"1950-10-01", "1950-11-01", "1950-12-01", "1951-01-01", "1951-02-01",
"1951-03-01", "1951-04-01", "1951-05-01", "1951-06-01", "1951-07-01",
"1951-08-01", "1951-09-01", "1951-10-01", "1951-11-01", "1951-12-01",
"1952-01-01", "1952-02-01", "1952-03-01", "1952-04-01", "1952-05-01",
"1952-06-01", "1952-07-01", "1952-08-01", "1952-09-01", "1952-10-01",
"1952-11-01", "1952-12-01", "1953-01-01", "1953-02-01", "1953-03-01",
"1953-04-01", "1953-05-01", "1953-06-01", "1953-07-01", "1953-08-01",
"1953-09-01", "1953-10-01", "1953-11-01", "1953-12-01", "1954-01-01",
"1954-02-01", "1954-03-01", "1954-04-01", "1954-05-01", "1954-06-01",
"1954-07-01", "1954-08-01", "1954-09-01", "1954-10-01", "1954-11-01",
"1954-12-01", "1955-01-01", "1955-02-01", "1955-03-01", "1955-04-01",
"1955-05-01", "1955-06-01", "1955-07-01", "1955-08-01", "1955-09-01",
"1955-10-01", "1955-11-01", "1955-12-01", "1956-01-01", "1956-02-01",
"1956-03-01", "1956-04-01", "1956-05-01", "1956-06-01", "1956-07-01",
"1956-08-01", "1956-09-01", "1956-10-01", "1956-11-01", "1956-12-01",
"1957-01-01", "1957-02-01", "1957-03-01", "1957-04-01", "1957-05-01",
"1957-06-01", "1957-07-01", "1957-08-01", "1957-09-01", "1957-10-01",
"1957-11-01", "1957-12-01", "1958-01-01", "1958-02-01", "1958-03-01",
"1958-04-01", "1958-05-01", "1958-06-01", "1958-07-01", "1958-08-01",
"1958-09-01", "1958-10-01", "1958-11-01", "1958-12-01", "1959-01-01",
"1959-02-01", "1959-03-01", "1959-04-01", "1959-05-01", "1959-06-01",
"1959-07-01", "1959-08-01", "1959-09-01", "1959-10-01", "1959-11-01",
"1959-12-01", "1960-01-01", "1960-02-01", "1960-03-01", "1960-04-01",
"1960-05-01", "1960-06-01", "1960-07-01", "1960-08-01", "1960-09-01",
"1960-10-01", "1960-11-01", "1960-12-01", "1961-01-01", "1961-02-01",
"1961-03-01", "1961-04-01", "1961-05-01", "1961-06-01", "1961-07-01",
"1961-08-01", "1961-09-01", "1961-10-01", "1961-11-01", "1961-12-01",
"1962-01-01", "1962-02-01", "1962-03-01", "1962-04-01", "1962-05-01",
"1962-06-01", "1962-07-01", "1962-08-01", "1962-09-01", "1962-10-01",
"1962-11-01", "1962-12-01", "1963-01-01", "1963-02-01", "1963-03-01",
"1963-04-01", "1963-05-01", "1963-06-01", "1963-07-01", "1963-08-01",
"1963-09-01", "1963-10-01", "1963-11-01", "1963-12-01", "1964-01-01",
"1964-02-01", "1964-03-01", "1964-04-01", "1964-05-01", "1964-06-01",
"1964-07-01", "1964-08-01", "1964-09-01", "1964-10-01", "1964-11-01",
"1964-12-01", "1965-01-01", "1965-02-01", "1965-03-01", "1965-04-01",
"1965-05-01", "1965-06-01", "1965-07-01", "1965-08-01", "1965-09-01",
"1965-10-01", "1965-11-01", "1965-12-01", "1966-01-01", "1966-02-01",
"1966-03-01", "1966-04-01", "1966-05-01", "1966-06-01", "1966-07-01",
"1966-08-01", "1966-09-01", "1966-10-01", "1966-11-01", "1966-12-01",
"1967-01-01", "1967-02-01", "1967-03-01", "1967-04-01", "1967-05-01",
"1967-06-01", "1967-07-01", "1967-08-01", "1967-09-01", "1967-10-01",
"1967-11-01", "1967-12-01", "1968-01-01", "1968-02-01", "1968-03-01",
"1968-04-01", "1968-05-01", "1968-06-01", "1968-07-01", "1968-08-01",
"1968-09-01", "1968-10-01", "1968-11-01", "1968-12-01", "1969-01-01",
"1969-02-01", "1969-03-01", "1969-04-01", "1969-05-01", "1969-06-01",
"1969-07-01", "1969-08-01", "1969-09-01", "1969-10-01", "1969-11-01",
"1969-12-01", "1970-01-01", "1970-02-01", "1970-03-01", "1970-04-01",
"1970-05-01", "1970-06-01", "1970-07-01", "1970-08-01", "1970-09-01",
"1970-10-01", "1970-11-01", "1970-12-01", "1971-01-01", "1971-02-01",
"1971-03-01", "1971-04-01", "1971-05-01", "1971-06-01", "1971-07-01",
"1971-08-01", "1971-09-01", "1971-10-01", "1971-11-01", "1971-12-01",
"1972-01-01", "1972-02-01", "1972-03-01", "1972-04-01", "1972-05-01",
"1972-06-01", "1972-07-01", "1972-08-01", "1972-09-01", "1972-10-01",
"1972-11-01", "1972-12-01", "1973-01-01", "1973-02-01", "1973-03-01",
"1973-04-01", "1973-05-01", "1973-06-01", "1973-07-01", "1973-08-01",
"1973-09-01", "1973-10-01", "1973-11-01", "1973-12-01", "1974-01-01",
"1974-02-01", "1974-03-01", "1974-04-01", "1974-05-01", "1974-06-01",
"1974-07-01", "1974-08-01", "1974-09-01", "1974-10-01", "1974-11-01",
"1974-12-01", "1975-01-01", "1975-02-01", "1975-03-01", "1975-04-01",
"1975-05-01", "1975-06-01", "1975-07-01", "1975-08-01", "1975-09-01",
"1975-10-01", "1975-11-01", "1975-12-01", "1976-01-01", "1976-02-01",
"1976-03-01", "1976-04-01", "1976-05-01", "1976-06-01", "1976-07-01",
"1976-08-01", "1976-09-01", "1976-10-01", "1976-11-01", "1976-12-01",
"1977-01-01", "1977-02-01", "1977-03-01", "1977-04-01", "1977-05-01",
"1977-06-01", "1977-07-01", "1977-08-01", "1977-09-01", "1977-10-01",
"1977-11-01", "1977-12-01", "1978-01-01", "1978-02-01", "1978-03-01",
"1978-04-01", "1978-05-01", "1978-06-01", "1978-07-01", "1978-08-01",
"1978-09-01", "1978-10-01", "1978-11-01", "1978-12-01", "1979-01-01",
"1979-02-01", "1979-03-01", "1979-04-01", "1979-05-01", "1979-06-01",
"1979-07-01", "1979-08-01", "1979-09-01", "1979-10-01", "1979-11-01",
"1979-12-01", "1980-01-01", "1980-02-01", "1980-03-01", "1980-04-01",
"1980-05-01", "1980-06-01", "1980-07-01", "1980-08-01", "1980-09-01",
"1980-10-01", "1980-11-01", "1980-12-01", "1981-01-01", "1981-02-01",
"1981-03-01", "1981-04-01", "1981-05-01", "1981-06-01", "1981-07-01",
"1981-08-01", "1981-09-01", "1981-10-01", "1981-11-01", "1981-12-01",
"1982-01-01", "1982-02-01", "1982-03-01", "1982-04-01", "1982-05-01",
"1982-06-01", "1982-07-01", "1982-08-01", "1982-09-01", "1982-10-01",
"1982-11-01", "1982-12-01", "1983-01-01", "1983-02-01", "1983-03-01",
"1983-04-01", "1983-05-01", "1983-06-01", "1983-07-01", "1983-08-01",
"1983-09-01", "1983-10-01", "1983-11-01", "1983-12-01", "1984-01-01",
"1984-02-01", "1984-03-01", "1984-04-01", "1984-05-01", "1984-06-01",
"1984-07-01", "1984-08-01", "1984-09-01", "1984-10-01", "1984-11-01",
"1984-12-01", "1985-01-01", "1985-02-01", "1985-03-01", "1985-04-01",
"1985-05-01", "1985-06-01", "1985-07-01", "1985-08-01", "1985-09-01",
"1985-10-01", "1985-11-01", "1985-12-01", "1986-01-01", "1986-02-01",
"1986-03-01", "1986-04-01", "1986-05-01", "1986-06-01", "1986-07-01",
"1986-08-01", "1986-09-01", "1986-10-01", "1986-11-01", "1986-12-01",
"1987-01-01", "1987-02-01", "1987-03-01", "1987-04-01", "1987-05-01",
"1987-06-01", "1987-07-01", "1987-08-01", "1987-09-01", "1987-10-01",
"1987-11-01", "1987-12-01", "1988-01-01", "1988-02-01", "1988-03-01",
"1988-04-01", "1988-05-01", "1988-06-01", "1988-07-01", "1988-08-01",
"1988-09-01", "1988-10-01", "1988-11-01", "1988-12-01", "1989-01-01",
"1989-02-01", "1989-03-01", "1989-04-01", "1989-05-01", "1989-06-01",
"1989-07-01", "1989-08-01", "1989-09-01", "1989-10-01", "1989-11-01",
"1989-12-01", "1990-01-01", "1990-02-01", "1990-03-01", "1990-04-01",
"1990-05-01", "1990-06-01", "1990-07-01", "1990-08-01", "1990-09-01",
"1990-10-01", "1990-11-01", "1990-12-01", "1991-01-01", "1991-02-01",
"1991-03-01", "1991-04-01", "1991-05-01", "1991-06-01", "1991-07-01",
"1991-08-01", "1991-09-01", "1991-10-01", "1991-11-01", "1991-12-01",
"1992-01-01", "1992-02-01", "1992-03-01", "1992-04-01", "1992-05-01",
"1992-06-01", "1992-07-01", "1992-08-01", "1992-09-01", "1992-10-01",
"1992-11-01", "1992-12-01", "1993-01-01", "1993-02-01", "1993-03-01",
"1993-04-01", "1993-05-01", "1993-06-01", "1993-07-01", "1993-08-01",
"1993-09-01", "1993-10-01", "1993-11-01", "1993-12-01", "1994-01-01",
"1994-02-01", "1994-03-01", "1994-04-01", "1994-05-01", "1994-06-01",
"1994-07-01", "1994-08-01", "1994-09-01", "1994-10-01", "1994-11-01",
"1994-12-01", "1995-01-01", "1995-02-01", "1995-03-01", "1995-04-01",
"1995-05-01", "1995-06-01", "1995-07-01", "1995-08-01", "1995-09-01",
"1995-10-01", "1995-11-01", "1995-12-01", "1996-01-01", "1996-02-01",
"1996-03-01", "1996-04-01", "1996-05-01", "1996-06-01", "1996-07-01",
"1996-08-01", "1996-09-01", "1996-10-01", "1996-11-01", "1996-12-01",
"1997-01-01", "1997-02-01", "1997-03-01", "1997-04-01", "1997-05-01",
"1997-06-01", "1997-07-01", "1997-08-01", "1997-09-01", "1997-10-01",
"1997-11-01", "1997-12-01", "1998-01-01", "1998-02-01", "1998-03-01",
"1998-04-01", "1998-05-01", "1998-06-01", "1998-07-01", "1998-08-01",
"1998-09-01", "1998-10-01", "1998-11-01", "1998-12-01", "1999-01-01",
"1999-02-01", "1999-03-01", "1999-04-01", "1999-05-01", "1999-06-01",
"1999-07-01", "1999-08-01", "1999-09-01", "1999-10-01", "1999-11-01",
"1999-12-01", "2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
"2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01", "2000-09-01",
"2000-10-01", "2000-11-01", "2000-12-01", "2001-01-01", "2001-02-01",
"2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01", "2001-07-01",
"2001-08-01", "2001-09-01", "2001-10-01", "2001-11-01", "2001-12-01",
"2002-01-01", "2002-02-01", "2002-03-01", "2002-04-01", "2002-05-01",
"2002-06-01", "2002-07-01", "2002-08-01", "2002-09-01", "2002-10-01",
"2002-11-01", "2002-12-01", "2003-01-01", "2003-02-01", "2003-03-01",
"2003-04-01", "2003-05-01", "2003-06-01", "2003-07-01", "2003-08-01",
"2003-09-01", "2003-10-01", "2003-11-01", "2003-12-01", "2004-01-01",
"2004-02-01", "2004-03-01", "2004-04-01", "2004-05-01", "2004-06-01",
"2004-07-01", "2004-08-01", "2004-09-01", "2004-10-01", "2004-11-01",
"2004-12-01", "2005-01-01", "2005-02-01", "2005-03-01", "2005-04-01",
"2005-05-01", "2005-06-01", "2005-07-01", "2005-08-01", "2005-09-01",
"2005-10-01", "2005-11-01", "2005-12-01", "2006-01-01", "2006-02-01",
"2006-03-01", "2006-04-01", "2006-05-01", "2006-06-01", "2006-07-01",
"2006-08-01", "2006-09-01", "2006-10-01", "2006-11-01", "2006-12-01",
"2007-01-01", "2007-02-01", "2007-03-01", "2007-04-01", "2007-05-01",
"2007-06-01", "2007-07-01", "2007-08-01", "2007-09-01", "2007-10-01",
"2007-11-01", "2007-12-01", "2008-01-01", "2008-02-01", "2008-03-01",
"2008-04-01", "2008-05-01", "2008-06-01", "2008-07-01", "2008-08-01",
"2008-09-01", "2008-10-01", "2008-11-01", "2008-12-01", "2009-01-01",
"2009-02-01", "2009-03-01", "2009-04-01", "2009-05-01", "2009-06-01",
"2009-07-01", "2009-08-01", "2009-09-01", "2009-10-01", "2009-11-01",
"2009-12-01", "2010-01-01", "2010-02-01", "2010-03-01", "2010-04-01",
"2010-05-01", "2010-06-01", "2010-07-01", "2010-08-01", "2010-09-01",
"2010-10-01", "2010-11-01", "2010-12-01", "2011-01-01", "2011-02-01",
"2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01",
"2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01", "2011-12-01",
"2012-01-01", "2012-02-01", "2012-03-01", "2012-04-01", "2012-05-01",
"2012-06-01", "2012-07-01", "2012-08-01", "2012-09-01", "2012-10-01",
"2012-11-01", "2012-12-01", "2013-01-01", "2013-02-01", "2013-03-01",
"2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01",
"2013-09-01", "2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01",
"2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01",
"2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01",
"2014-12-01", "2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01",
"2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01",
"2015-10-01", "2015-11-01", "2015-12-01", "2016-01-01", "2016-02-01",
"2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01",
"2016-08-01", "2016-09-01", "2016-10-01"), unemp = c(0.034, 0.038,
0.04, 0.039, 0.035, 0.036, 0.036, 0.039, 0.038, 0.037, 0.038,
0.04, 0.043, 0.047, 0.05, 0.053, 0.061, 0.062, 0.067, 0.068,
0.066, 0.079, 0.064, 0.066, 0.065, 0.064, 0.063, 0.058, 0.055,
0.054, 0.05, 0.045, 0.044, 0.042, 0.042, 0.043, 0.037, 0.034,
0.034, 0.031, 0.03, 0.032, 0.031, 0.031, 0.033, 0.035, 0.035,
0.031, 0.032, 0.031, 0.029, 0.029, 0.03, 0.03, 0.032, 0.034,
0.031, 0.03, 0.028, 0.027, 0.029, 0.026, 0.026, 0.027, 0.025,
0.025, 0.026, 0.027, 0.029, 0.031, 0.035, 0.045, 0.049, 0.052,
0.057, 0.059, 0.059, 0.056, 0.058, 0.06, 0.061, 0.057, 0.053,
0.05, 0.049, 0.047, 0.046, 0.047, 0.043, 0.042, 0.04, 0.042,
0.041, 0.043, 0.042, 0.042, 0.04, 0.039, 0.042, 0.04, 0.043,
0.043, 0.044, 0.041, 0.039, 0.039, 0.043, 0.042, 0.042, 0.039,
0.037, 0.039, 0.041, 0.043, 0.042, 0.041, 0.044, 0.045, 0.051,
0.052, 0.058, 0.064, 0.067, 0.074, 0.074, 0.073, 0.075, 0.074,
0.071, 0.067, 0.062, 0.062, 0.06, 0.059, 0.056, 0.052, 0.051,
0.05, 0.051, 0.052, 0.055, 0.057, 0.058, 0.053, 0.052, 0.048,
0.054, 0.052, 0.051, 0.054, 0.055, 0.056, 0.055, 0.061, 0.061,
0.066, 0.066, 0.069, 0.069, 0.07, 0.071, 0.069, 0.07, 0.066,
0.067, 0.065, 0.061, 0.06, 0.058, 0.055, 0.056, 0.056, 0.055,
0.055, 0.054, 0.057, 0.056, 0.054, 0.057, 0.055, 0.057, 0.059,
0.057, 0.057, 0.059, 0.056, 0.056, 0.054, 0.055, 0.055, 0.057,
0.055, 0.056, 0.054, 0.054, 0.053, 0.051, 0.052, 0.049, 0.05,
0.051, 0.051, 0.048, 0.05, 0.049, 0.051, 0.047, 0.048, 0.046,
0.046, 0.044, 0.044, 0.043, 0.042, 0.041, 0.04, 0.04, 0.038,
0.038, 0.038, 0.039, 0.038, 0.038, 0.038, 0.037, 0.037, 0.036,
0.038, 0.039, 0.038, 0.038, 0.038, 0.038, 0.039, 0.038, 0.038,
0.038, 0.04, 0.039, 0.038, 0.037, 0.038, 0.037, 0.035, 0.035,
0.037, 0.037, 0.035, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034,
0.034, 0.034, 0.034, 0.035, 0.035, 0.035, 0.037, 0.037, 0.035,
0.035, 0.039, 0.042, 0.044, 0.046, 0.048, 0.049, 0.05, 0.051,
0.054, 0.055, 0.059, 0.061, 0.059, 0.059, 0.06, 0.059, 0.059,
0.059, 0.06, 0.061, 0.06, 0.058, 0.06, 0.06, 0.058, 0.057, 0.058,
0.057, 0.057, 0.057, 0.056, 0.056, 0.055, 0.056, 0.053, 0.052,
0.049, 0.05, 0.049, 0.05, 0.049, 0.049, 0.048, 0.048, 0.048,
0.046, 0.048, 0.049, 0.051, 0.052, 0.051, 0.051, 0.051, 0.054,
0.055, 0.055, 0.059, 0.06, 0.066, 0.072, 0.081, 0.081, 0.086,
0.088, 0.09, 0.088, 0.086, 0.084, 0.084, 0.084, 0.083, 0.082,
0.079, 0.077, 0.076, 0.077, 0.074, 0.076, 0.078, 0.078, 0.076,
0.077, 0.078, 0.078, 0.075, 0.076, 0.074, 0.072, 0.07, 0.072,
0.069, 0.07, 0.068, 0.068, 0.068, 0.064, 0.064, 0.063, 0.063,
0.061, 0.06, 0.059, 0.062, 0.059, 0.06, 0.058, 0.059, 0.06, 0.059,
0.059, 0.058, 0.058, 0.056, 0.057, 0.057, 0.06, 0.059, 0.06,
0.059, 0.06, 0.063, 0.063, 0.063, 0.069, 0.075, 0.076, 0.078,
0.077, 0.075, 0.075, 0.075, 0.072, 0.075, 0.074, 0.074, 0.072,
0.075, 0.075, 0.072, 0.074, 0.076, 0.079, 0.083, 0.085, 0.086,
0.089, 0.09, 0.093, 0.094, 0.096, 0.098, 0.098, 0.101, 0.104,
0.108, 0.108, 0.104, 0.104, 0.103, 0.102, 0.101, 0.101, 0.094,
0.095, 0.092, 0.088, 0.085, 0.083, 0.08, 0.078, 0.078, 0.077,
0.074, 0.072, 0.075, 0.075, 0.073, 0.074, 0.072, 0.073, 0.073,
0.072, 0.072, 0.073, 0.072, 0.074, 0.074, 0.071, 0.071, 0.071,
0.07, 0.07, 0.067, 0.072, 0.072, 0.071, 0.072, 0.072, 0.07, 0.069,
0.07, 0.07, 0.069, 0.066, 0.066, 0.066, 0.066, 0.063, 0.063,
0.062, 0.061, 0.06, 0.059, 0.06, 0.058, 0.057, 0.057, 0.057,
0.057, 0.054, 0.056, 0.054, 0.054, 0.056, 0.054, 0.054, 0.053,
0.053, 0.054, 0.052, 0.05, 0.052, 0.052, 0.053, 0.052, 0.052,
0.053, 0.053, 0.054, 0.054, 0.054, 0.053, 0.052, 0.054, 0.054,
0.052, 0.055, 0.057, 0.059, 0.059, 0.062, 0.063, 0.064, 0.066,
0.068, 0.067, 0.069, 0.069, 0.068, 0.069, 0.069, 0.07, 0.07,
0.073, 0.073, 0.074, 0.074, 0.074, 0.076, 0.078, 0.077, 0.076,
0.076, 0.073, 0.074, 0.074, 0.073, 0.071, 0.07, 0.071, 0.071,
0.07, 0.069, 0.068, 0.067, 0.068, 0.066, 0.065, 0.066, 0.066,
0.065, 0.064, 0.061, 0.061, 0.061, 0.06, 0.059, 0.058, 0.056,
0.055, 0.056, 0.054, 0.054, 0.058, 0.056, 0.056, 0.057, 0.057,
0.056, 0.055, 0.056, 0.056, 0.056, 0.055, 0.055, 0.056, 0.056,
0.053, 0.055, 0.051, 0.052, 0.052, 0.054, 0.054, 0.053, 0.052,
0.052, 0.051, 0.049, 0.05, 0.049, 0.048, 0.049, 0.047, 0.046,
0.047, 0.046, 0.046, 0.047, 0.043, 0.044, 0.045, 0.045, 0.045,
0.046, 0.045, 0.044, 0.044, 0.043, 0.044, 0.042, 0.043, 0.042,
0.043, 0.043, 0.042, 0.042, 0.041, 0.041, 0.04, 0.04, 0.041,
0.04, 0.038, 0.04, 0.04, 0.04, 0.041, 0.039, 0.039, 0.039, 0.039,
0.042, 0.042, 0.043, 0.044, 0.043, 0.045, 0.046, 0.049, 0.05,
0.053, 0.055, 0.057, 0.057, 0.057, 0.057, 0.059, 0.058, 0.058,
0.058, 0.057, 0.057, 0.057, 0.059, 0.06, 0.058, 0.059, 0.059,
0.06, 0.061, 0.063, 0.062, 0.061, 0.061, 0.06, 0.058, 0.057,
0.057, 0.056, 0.058, 0.056, 0.056, 0.056, 0.055, 0.054, 0.054,
0.055, 0.054, 0.054, 0.053, 0.054, 0.052, 0.052, 0.051, 0.05,
0.05, 0.049, 0.05, 0.05, 0.05, 0.049, 0.047, 0.048, 0.047, 0.047,
0.046, 0.046, 0.047, 0.047, 0.045, 0.044, 0.045, 0.044, 0.046,
0.045, 0.044, 0.045, 0.044, 0.046, 0.047, 0.046, 0.047, 0.047,
0.047, 0.05, 0.05, 0.049, 0.051, 0.05, 0.054, 0.056, 0.058, 0.061,
0.061, 0.065, 0.068, 0.073, 0.078, 0.083, 0.087, 0.09, 0.094,
0.095, 0.095, 0.096, 0.098, 0.1, 0.099, 0.099, 0.098, 0.098,
0.099, 0.099, 0.096, 0.094, 0.094, 0.095, 0.095, 0.094, 0.098,
0.093, 0.091, 0.09, 0.09, 0.091, 0.09, 0.091, 0.09, 0.09, 0.09,
0.088, 0.086, 0.085, 0.083, 0.083, 0.082, 0.082, 0.082, 0.082,
0.082, 0.081, 0.078, 0.078, 0.077, 0.079, 0.08, 0.077, 0.075,
0.076, 0.075, 0.075, 0.073, 0.073, 0.073, 0.072, 0.069, 0.067,
0.066, 0.067, 0.067, 0.062, 0.062, 0.061, 0.062, 0.062, 0.06,
0.057, 0.058, 0.056, 0.057, 0.055, 0.055, 0.054, 0.055, 0.053,
0.053, 0.051, 0.051, 0.05, 0.05, 0.05, 0.049, 0.049, 0.05, 0.05,
0.047, 0.049, 0.049, 0.049, 0.05, 0.049)), class = "data.frame", row.names = c(NA,
-826L))
如何修改代码使其像上面第一张图一样?
非常感谢,
为了消除评论中的混淆,此问题引用了 。
使用您的 dput
(我已在下面的 reprex 中将其替换为 read.csv
),以下工作。请注意,您可能可以使用 readr::read_csv
一次性获得合适的日期格式,而不是读取然后转换为 tibble
,但我会把它留给您。
library(glue)
library(lubridate)
library(tidyverse)
unemp <- read.csv("unemployment.csv", stringsAsFactors = FALSE) # The following is run with the dput, but I've added this line back in for clarity.
unemp <- tibble(date = as_date(unemp$date), unemp = unemp$unemp)
min_unemp <- min(unemp$unemp)
max_unemp <- max(unemp$unemp)
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)
ggplot(unemp,
aes(x = date,
y = unemp)) +
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 * 1.1,
label = name),
angle = 90,
vjust = 1) +
geom_rect(data = pres,
mapping = aes(xmin = start,
xmax = end,
ymin = min_unemp,
ymax = max_unemp * 1.2,
fill = party),
inherit.aes = FALSE,
alpha = 0.25) +
coord_cartesian(expand = FALSE) +
scale_y_continuous(labels = scales::label_percent()) +
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")
由 reprex package (v0.3.0)
于 2019-12-03 创建
我正在尝试制作如下图所示的图表。
这也是我在代码中使用数据所做的。
unemp <- read.csv("unemployment.csv", stringsAsFactors = FALSE)
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")
而且,这是来自 "unemp"
的已用数据示例 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
7 1948-07-01 0.036
8 1948-08-01 0.039
9 1948-09-01 0.038
10 1948-10-01 0.037
但我可以得到这张照片。依旧是下图
而且,我还添加了 dput 函数制作的整个结构:
structure(list(date = c("1948-01-01", "1948-02-01", "1948-03-01", "1948-04-01", "1948-05-01", "1948-06-01", "1948-07-01", "1948-08-01",
"1948-09-01", "1948-10-01", "1948-11-01", "1948-12-01", "1949-01-01",
"1949-02-01", "1949-03-01", "1949-04-01", "1949-05-01", "1949-06-01",
"1949-07-01", "1949-08-01", "1949-09-01", "1949-10-01", "1949-11-01",
"1949-12-01", "1950-01-01", "1950-02-01", "1950-03-01", "1950-04-01",
"1950-05-01", "1950-06-01", "1950-07-01", "1950-08-01", "1950-09-01",
"1950-10-01", "1950-11-01", "1950-12-01", "1951-01-01", "1951-02-01",
"1951-03-01", "1951-04-01", "1951-05-01", "1951-06-01", "1951-07-01",
"1951-08-01", "1951-09-01", "1951-10-01", "1951-11-01", "1951-12-01",
"1952-01-01", "1952-02-01", "1952-03-01", "1952-04-01", "1952-05-01",
"1952-06-01", "1952-07-01", "1952-08-01", "1952-09-01", "1952-10-01",
"1952-11-01", "1952-12-01", "1953-01-01", "1953-02-01", "1953-03-01",
"1953-04-01", "1953-05-01", "1953-06-01", "1953-07-01", "1953-08-01",
"1953-09-01", "1953-10-01", "1953-11-01", "1953-12-01", "1954-01-01",
"1954-02-01", "1954-03-01", "1954-04-01", "1954-05-01", "1954-06-01",
"1954-07-01", "1954-08-01", "1954-09-01", "1954-10-01", "1954-11-01",
"1954-12-01", "1955-01-01", "1955-02-01", "1955-03-01", "1955-04-01",
"1955-05-01", "1955-06-01", "1955-07-01", "1955-08-01", "1955-09-01",
"1955-10-01", "1955-11-01", "1955-12-01", "1956-01-01", "1956-02-01",
"1956-03-01", "1956-04-01", "1956-05-01", "1956-06-01", "1956-07-01",
"1956-08-01", "1956-09-01", "1956-10-01", "1956-11-01", "1956-12-01",
"1957-01-01", "1957-02-01", "1957-03-01", "1957-04-01", "1957-05-01",
"1957-06-01", "1957-07-01", "1957-08-01", "1957-09-01", "1957-10-01",
"1957-11-01", "1957-12-01", "1958-01-01", "1958-02-01", "1958-03-01",
"1958-04-01", "1958-05-01", "1958-06-01", "1958-07-01", "1958-08-01",
"1958-09-01", "1958-10-01", "1958-11-01", "1958-12-01", "1959-01-01",
"1959-02-01", "1959-03-01", "1959-04-01", "1959-05-01", "1959-06-01",
"1959-07-01", "1959-08-01", "1959-09-01", "1959-10-01", "1959-11-01",
"1959-12-01", "1960-01-01", "1960-02-01", "1960-03-01", "1960-04-01",
"1960-05-01", "1960-06-01", "1960-07-01", "1960-08-01", "1960-09-01",
"1960-10-01", "1960-11-01", "1960-12-01", "1961-01-01", "1961-02-01",
"1961-03-01", "1961-04-01", "1961-05-01", "1961-06-01", "1961-07-01",
"1961-08-01", "1961-09-01", "1961-10-01", "1961-11-01", "1961-12-01",
"1962-01-01", "1962-02-01", "1962-03-01", "1962-04-01", "1962-05-01",
"1962-06-01", "1962-07-01", "1962-08-01", "1962-09-01", "1962-10-01",
"1962-11-01", "1962-12-01", "1963-01-01", "1963-02-01", "1963-03-01",
"1963-04-01", "1963-05-01", "1963-06-01", "1963-07-01", "1963-08-01",
"1963-09-01", "1963-10-01", "1963-11-01", "1963-12-01", "1964-01-01",
"1964-02-01", "1964-03-01", "1964-04-01", "1964-05-01", "1964-06-01",
"1964-07-01", "1964-08-01", "1964-09-01", "1964-10-01", "1964-11-01",
"1964-12-01", "1965-01-01", "1965-02-01", "1965-03-01", "1965-04-01",
"1965-05-01", "1965-06-01", "1965-07-01", "1965-08-01", "1965-09-01",
"1965-10-01", "1965-11-01", "1965-12-01", "1966-01-01", "1966-02-01",
"1966-03-01", "1966-04-01", "1966-05-01", "1966-06-01", "1966-07-01",
"1966-08-01", "1966-09-01", "1966-10-01", "1966-11-01", "1966-12-01",
"1967-01-01", "1967-02-01", "1967-03-01", "1967-04-01", "1967-05-01",
"1967-06-01", "1967-07-01", "1967-08-01", "1967-09-01", "1967-10-01",
"1967-11-01", "1967-12-01", "1968-01-01", "1968-02-01", "1968-03-01",
"1968-04-01", "1968-05-01", "1968-06-01", "1968-07-01", "1968-08-01",
"1968-09-01", "1968-10-01", "1968-11-01", "1968-12-01", "1969-01-01",
"1969-02-01", "1969-03-01", "1969-04-01", "1969-05-01", "1969-06-01",
"1969-07-01", "1969-08-01", "1969-09-01", "1969-10-01", "1969-11-01",
"1969-12-01", "1970-01-01", "1970-02-01", "1970-03-01", "1970-04-01",
"1970-05-01", "1970-06-01", "1970-07-01", "1970-08-01", "1970-09-01",
"1970-10-01", "1970-11-01", "1970-12-01", "1971-01-01", "1971-02-01",
"1971-03-01", "1971-04-01", "1971-05-01", "1971-06-01", "1971-07-01",
"1971-08-01", "1971-09-01", "1971-10-01", "1971-11-01", "1971-12-01",
"1972-01-01", "1972-02-01", "1972-03-01", "1972-04-01", "1972-05-01",
"1972-06-01", "1972-07-01", "1972-08-01", "1972-09-01", "1972-10-01",
"1972-11-01", "1972-12-01", "1973-01-01", "1973-02-01", "1973-03-01",
"1973-04-01", "1973-05-01", "1973-06-01", "1973-07-01", "1973-08-01",
"1973-09-01", "1973-10-01", "1973-11-01", "1973-12-01", "1974-01-01",
"1974-02-01", "1974-03-01", "1974-04-01", "1974-05-01", "1974-06-01",
"1974-07-01", "1974-08-01", "1974-09-01", "1974-10-01", "1974-11-01",
"1974-12-01", "1975-01-01", "1975-02-01", "1975-03-01", "1975-04-01",
"1975-05-01", "1975-06-01", "1975-07-01", "1975-08-01", "1975-09-01",
"1975-10-01", "1975-11-01", "1975-12-01", "1976-01-01", "1976-02-01",
"1976-03-01", "1976-04-01", "1976-05-01", "1976-06-01", "1976-07-01",
"1976-08-01", "1976-09-01", "1976-10-01", "1976-11-01", "1976-12-01",
"1977-01-01", "1977-02-01", "1977-03-01", "1977-04-01", "1977-05-01",
"1977-06-01", "1977-07-01", "1977-08-01", "1977-09-01", "1977-10-01",
"1977-11-01", "1977-12-01", "1978-01-01", "1978-02-01", "1978-03-01",
"1978-04-01", "1978-05-01", "1978-06-01", "1978-07-01", "1978-08-01",
"1978-09-01", "1978-10-01", "1978-11-01", "1978-12-01", "1979-01-01",
"1979-02-01", "1979-03-01", "1979-04-01", "1979-05-01", "1979-06-01",
"1979-07-01", "1979-08-01", "1979-09-01", "1979-10-01", "1979-11-01",
"1979-12-01", "1980-01-01", "1980-02-01", "1980-03-01", "1980-04-01",
"1980-05-01", "1980-06-01", "1980-07-01", "1980-08-01", "1980-09-01",
"1980-10-01", "1980-11-01", "1980-12-01", "1981-01-01", "1981-02-01",
"1981-03-01", "1981-04-01", "1981-05-01", "1981-06-01", "1981-07-01",
"1981-08-01", "1981-09-01", "1981-10-01", "1981-11-01", "1981-12-01",
"1982-01-01", "1982-02-01", "1982-03-01", "1982-04-01", "1982-05-01",
"1982-06-01", "1982-07-01", "1982-08-01", "1982-09-01", "1982-10-01",
"1982-11-01", "1982-12-01", "1983-01-01", "1983-02-01", "1983-03-01",
"1983-04-01", "1983-05-01", "1983-06-01", "1983-07-01", "1983-08-01",
"1983-09-01", "1983-10-01", "1983-11-01", "1983-12-01", "1984-01-01",
"1984-02-01", "1984-03-01", "1984-04-01", "1984-05-01", "1984-06-01",
"1984-07-01", "1984-08-01", "1984-09-01", "1984-10-01", "1984-11-01",
"1984-12-01", "1985-01-01", "1985-02-01", "1985-03-01", "1985-04-01",
"1985-05-01", "1985-06-01", "1985-07-01", "1985-08-01", "1985-09-01",
"1985-10-01", "1985-11-01", "1985-12-01", "1986-01-01", "1986-02-01",
"1986-03-01", "1986-04-01", "1986-05-01", "1986-06-01", "1986-07-01",
"1986-08-01", "1986-09-01", "1986-10-01", "1986-11-01", "1986-12-01",
"1987-01-01", "1987-02-01", "1987-03-01", "1987-04-01", "1987-05-01",
"1987-06-01", "1987-07-01", "1987-08-01", "1987-09-01", "1987-10-01",
"1987-11-01", "1987-12-01", "1988-01-01", "1988-02-01", "1988-03-01",
"1988-04-01", "1988-05-01", "1988-06-01", "1988-07-01", "1988-08-01",
"1988-09-01", "1988-10-01", "1988-11-01", "1988-12-01", "1989-01-01",
"1989-02-01", "1989-03-01", "1989-04-01", "1989-05-01", "1989-06-01",
"1989-07-01", "1989-08-01", "1989-09-01", "1989-10-01", "1989-11-01",
"1989-12-01", "1990-01-01", "1990-02-01", "1990-03-01", "1990-04-01",
"1990-05-01", "1990-06-01", "1990-07-01", "1990-08-01", "1990-09-01",
"1990-10-01", "1990-11-01", "1990-12-01", "1991-01-01", "1991-02-01",
"1991-03-01", "1991-04-01", "1991-05-01", "1991-06-01", "1991-07-01",
"1991-08-01", "1991-09-01", "1991-10-01", "1991-11-01", "1991-12-01",
"1992-01-01", "1992-02-01", "1992-03-01", "1992-04-01", "1992-05-01",
"1992-06-01", "1992-07-01", "1992-08-01", "1992-09-01", "1992-10-01",
"1992-11-01", "1992-12-01", "1993-01-01", "1993-02-01", "1993-03-01",
"1993-04-01", "1993-05-01", "1993-06-01", "1993-07-01", "1993-08-01",
"1993-09-01", "1993-10-01", "1993-11-01", "1993-12-01", "1994-01-01",
"1994-02-01", "1994-03-01", "1994-04-01", "1994-05-01", "1994-06-01",
"1994-07-01", "1994-08-01", "1994-09-01", "1994-10-01", "1994-11-01",
"1994-12-01", "1995-01-01", "1995-02-01", "1995-03-01", "1995-04-01",
"1995-05-01", "1995-06-01", "1995-07-01", "1995-08-01", "1995-09-01",
"1995-10-01", "1995-11-01", "1995-12-01", "1996-01-01", "1996-02-01",
"1996-03-01", "1996-04-01", "1996-05-01", "1996-06-01", "1996-07-01",
"1996-08-01", "1996-09-01", "1996-10-01", "1996-11-01", "1996-12-01",
"1997-01-01", "1997-02-01", "1997-03-01", "1997-04-01", "1997-05-01",
"1997-06-01", "1997-07-01", "1997-08-01", "1997-09-01", "1997-10-01",
"1997-11-01", "1997-12-01", "1998-01-01", "1998-02-01", "1998-03-01",
"1998-04-01", "1998-05-01", "1998-06-01", "1998-07-01", "1998-08-01",
"1998-09-01", "1998-10-01", "1998-11-01", "1998-12-01", "1999-01-01",
"1999-02-01", "1999-03-01", "1999-04-01", "1999-05-01", "1999-06-01",
"1999-07-01", "1999-08-01", "1999-09-01", "1999-10-01", "1999-11-01",
"1999-12-01", "2000-01-01", "2000-02-01", "2000-03-01", "2000-04-01",
"2000-05-01", "2000-06-01", "2000-07-01", "2000-08-01", "2000-09-01",
"2000-10-01", "2000-11-01", "2000-12-01", "2001-01-01", "2001-02-01",
"2001-03-01", "2001-04-01", "2001-05-01", "2001-06-01", "2001-07-01",
"2001-08-01", "2001-09-01", "2001-10-01", "2001-11-01", "2001-12-01",
"2002-01-01", "2002-02-01", "2002-03-01", "2002-04-01", "2002-05-01",
"2002-06-01", "2002-07-01", "2002-08-01", "2002-09-01", "2002-10-01",
"2002-11-01", "2002-12-01", "2003-01-01", "2003-02-01", "2003-03-01",
"2003-04-01", "2003-05-01", "2003-06-01", "2003-07-01", "2003-08-01",
"2003-09-01", "2003-10-01", "2003-11-01", "2003-12-01", "2004-01-01",
"2004-02-01", "2004-03-01", "2004-04-01", "2004-05-01", "2004-06-01",
"2004-07-01", "2004-08-01", "2004-09-01", "2004-10-01", "2004-11-01",
"2004-12-01", "2005-01-01", "2005-02-01", "2005-03-01", "2005-04-01",
"2005-05-01", "2005-06-01", "2005-07-01", "2005-08-01", "2005-09-01",
"2005-10-01", "2005-11-01", "2005-12-01", "2006-01-01", "2006-02-01",
"2006-03-01", "2006-04-01", "2006-05-01", "2006-06-01", "2006-07-01",
"2006-08-01", "2006-09-01", "2006-10-01", "2006-11-01", "2006-12-01",
"2007-01-01", "2007-02-01", "2007-03-01", "2007-04-01", "2007-05-01",
"2007-06-01", "2007-07-01", "2007-08-01", "2007-09-01", "2007-10-01",
"2007-11-01", "2007-12-01", "2008-01-01", "2008-02-01", "2008-03-01",
"2008-04-01", "2008-05-01", "2008-06-01", "2008-07-01", "2008-08-01",
"2008-09-01", "2008-10-01", "2008-11-01", "2008-12-01", "2009-01-01",
"2009-02-01", "2009-03-01", "2009-04-01", "2009-05-01", "2009-06-01",
"2009-07-01", "2009-08-01", "2009-09-01", "2009-10-01", "2009-11-01",
"2009-12-01", "2010-01-01", "2010-02-01", "2010-03-01", "2010-04-01",
"2010-05-01", "2010-06-01", "2010-07-01", "2010-08-01", "2010-09-01",
"2010-10-01", "2010-11-01", "2010-12-01", "2011-01-01", "2011-02-01",
"2011-03-01", "2011-04-01", "2011-05-01", "2011-06-01", "2011-07-01",
"2011-08-01", "2011-09-01", "2011-10-01", "2011-11-01", "2011-12-01",
"2012-01-01", "2012-02-01", "2012-03-01", "2012-04-01", "2012-05-01",
"2012-06-01", "2012-07-01", "2012-08-01", "2012-09-01", "2012-10-01",
"2012-11-01", "2012-12-01", "2013-01-01", "2013-02-01", "2013-03-01",
"2013-04-01", "2013-05-01", "2013-06-01", "2013-07-01", "2013-08-01",
"2013-09-01", "2013-10-01", "2013-11-01", "2013-12-01", "2014-01-01",
"2014-02-01", "2014-03-01", "2014-04-01", "2014-05-01", "2014-06-01",
"2014-07-01", "2014-08-01", "2014-09-01", "2014-10-01", "2014-11-01",
"2014-12-01", "2015-01-01", "2015-02-01", "2015-03-01", "2015-04-01",
"2015-05-01", "2015-06-01", "2015-07-01", "2015-08-01", "2015-09-01",
"2015-10-01", "2015-11-01", "2015-12-01", "2016-01-01", "2016-02-01",
"2016-03-01", "2016-04-01", "2016-05-01", "2016-06-01", "2016-07-01",
"2016-08-01", "2016-09-01", "2016-10-01"), unemp = c(0.034, 0.038,
0.04, 0.039, 0.035, 0.036, 0.036, 0.039, 0.038, 0.037, 0.038,
0.04, 0.043, 0.047, 0.05, 0.053, 0.061, 0.062, 0.067, 0.068,
0.066, 0.079, 0.064, 0.066, 0.065, 0.064, 0.063, 0.058, 0.055,
0.054, 0.05, 0.045, 0.044, 0.042, 0.042, 0.043, 0.037, 0.034,
0.034, 0.031, 0.03, 0.032, 0.031, 0.031, 0.033, 0.035, 0.035,
0.031, 0.032, 0.031, 0.029, 0.029, 0.03, 0.03, 0.032, 0.034,
0.031, 0.03, 0.028, 0.027, 0.029, 0.026, 0.026, 0.027, 0.025,
0.025, 0.026, 0.027, 0.029, 0.031, 0.035, 0.045, 0.049, 0.052,
0.057, 0.059, 0.059, 0.056, 0.058, 0.06, 0.061, 0.057, 0.053,
0.05, 0.049, 0.047, 0.046, 0.047, 0.043, 0.042, 0.04, 0.042,
0.041, 0.043, 0.042, 0.042, 0.04, 0.039, 0.042, 0.04, 0.043,
0.043, 0.044, 0.041, 0.039, 0.039, 0.043, 0.042, 0.042, 0.039,
0.037, 0.039, 0.041, 0.043, 0.042, 0.041, 0.044, 0.045, 0.051,
0.052, 0.058, 0.064, 0.067, 0.074, 0.074, 0.073, 0.075, 0.074,
0.071, 0.067, 0.062, 0.062, 0.06, 0.059, 0.056, 0.052, 0.051,
0.05, 0.051, 0.052, 0.055, 0.057, 0.058, 0.053, 0.052, 0.048,
0.054, 0.052, 0.051, 0.054, 0.055, 0.056, 0.055, 0.061, 0.061,
0.066, 0.066, 0.069, 0.069, 0.07, 0.071, 0.069, 0.07, 0.066,
0.067, 0.065, 0.061, 0.06, 0.058, 0.055, 0.056, 0.056, 0.055,
0.055, 0.054, 0.057, 0.056, 0.054, 0.057, 0.055, 0.057, 0.059,
0.057, 0.057, 0.059, 0.056, 0.056, 0.054, 0.055, 0.055, 0.057,
0.055, 0.056, 0.054, 0.054, 0.053, 0.051, 0.052, 0.049, 0.05,
0.051, 0.051, 0.048, 0.05, 0.049, 0.051, 0.047, 0.048, 0.046,
0.046, 0.044, 0.044, 0.043, 0.042, 0.041, 0.04, 0.04, 0.038,
0.038, 0.038, 0.039, 0.038, 0.038, 0.038, 0.037, 0.037, 0.036,
0.038, 0.039, 0.038, 0.038, 0.038, 0.038, 0.039, 0.038, 0.038,
0.038, 0.04, 0.039, 0.038, 0.037, 0.038, 0.037, 0.035, 0.035,
0.037, 0.037, 0.035, 0.034, 0.034, 0.034, 0.034, 0.034, 0.034,
0.034, 0.034, 0.034, 0.035, 0.035, 0.035, 0.037, 0.037, 0.035,
0.035, 0.039, 0.042, 0.044, 0.046, 0.048, 0.049, 0.05, 0.051,
0.054, 0.055, 0.059, 0.061, 0.059, 0.059, 0.06, 0.059, 0.059,
0.059, 0.06, 0.061, 0.06, 0.058, 0.06, 0.06, 0.058, 0.057, 0.058,
0.057, 0.057, 0.057, 0.056, 0.056, 0.055, 0.056, 0.053, 0.052,
0.049, 0.05, 0.049, 0.05, 0.049, 0.049, 0.048, 0.048, 0.048,
0.046, 0.048, 0.049, 0.051, 0.052, 0.051, 0.051, 0.051, 0.054,
0.055, 0.055, 0.059, 0.06, 0.066, 0.072, 0.081, 0.081, 0.086,
0.088, 0.09, 0.088, 0.086, 0.084, 0.084, 0.084, 0.083, 0.082,
0.079, 0.077, 0.076, 0.077, 0.074, 0.076, 0.078, 0.078, 0.076,
0.077, 0.078, 0.078, 0.075, 0.076, 0.074, 0.072, 0.07, 0.072,
0.069, 0.07, 0.068, 0.068, 0.068, 0.064, 0.064, 0.063, 0.063,
0.061, 0.06, 0.059, 0.062, 0.059, 0.06, 0.058, 0.059, 0.06, 0.059,
0.059, 0.058, 0.058, 0.056, 0.057, 0.057, 0.06, 0.059, 0.06,
0.059, 0.06, 0.063, 0.063, 0.063, 0.069, 0.075, 0.076, 0.078,
0.077, 0.075, 0.075, 0.075, 0.072, 0.075, 0.074, 0.074, 0.072,
0.075, 0.075, 0.072, 0.074, 0.076, 0.079, 0.083, 0.085, 0.086,
0.089, 0.09, 0.093, 0.094, 0.096, 0.098, 0.098, 0.101, 0.104,
0.108, 0.108, 0.104, 0.104, 0.103, 0.102, 0.101, 0.101, 0.094,
0.095, 0.092, 0.088, 0.085, 0.083, 0.08, 0.078, 0.078, 0.077,
0.074, 0.072, 0.075, 0.075, 0.073, 0.074, 0.072, 0.073, 0.073,
0.072, 0.072, 0.073, 0.072, 0.074, 0.074, 0.071, 0.071, 0.071,
0.07, 0.07, 0.067, 0.072, 0.072, 0.071, 0.072, 0.072, 0.07, 0.069,
0.07, 0.07, 0.069, 0.066, 0.066, 0.066, 0.066, 0.063, 0.063,
0.062, 0.061, 0.06, 0.059, 0.06, 0.058, 0.057, 0.057, 0.057,
0.057, 0.054, 0.056, 0.054, 0.054, 0.056, 0.054, 0.054, 0.053,
0.053, 0.054, 0.052, 0.05, 0.052, 0.052, 0.053, 0.052, 0.052,
0.053, 0.053, 0.054, 0.054, 0.054, 0.053, 0.052, 0.054, 0.054,
0.052, 0.055, 0.057, 0.059, 0.059, 0.062, 0.063, 0.064, 0.066,
0.068, 0.067, 0.069, 0.069, 0.068, 0.069, 0.069, 0.07, 0.07,
0.073, 0.073, 0.074, 0.074, 0.074, 0.076, 0.078, 0.077, 0.076,
0.076, 0.073, 0.074, 0.074, 0.073, 0.071, 0.07, 0.071, 0.071,
0.07, 0.069, 0.068, 0.067, 0.068, 0.066, 0.065, 0.066, 0.066,
0.065, 0.064, 0.061, 0.061, 0.061, 0.06, 0.059, 0.058, 0.056,
0.055, 0.056, 0.054, 0.054, 0.058, 0.056, 0.056, 0.057, 0.057,
0.056, 0.055, 0.056, 0.056, 0.056, 0.055, 0.055, 0.056, 0.056,
0.053, 0.055, 0.051, 0.052, 0.052, 0.054, 0.054, 0.053, 0.052,
0.052, 0.051, 0.049, 0.05, 0.049, 0.048, 0.049, 0.047, 0.046,
0.047, 0.046, 0.046, 0.047, 0.043, 0.044, 0.045, 0.045, 0.045,
0.046, 0.045, 0.044, 0.044, 0.043, 0.044, 0.042, 0.043, 0.042,
0.043, 0.043, 0.042, 0.042, 0.041, 0.041, 0.04, 0.04, 0.041,
0.04, 0.038, 0.04, 0.04, 0.04, 0.041, 0.039, 0.039, 0.039, 0.039,
0.042, 0.042, 0.043, 0.044, 0.043, 0.045, 0.046, 0.049, 0.05,
0.053, 0.055, 0.057, 0.057, 0.057, 0.057, 0.059, 0.058, 0.058,
0.058, 0.057, 0.057, 0.057, 0.059, 0.06, 0.058, 0.059, 0.059,
0.06, 0.061, 0.063, 0.062, 0.061, 0.061, 0.06, 0.058, 0.057,
0.057, 0.056, 0.058, 0.056, 0.056, 0.056, 0.055, 0.054, 0.054,
0.055, 0.054, 0.054, 0.053, 0.054, 0.052, 0.052, 0.051, 0.05,
0.05, 0.049, 0.05, 0.05, 0.05, 0.049, 0.047, 0.048, 0.047, 0.047,
0.046, 0.046, 0.047, 0.047, 0.045, 0.044, 0.045, 0.044, 0.046,
0.045, 0.044, 0.045, 0.044, 0.046, 0.047, 0.046, 0.047, 0.047,
0.047, 0.05, 0.05, 0.049, 0.051, 0.05, 0.054, 0.056, 0.058, 0.061,
0.061, 0.065, 0.068, 0.073, 0.078, 0.083, 0.087, 0.09, 0.094,
0.095, 0.095, 0.096, 0.098, 0.1, 0.099, 0.099, 0.098, 0.098,
0.099, 0.099, 0.096, 0.094, 0.094, 0.095, 0.095, 0.094, 0.098,
0.093, 0.091, 0.09, 0.09, 0.091, 0.09, 0.091, 0.09, 0.09, 0.09,
0.088, 0.086, 0.085, 0.083, 0.083, 0.082, 0.082, 0.082, 0.082,
0.082, 0.081, 0.078, 0.078, 0.077, 0.079, 0.08, 0.077, 0.075,
0.076, 0.075, 0.075, 0.073, 0.073, 0.073, 0.072, 0.069, 0.067,
0.066, 0.067, 0.067, 0.062, 0.062, 0.061, 0.062, 0.062, 0.06,
0.057, 0.058, 0.056, 0.057, 0.055, 0.055, 0.054, 0.055, 0.053,
0.053, 0.051, 0.051, 0.05, 0.05, 0.05, 0.049, 0.049, 0.05, 0.05,
0.047, 0.049, 0.049, 0.049, 0.05, 0.049)), class = "data.frame", row.names = c(NA,
-826L))
如何修改代码使其像上面第一张图一样? 非常感谢,
为了消除评论中的混淆,此问题引用了
使用您的 dput
(我已在下面的 reprex 中将其替换为 read.csv
),以下工作。请注意,您可能可以使用 readr::read_csv
一次性获得合适的日期格式,而不是读取然后转换为 tibble
,但我会把它留给您。
library(glue)
library(lubridate)
library(tidyverse)
unemp <- read.csv("unemployment.csv", stringsAsFactors = FALSE) # The following is run with the dput, but I've added this line back in for clarity.
unemp <- tibble(date = as_date(unemp$date), unemp = unemp$unemp)
min_unemp <- min(unemp$unemp)
max_unemp <- max(unemp$unemp)
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)
ggplot(unemp,
aes(x = date,
y = unemp)) +
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 * 1.1,
label = name),
angle = 90,
vjust = 1) +
geom_rect(data = pres,
mapping = aes(xmin = start,
xmax = end,
ymin = min_unemp,
ymax = max_unemp * 1.2,
fill = party),
inherit.aes = FALSE,
alpha = 0.25) +
coord_cartesian(expand = FALSE) +
scale_y_continuous(labels = scales::label_percent()) +
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")
由 reprex package (v0.3.0)
于 2019-12-03 创建