在盒须 ggplot 上自动调整闪避 Geom_text 的垂直位置

问题描述 投票:0回答:1

我正在尝试将每个百分比标签(对应于平均值)的高度自动设置为每个胡须顶部的末端(例如)。这可以通过 geom_text 或其他地方的简单函数或参数实现吗?或者,我是否必须手动进入并为每个标签创建一个 geom_text 以手动设置其位置?

如您所见,当前输出显示我

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的示例结构:

小题:540 × 6

Samples Clusters Proportions Group Subtype FourGroup
1 Ctrl1 中级 B 0.0556 控制控制控制
2 pt001 中间体 B 0.103 ......B ....b.1 ....b.1
3 pt032 中间体 B 0.192 ......B ....b.2 ....b.2
4 Ctrl37 中级 B 0.207 控制控制控制
25 pt032 中间体 B 0.228 ......B ....b.2 ......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)))

Current Output

我已经尝试将位置更改为

position_dodge
,或
position_dodgev
position_jitterdodge
,或
position_nudge
并尝试这些参数无济于事(可能是因为我使用
bcmeanbxplt
作为数据
geom_text()
层?)。

编辑:使用 M.Viking 的解决方案更新图形
Updated figure

r ggplot2 plot boxplot
1个回答
0
投票

一种方法是使用

stat_summary()
函数和一个辅助函数,它返回箱线图geom使用的y位置通过
boxplot.stats()

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")

Stat Summary Idea - https://stackoverflow.com/a/15720769/10276092

箱线图统计想法 - https://stackoverflow.com/a/4947033/10276092

© www.soinside.com 2019 - 2024. All rights reserved.