【发布时间】:2021-02-27 05:52:15
【问题描述】:
我正在学习 R 和 ggplot2。根据说明,geom 和 stat 通常可以互换,因为 geom 有一个默认的 stat,而 stat 有一个默认的 geom。
我的练习是通过 3 种方式创建绘图:使用 stat、手动和使用 geom_pointrange。我被困在第三部分:
library("tidyverse")
# With stat_summary
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.min = min,
fun.max = max,
fun = median
)
# Manually
diamonds_summary = diamonds %>%
group_by(cut) %>%
summarize(p = median(depth), lower = min(depth), upper = max(depth))
ggplot(diamonds_summary) +
geom_pointrange(
mapping = aes(x = cut, y = p, ymin = lower, ymax = upper)
)
# With geom_pointrange and stat
ggplot(data = diamonds) +
geom_pointrange(
mapping = aes(x = cut, y = median(depth)),
stat = "summary"
)
# Warning: No summary function supplied, defaulting to `mean_se()`
如何将两个汇总函数(最小值和最大值)传递给stat 参数标识的函数?
所有三种解决方案都应产生以下输出:
【问题讨论】: