R语言 自动调整盒须ggplot上隐藏的Geom_text的垂直位置

eh57zj3b  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(134)

我尝试自动设置每个百分比标签的高度(对应于平均值)到每个晶须顶部的末端(例如)。在geom_text或其他地方使用简单的函数或参数是否可能?或者,我是否必须手动为每个标签创建一个geom_text来手动设置其位置?
正如您所看到的,当前输出显示I position_jitterdodge()'d了百分比标签,这看起来很混乱,并且彼此有些重叠,我希望避免这种情况。或者,如果能够在每个框中显示百分比就更好了。

ggplot编码:

ggplot(bcfourgroupbxplt, aes(x = Clusters, y = Proportions, 
                             fill = FourGroup)) +
  geom_point(aes(color = FourGroup, alpha = 0.5),
             position = position_jitterdodge(jitter.width = 0, 
                                             jitter.height = 0))  +
  geom_boxplot(aes(fill = FourGroup)) + 
  scale_fill_manual(name = "Group",
                    values = c("#20b0a8", "#f98110", "#FFC300", "#C70039"),
                    labels = c("Control", "B", "b.1", "b.2")) +
  scale_color_manual(values = c("Control" = "#20b0a8", "B" = "#f98110", 
  "b.1" = "#FFC300", "b.2" = "#C70039"),
                     labels = c("Control", "B", "b.1", "b.2")) +
  geom_text(aes(label = Mean, y = 0.9), 
            stat = "identity",
            position = position_jitterdodge(jitter.width = 0, 
                                            jitter.height = 0.15),
            size=2.5,
            data = bcmeanbxplt) +
  stat_summary(fun = mean, aes(alpha = 0.5), color = "black", shape = 3, 
               position = position_jitterdodge(jitter.width = 0, 
                                               jitter.height = 0)) +
  scale_x_discrete(labels = levels(finalnewbcell$Annots)) +
  xlab("Clusters") +
  ggtitle("B cell Compartment Proportions") +
  theme_clean() +
  theme(plot.title = element_text(hjust = 0.5, face = "bold")) +
  guides(alpha = "none", color = "none")

bcfourgroupbxplt示例结构:

A tibble:540 × 6

样本聚类比例组亚型FourGroup
1对照1中间体B 0.0556对照对照
中间体B 0.103...... B...... b.1...... b.1
中间体B 0·192····B···b·2···b·2
4Ctrl 37中间体B 0.207对照对照对照
中间体B 0.228...... B...... B...... B
--> B组个体重复,因为我想将总B组显示为一个框,然后按子组(b.1和b.2)将其拆分

geom_text表示的代码:

bcmeanbxplt <- bcfourgroupbxplt %>%
  group_by(Clusters, FourGroup) %>%
  summarize(Mean = mean(Proportions)) %>%
  ungroup() %>%
  mutate(Mean = percent(round(Mean, 3)))

我尝试将位置更改为position_dodgeposition_dodgevposition_jitterdodgeposition_nudge,并尝试使用这些参数,但没有任何效果(可能是因为我使用bcmeanbxplt作为geom_text()层中的数据?).

编辑:使用M.维京海盗的解决方案更新了图

Updated figure

lpwwtiir

lpwwtiir1#

一种方法是使用stat_summary()函数和一个helper函数,该函数通过boxplot.stats()返回箱线图geom使用的y位置

boxplot.stats(iris$Petal.Width)
#$stats
#[1] 0.1 0.3 1.3 1.8 2.5   # <- the 5th value is the end of the whisker
#$n
#[1] 150
#$conf
#[1] 1.10649 1.49351

mean_text_fun <- function(x){return(data.frame(y = boxplot.stats(x)$stats[5]+0.1, 
                                               label = paste0(mean(x))))}

图代码的第一部分,其中iris数据集与stat_summary组合

iris %>% 
  ggplot(aes(x = Species, y = Petal.Width, fill = Species)) +
  geom_point(aes(color = Species),alpha = 0.5,
             position = position_jitterdodge(jitter.width = 0, jitter.height = 0))  +
  geom_boxplot() +
  stat_summary(fun.data = mean_text_fun, geom = "text")

统计摘要想法-https://stackoverflow.com/a/15720769/10276092
箱形图统计理念-https://stackoverflow.com/a/4947033/10276092

相关问题