[英]ggplot2 show different outcome with same code
我用ggplot2制作plot,但是同樣的代碼不同的plot,
圖。1
fed <- tibble(
start = c(ymd("1982-09-27"),ymd("1987-08-11"),ymd("2006-02-01"),
ymd("2014-02-04"),ymd("2018-02-04")),
end = c(ymd("1987-08-10"),ymd("2006-01-31"),ymd("2014-02-03"),
ymd("2018-02-03"),ymd("2020-05-12")),
pd = c("Paul Volker","Alan Greenspan","Ben Bernanke",
"Janet Yellen","Jerome Powell")
)
ggplot(fed_d) +
geom_line(aes(x = date,y = rate/100,linetype = "solid"),
size = 1.1) +
geom_rect(data = fed,
aes(xmin = start,
xmax = end,
ymin = -Inf,ymax = Inf,
fill = pd),alpha = 0.4)
為了制作陰影背景,我制作了一個數據集並使用 geom_rect function
圖2
piie_china <- tibble(
xstart = c(as.Date("2018-01-01"),as.Date("2018-04-02"),as.Date("2018-05-01"),
as.Date("2018-07-01"),as.Date("2018-07-06"),as.Date("2018-08-23"),
as.Date("2018-09-24"),as.Date("2018-11-01"),as.Date("2019-01-01"),
as.Date("2019-06-01"),as.Date("2019-07-01"),as.Date("2019-09-01"),
as.Date("2019-09-17"),as.Date("2019-12-26"),as.Date("2020-02-14")),
xend = c(as.Date("2018-04-01"),as.Date("2018-04-30"),as.Date("2018-06-30"),
as.Date("2018-07-05"),as.Date("2018-08-22"),as.Date("2018-09-23"),
as.Date("2018-10-31"),as.Date("2018-12-31"),as.Date("2019-05-31"),
as.Date("2019-06-30"),as.Date("2019-08-31"),as.Date("2019-09-16"),
as.Date("2019-12-25"),as.Date("2020-02-13"),as.Date("2020-04-30")),
ystart = c(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),
yend = c(8.0,8.4,8.3,7.2,10.1,14.4,18.3,18.2,16.5,
20.7,20.7,21.8,21.1,20.9,20.3),
pd_china = c("CA","CB","CC","CD","CE","CF","CG",
"CH","CI","CJ","CK","CL","CM","CN","CO")
)
ggplot(d_tar) +
geom_rect(data = piie_china,
aes(xmin = xstart,
xmax = xend,
ymin = -Inf,
ymax = Inf,
fill = pd_china)) +
geom_line(data = d_tar,
aes(x = date,y = china),
size = 1) +
geom_line(data = d_tar,
aes(x = date,y = us),
size = 1)
fig2我還使用 geom_rect 來制作陰影背景,我還制作了一個數據集,就像在 fig1 中使用的那樣
為什么相同的代碼和相同的數據集結構會產生不同的結果? 在圖2中,某些日期有空白白線,但在圖2中,陰影背景是連續的
實際上,它遠非“相同的代碼和相同的結構”!
首先,請記住 ggplot 逐層工作。 如果您先指定 geom rect,然后指定 geom_line,則它與 geom_line first 和 geom_rect 不同。
這將導致 geom_line 被繪制在 geom_rect 前面:
ggplot(fed_d) +
geom_rect(data = fed,
aes(xmin = start,
xmax = end,
ymin = -Inf,
ymax = Inf,
fill = pd)) +
geom_line(aes(x = date,
y = rate/100,
linetype = "solid"),
size = 1.1)
這將導致 geom_line 被繪制在 geom_rect 后面:
ggplot(fed_d) +
geom_line(aes(x = date,
y = rate/100,
linetype = "solid"),
size = 1.1) +
geom_rect(data = fed,
aes(xmin = start,
xmax = end,
ymin = -Inf,
ymax = Inf,
fill = pd))
然后,如果我理解得很好,你會想要一個像Fig.1那樣的“淺色”背景。 這由 geom_rect 中傳遞的“alpha = 0.4”參數指定。 您應該在圖 2 中再次編寫它。
最后,關於圖 2中的“白線”:這只是由於您的小標題的結構。 在圖 1中,您看不到矩形之間的任何空白,因為范圍(1982 年到 2020 年)比圖 2中(2018 年到 2020 年)更寬。 即使存在這些空白,圖 1 上的一天也比圖2中的要薄得多。
==> 為了解決這個問題,我只是更改了 xstart,使其與上一個 xend 開始的日期相同,因此它們重疊。
在您的 piie_china 中,不要將一個匯率列用於中國,一個用於我們,而是將它們組合成一個,並在您指定國家/地區的位置創建一個新列。 它允許您使用此新列將您的國家與同一 geom_line 中的線型分開。 這樣更清楚:)
這是我的代碼:
(我不得不隨機生成你的 fed_d 和 d_tar,下次考慮通過提供這些表和一些虛擬表來提供一個可重復的示例)
#############
### Fig.1 ###
#############
fed <- tibble(
start = c(ymd("1982-09-27"),ymd("1987-08-11"),ymd("2006-02-01"),
ymd("2014-02-04"),ymd("2018-02-04")),
end = c(ymd("1987-08-10"),ymd("2006-01-31"),ymd("2014-02-03"),
ymd("2018-02-03"),ymd("2020-05-12")),
pd = c("Paul Volker","Alan Greenspan","Ben Bernanke",
"Janet Yellen","Jerome Powell")
)
### Randomly generate dates and rate
set.seed(1984)
date = sample(seq(as.Date('1982-09-27'), as.Date('2020-05-12'), by = "month"), 50, replace = TRUE)
rate = sample(seq(25,75,by = 1),50,replace = TRUE)
### Store them in tibble
fed_d = tibble(date = date,
rate = rate)
### Draw plot
ggplot(fed_d) +
geom_rect(data = fed,
aes(xmin = start, xmax = end,
ymin = -Inf,ymax = Inf,
fill = pd),
alpha = 0.4) + # outsite aes() because it is not supposed to change
geom_line(aes(x = date,y = rate/100, linetype = "solid"),
size = 1.1) +
scale_y_continuous(limits = c(0,1))
#############
### Fig.2 ###
#############
piie_china <- tibble(
xstart = c(as.Date("2018-01-01"),as.Date("2018-04-01"),as.Date("2018-04-30"),
as.Date("2018-07-01"),as.Date("2018-07-06"),as.Date("2018-08-23"),
as.Date("2018-09-24"),as.Date("2018-11-01"),as.Date("2019-01-01"),
as.Date("2019-06-01"),as.Date("2019-07-01"),as.Date("2019-09-01"),
as.Date("2019-09-17"),as.Date("2019-12-26"),as.Date("2020-02-14")),
xend = c(as.Date("2018-04-01"),as.Date("2018-04-30"),as.Date("2018-07-01"),
as.Date("2018-07-06"),as.Date("2018-08-23"),as.Date("2018-09-24"),
as.Date("2018-11-01"),as.Date("2019-01-01"),as.Date("2019-06-01"),
as.Date("2019-07-01"),as.Date("2019-09-01"),as.Date("2019-09-17"),
as.Date("2019-12-26"),as.Date("2020-02-14"),as.Date("2020-04-30")),
pd_china = c("CA","CB","CC","CD","CE","CF","CG",
"CH","CI","CJ","CK","CL","CM","CN","CO")
)
### Randomly generate dates and rate
set.seed(1984)
date = sample(seq(as.Date('2018-01-01'), as.Date('2020-04-30'), by = "month"), 50, replace = TRUE)
rate = sample(seq(25,75,by = 1),50,replace = TRUE)
d_tar = tibble(date = date,
rate = rate,
country = rep(c("china","us"), 25)) # Create a new column with country
ggplot() +
geom_rect(data = piie_china,
aes(xmin = xstart,
xmax = xend,
ymin = -Inf,
ymax = Inf,
fill = pd_china),
alpha = 0.4) + # outsite aes() because it is not supposed to change
geom_line(data = d_tar,
aes(x = date, y = rate, linetype = country),
size = 1) + # outsite aes() because it is not supposed to change
scale_y_continuous(limits = c(0,100))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.