R语言 雨云图的定标问题

fruv7luv  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(112)

我试图创建一个雨云图来显示性别分数,但它是基于其分数

对每个点进行分组,我希望它看起来像这个图像,其中花瓣。长度按物种分组,而不是长度本身描绘

。我有代码,一直与其他集,但我不知道是什么问题。
我也检查了一下,看看分数是连续的还是离散的,它是连续的 *
下面是我在R中使用的代码:

dplyr::group_by(sex) %>%
  dplyr::mutate(
    mean = mean(score),
    se = sd(score) / sqrt(length(score)),
    sex_y = paste0(sex, "\n(", n(), ")")
  ) %>%
  ungroup() %>%
  ggplot(aes(x = NIH_score, y = sex_y)) +
  stat_slab(aes(fill = sex)) +
  geom_point(aes(color = sex),shape = 16,
             position = ggpp::position_jitternudge(height = 0.125, width = 0, 
                                             y = -0.125,
                                             nudge.from = "jittered")) +
  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 = "score")```

字符串

xzlaal3s

xzlaal3s1#

这并不是说你的数据是按x轴值分组的。只是核密度估计器的带宽太小了。
让我们使用基本相同的代码重新创建您的问题,但使用一些虚构的数据:

library(tidyverse)
library(ggdist)

set.seed(1)
df <- tibble(NIH_score = sample(2:8, 200, TRUE),
             sex = sample(c("Male", "Female"), 200, TRUE),
             score = NIH_score)

df  %>%
  dplyr::group_by(sex) %>%
  dplyr::mutate(
    mean = mean(score),
    se = sd(score) / sqrt(length(score)),
    sex_y = paste0(sex, "\n(", n(), ")")
  ) %>%
  ungroup() %>%
  ggplot(aes(x = NIH_score, y = sex_y)) +
  stat_slab(aes(fill = sex), adjust = 0.1) +
  geom_point(aes(color = sex),shape = 16,
             position = ggpp::position_jitternudge(height = 0.125, width = 0, 
                                                   y = -0.125,
                                                   nudge.from = "jittered")) +
  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 = "score")

字符串


的数据
但是,如果我们使用adjust参数将带宽增加到stat_slab内部的2,我们会得到:

目前尚不清楚您的设置或数据是如何提供如此窄的带宽的(因为您的问题中也没有),但您应该能够通过增加adjust来获得所需的结果

相关问题