R语言 选择要在ggplot2中绘制的顶部和底部10%

wz1wpwve  于 2023-09-27  发布在  其他
关注(0)|答案(1)|浏览(120)

我想很容易地选择要在ggplot中绘制的变量平均值的顶部和底部10%。我有一个更大的数据集,在2年的时间内,“治疗”已经重复。我想找出2年内的平均值,并仅绘制平均值为所有治疗的顶部和底部10%的治疗。
目前,我已经为某个变量创建了所有治疗的图,然后找到顶部和底部的10%,并使用subset()选择这些治疗仅包含在最终图中。这太耗时,并且不能容易地转移到另一个变量。
我用星球大战的数据集复制了这个:

  1. ggplot(subset(starwars,homeworld %in% c("Quermia","Kashyyyk","Kalee","Kamino","Aleen Minor","Endor","Vulpter","Malastare")), aes(x=`homeworld`, y=`height`, fill = homeworld)) +
  2. geom_boxplot(outlier.shape = NA) +
  3. stat_summary(fun.y=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  4. theme(legend.position = 'none') +
  5. theme(axis.text.x = element_text(angle = 40,hjust = 1, vjust = 1,face = "bold",
  6. colour = "black", size = rel(0.8)))

理想情况下,我会有一行代码可以复制并用于,在星球大战的例子中,质量而不是高度。使用我目前的方法,我必须绘制所有的家园世界,然后选择我想添加到最终的情节。

vdgimpew

vdgimpew1#

一种方法是创建一个函数来为您创建子集,这样您就不必像下面的best_worst()函数那样做了。它接受数据、分组变量和要计算其均值的变量,并返回具有最高和最低均值的prop*n组。然后可以在图中使用此数据。

  1. library(dplyr)
  2. library(ggplot2)
  3. best_worst <- function(.data, .group, .vbl, prop = .1, ...){
  4. sum_data <- .data %>%
  5. group_by({{.group}}) %>%
  6. filter(!is.na({{.vbl}})) %>%
  7. summarise(x = mean({{.vbl}}, na.rm=TRUE)) %>%
  8. arrange(x)
  9. n <- nrow(sum_data)
  10. n_keep <- floor(n*prop)
  11. top <- sum_data %>%
  12. ungroup %>%
  13. slice_head(n=n_keep) %>%
  14. select({{.group}}) %>%
  15. pull()
  16. bottom <- sum_data %>%
  17. ungroup %>%
  18. slice_tail(n=n_keep) %>%
  19. select({{.group}}) %>%
  20. pull()
  21. .data %>% filter({{.group}} %in% c(top, bottom))
  22. }
  1. ggplot(best_worst(starwars, homeworld, height), aes(x=`homeworld`, y=`height`, fill = homeworld)) +
  2. geom_boxplot(outlier.shape = NA) +
  3. stat_summary(fun=mean, geom="point", shape=20, size=5, color="red", fill="red") +
  4. theme(legend.position = 'none') +
  5. theme(axis.text.x = element_text(angle = 40,hjust = 1, vjust = 1,face = "bold",
  6. colour = "black", size = rel(0.8)))

创建于2023-09-26附带reprex v2.0.2

展开查看全部

相关问题