You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为ggplot小提琴图各分块差异化调整vjust或优化标签位置?

如何在ggplot小提琴图中为每个分块差异化调整n值标签的位置?

我用ggplot绘制小提琴图时,需要用mean_cl_boot标注均值置信区间,同时给每个小提琴分块标注样本量n值。但由于各小提琴分块的长度不一样,统一设置vjust(比如设为-8)会导致标签位置混乱。想请教:是否可以为每个小提琴分块差异化调整vjust,或者让计数标签整齐显示在分块的上方?

以下是我的绘图代码及iris数据集的可复现代码:

自定义数据集绘图代码

ggplot(data_changedorderwithoutNAs, aes(x = KnownBehAgeCategories,y = Energetically.Expensive.Displays.Per.Hour)) +
  geom_violin(
    mapping = aes(
      x = KnownBehAgeCategories,
      y = Energetically.Expensive.Displays.Per.Hour, fill = KnownBehAgeCategories
    )
  )+
  theme_classic()+
  stat_summary(fun.data = "mean_cl_boot", geom = "pointrange")+
  stat_summary(fun.data = n_fun, geom = "text", vjust = -6)+
  labs(y="Display Rate", x="Known Behavioral Age")+
  scale_fill_brewer(palette="BuPu")

iris数据集可复现代码

ggplot(iris, aes(x = Species,y = Petal.Width)) +
  geom_violin(
    mapping = aes(
      x = Species,
      y = Petal.Width, fill = Species
    )
  )+
  theme_classic()+
  stat_summary(fun.data = "mean_cl_boot", geom = "pointrange")+
  stat_summary(fun.data = n_fun, geom = "text", vjust = -8)+
  labs(y="Species", x="Petal Width")+
  scale_fill_brewer(palette="BuPu")

内容的提问来源于stack exchange,提问作者ksh530

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:28:21