前言
面積圖是在折線圖的基礎(chǔ)上形成的,它將線段與坐標(biāo)軸之間的區(qū)域用顏色或條紋來(lái)填充,以凸顯數(shù)據(jù)的變換趨勢(shì)。
以 x 軸作為繪制方向,可以使用 geom_ribbon() 函數(shù)來(lái)繪制一個(gè)指定 y 軸區(qū)域(即設(shè)置 ymin 和 ymax 參數(shù))的圖形
使用 geom_area() 繪制線條與 x 軸之間的范圍(即 ymin=0, ymax=y)
如果以 y 軸作為繪制方向,那么相應(yīng)的范圍參數(shù)則變?yōu)?xmin 和 xmax
示例
單數(shù)據(jù)面積圖
df <- tibble(x = sample(30:50, 20), y = sample(1:20, 20))
p1 <- ggplot(df, aes(x, y)) +
geom_line() +
geom_area(fill = 'blue', alpha = 0.5)
p2 <- ggplot(df, aes(x, y)) +
geom_line() +
geom_ribbon(aes(ymin = y - 1, ymax = y + 1), fill = 'lightgreen', alpha = 0.5)
p3 <- ggplot(df, aes(x, y)) +
geom_line(orientation = 'y') +
geom_area(fill = 'blue', alpha = 0.5, orientation = 'y')
p4 <- ggplot(df, aes(x, y)) +
geom_line(orientation = 'y') +
geom_ribbon(aes(xmin = x - 1, xmax = x + 1), orientation = 'y',
fill = 'lightgreen', alpha = 0.5)
plot_grid(p1, p2, p3, p4)

多數(shù)據(jù)堆積和百分比面積圖
p <- subset(economics_long, variable %in% c("pce", "unemploy")) %>%
ggplot(aes(x = date))
p1 <- p + geom_area(aes(y = value01, fill = variable), alpha = 0.4, position = 'stack')
p2 <- p + geom_area(aes(y = value01, fill = variable), alpha = 0.4, position = 'fill')
plot_grid(p1, p2)

繪制兩條曲線之間的區(qū)域面積
p <- subset(economics_long, variable %in% c("pce", "unemploy")) %>%
select(c(date, variable, value01)) %>%
pivot_wider(names_from = variable, values_from = value01) %>%
ggplot(aes(x = date)) +
geom_ribbon(aes(ymin = if_else(pce > unemploy, unemploy, pce),
ymax = if_else(pce > unemploy, pce, unemploy)),
fill = "#decbe4", colour = "black")
p

但有時(shí)候,我們可能想要區(qū)分兩條曲線大小關(guān)系,體現(xiàn)在圖形上就是,曲線 1 大于曲線二的區(qū)域設(shè)置為一種顏色,曲線 1 小于曲線 2 的區(qū)域設(shè)置為另一種不同的顏色
p + geom_ribbon(aes(ymin = pmin(unemploy, pce),
ymax = pmax(pce, unemploy),
fill = pce > unemploy,
colour = pce > unemploy),
show.legend = FALSE, na.rm = TRUE) +
scale_fill_manual(values = c("#8dd3c7", "#fdb462"))

但是,從圖上我們看到,不同的區(qū)域之間會(huì)有粘連,那怎么去除這些粘連呢?
我的解決辦法是,將兩種區(qū)域分開(kāi)繪制,在繪制某一種區(qū)域時(shí),將相反區(qū)域的值都設(shè)置為 NA
df <- subset(economics_long, variable %in% c("pce", "unemploy")) %>%
select(c(date, variable, value01)) %>%
pivot_wider(names_from = variable, values_from = value01) %>%
mutate(low_min = pmin(pce, unemploy), low_max = pmax(pce, unemploy),
high_min = low_min, high_max = low_max)
df$low_min[df$pce > df$unemploy] = NA
df$low_max[df$pce > df$unemploy] = NA
df$high_min[df$pce <= df$unemploy] = NA
df$high_max[df$pce <= df$unemploy] = NA
ggplot(df, aes(x = date)) +
geom_ribbon(aes(ymin = low_min, ymax = low_max),
fill = "#8dd3c7", alpha = 0.7) +
geom_ribbon(aes(ymin = high_min, ymax = high_max),
fill = "#fdb462", alpha = 0.7) +
geom_line(aes(y = pce), colour = "#fb8072", size = .75) +
geom_line(aes(y = unemploy), colour = "#80b1d3", size = .75)

好了,問(wèn)題解決。