前言
面积图是在折线图的基础上形成的,它将线段与坐标轴之间的区域用颜色或条纹来填充,以凸显数据的变换趋势。
以 x
轴作为绘制方向,可以使用 geom_ribbon()
函数来绘制一个指定 y
轴区域(即设置 ymin
和 ymax
参数)的图形
使用 geom_area()
绘制线条与 x
轴之间的范围(即 ymin=0
, ymax=y
)
如果以 y
轴作为绘制方向,那么相应的范围参数则变为 xmin
和 xmax
示例
单数据面积图
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)
多数据堆积和百分比面积图
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)
绘制两条曲线之间的区域面积
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
但有时候,我们可能想要区分两条曲线大小关系,体现在图形上就是,曲线 1
大于曲线二的区域设置为一种颜色,曲线 1
小于曲线 2
的区域设置为另一种不同的颜色
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"))
但是,从图上我们看到,不同的区域之间会有粘连,那怎么去除这些粘连呢?
我的解决办法是,将两种区域分开绘制,在绘制某一种区域时,将相反区域的值都设置为 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)
好了,问题解决。