如何使用R ggplot stat_summary绘制中位数和四分位数?

时间:2016-12-10 15:37:16

标签: r ggplot2 statistics

如何将此统计摘要图中的低点和高点更改为25%四分位数和75%四分位数?

ggplot(data = diamonds) + stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median

2 个答案:

答案 0 :(得分:5)

ggplot(data = diamonds) + stat_summary(
  mapping = aes(x = cut, y = depth),
  fun.ymin = function(z) { quantile(z,0.25) },
  fun.ymax = function(z) { quantile(z,0.75) },
  fun.y = median)

答案 1 :(得分:3)

使用geom函数代替stat函数重写G5W的解决方案:

ggplot(data = diamonds) +
  geom_pointrange(mapping = aes(x = cut, y = depth),
                  stat = "summary",
                  fun.ymin = function(z) {quantile(z,0.25)},
                  fun.ymax = function(z) {quantile(z,0.75)},
                  fun.y = median)

enter image description here