R语言 修改Y轴以显示每组中的观测数

zour9fqk  于 2023-03-10  发布在  其他
关注(0)|答案(1)|浏览(123)

我希望我的图显示每组中有多少人/观测值。我该怎么做?我找到了这个答案,但我很难理解它。Modify x-axis labels in each facet

Raincloud <- iris %>%
  dplyr::group_by(Species) %>%
  dplyr::mutate(mean = mean(Petal.Length),
         se = sd(Petal.Length)/sqrt(length(Petal.Length))) %>%
  ungroup() %>%
  ggplot(aes(x=Petal.Length, y=Species)) +
  stat_slab(aes(fill = Species, )) +
   stat_dots(aes(color = Species), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color"))  +
  geom_errorbar(aes(xmin = mean - 1.96 * se,
                    xmax = mean + 1.96 * se), width = 0.2) +
  stat_summary(fun=mean, geom="point", shape=16, size=3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top")+
  labs(title="Raincloud plot with ggdist")
  print(Raincloud + labs(x = "Petal Length"))  [![Result[1]][1]

RCsex <- uniquePeople %>%
  dplyr::group_by(sex) %>%
  dplyr::mutate(mean = mean(thresh.x),
         se = sd(thresh.x)/sqrt(length(thresh.x))) %>%
  ungroup() %>%
  ggplot(aes(x=thresh.x, y=sex)) +
  stat_slab(aes(fill = sex), scale = 0.5) +
   stat_dots(aes(color = sex), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
  geom_errorbar(aes(xmin = mean - 1.96 * se,
                    xmax = mean + 1.96 * se), width = 0.2) +
  stat_summary(fun=mean, geom="point", shape=16, size=3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top")+
 scale_x_continuous(name="Estimated Nominal Detection Threshold (log10 units)", limits=c(-4.5, 0.5),

breaks = seq(-4.5,0.5,by = 0.5))+实验室(标题=“显示按性别分层的气味检测阈值的雨云图”)打印(RCsex +实验室(y =“性别”))

nhaq1z21

nhaq1z211#

一种方法是在数据集中添加一个新列,将每个物种的观测数粘贴到物种名称上,然后将该列Map到y上:

library(dplyr)
library(ggplot2)
library(ggdist)

iris %>%
  dplyr::group_by(Species) %>%
  dplyr::mutate(
    mean = mean(Petal.Length),
    se = sd(Petal.Length) / sqrt(length(Petal.Length)),
    species_y = paste0(Species, "\n(", n(), ")")
  ) %>%
  ungroup() %>%
  ggplot(aes(x = Petal.Length, y = species_y)) +
  stat_slab(aes(fill = Species)) +
  stat_dots(aes(color = Species), side = "bottom", shape = 16) +
  scale_fill_brewer(palette = "Set1", aesthetics = c("fill", "color")) +
  geom_errorbar(aes(
    xmin = mean - 1.96 * se,
    xmax = mean + 1.96 * se
  ), width = 0.2) +
  stat_summary(fun = mean, geom = "point", shape = 16, size = 3.0) +
  theme_bw(base_size = 10) +
  theme(legend.position = "top") +
  labs(title = "Raincloud plot with ggdist", x = "Petal Length")

相关问题