我想在stat_summary中“强制”一个聚合函数来计算单个观测值的输出值:
set.seed(1)
value <- c(rep(1:6, each = 3), 7:8)
rel_freq <- sample(x = seq(0, 1, 0.1), size = length(value), replace = TRUE)
example_df <- data.frame(value, rel_freq)
require(ggplot2)
ggplot() +
stat_summary(data = example_df,
mapping = aes(x = as.character(value), y = rel_freq),
fun.data = mean_se)
# Warning message: Removed 2 rows containing missing values (geom_pointrange)
现在在这里发生的事件(IMO)是ggplot
删除了观测值7和8,因为stat_summary
中的聚合功能不适用于单个观测值吗?但是有办法在这里强制输出吗?
答案 0 :(得分:1)
您可以编写自己的扩展mean_se
的小函数,以处理x的长度等于1的情况。
mean_se_tjebo <- function (x, mult = 1) {
x <- stats::na.omit(x)
se <- mult * sqrt(stats::var(x)/length(x))
mean <- mean(x)
if(length(x) != 1) {
data.frame(y = mean, ymin = mean - se, ymax = mean + se)
} else {
data.frame(y = mean, ymin = mean, ymax = mean)
}
}
现在情节如下所示
ggplot() +
stat_summary(data = example_df,
mapping = aes(x = as.character(value), y = rel_freq),
fun.data = mean_se_tjebo)