用R中的面积图绘制箱线图

时间:2020-03-30 06:18:40

标签: r ggplot2

我想用面积图绘制箱形图,但在绘制箱形图后我不知道如何添加面积图。

这里是我使用的数据:

structure(list(x = structure(c(18314L, 18314L, 18314L, 18314L, 
18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 
18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 18314L, 
18314L, 18314L, 18314L, 18315L, 18315L, 18315L, 18315L, 18315L, 
18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 
18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 18315L, 
18315L, 18315L, 18315L, 18316L, 18316L, 18316L, 18316L, 18316L, 
18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 
18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 18316L, 
18316L, 18316L, 18316L), class = "Date"), y = c(24.2471666666667, 
22.0866666666667, 23.25525, 22.90175, 21.3551666666667, 22.639, 
22.5633333333333, 21.5476666666667, 22.4529166666667, 21.1831666666667, 
21.065, 20.8060833333333, 19.3153333333333, 20.4245833333333, 
20.2316666666667, 18.9655833333333, 20.49575, 19.7771666666667, 
17.6696666666667, 18.76425, 17.9373333333333, 15.7266666666667, 
16.4498333333333, 16.5565, 14.72075, 15.7441666666667, 16.0895, 
15.2769166666667, 16.0465833333333, 15.5703333333333, 15.1585, 
15.193, 14.7855, 15.2318333333333, 14.891, 13.8044166666667, 
14.8228333333333, 14.0265, 13.3326666666667, 14.6345833333333, 
13.9368333333333, 13.25425, 14.57225, 13.8561666666667, 12.739, 
14.4376666666667, 12.5178333333333, 10.4075833333333, 11.7431666666667, 
9.26116666666667, 7.66263636363636, 10.285, 8.23918181818182, 
6.272, 7.88, 7.2076, 5.948875, 5.33, 7.01281818181818, 4.97408333333333, 
6.114, 8.78925, 8.71483333333333, 9.90091666666667, 12.2376666666667, 
13.7695, 14.4801666666667, 16.6445, 19.7036666666667, 19.5710833333333, 
20.68325)), class = "data.frame", row.names = c(NA, -71L))

我尝试过:

box_plot <- ggplot(dataku, aes(x=x,y=y, group = x)) 
box_plot +
  geom_boxplot()

在绘制箱线图后,我尝试使用添加面积图:

box_plot +
  geom_boxplot() +
  geom_area(dataku, mapping = aes(x,y,fill=x))

我要保存的地块:

enter image description here

1 个答案:

答案 0 :(得分:1)

假设面积图反映了最小值和最大值...尝试此操作。基本思想是使用geom_ribbon和stat_summary计算最小值和最大值:

library(ggplot2)

dataku$x <- as.Date(dataku$x)

ggplot(dataku) +
  stat_summary(aes(x, y), fun.min = min, fun.max = max, geom = "ribbon", fill = "orange", alpha = .6) +
  geom_boxplot(aes(x, y, fill = factor(x), group = x), alpha = .4)

reprex package(v0.3.0)于2020-03-30创建