R语言 小提琴图,调整vjust不同的每一个形状?

ss2ws0br  于 2023-03-20  发布在  其他
关注(0)|答案(2)|浏览(229)

我尝试在ggplot中用mean_cl_ Boot 和n值来标记小提琴图形,并想知道是否有办法针对每个小提琴图形不同地调整vjust,或者是否有办法定期获取图形上方的计数值。正如您所看到的,我的小提琴有不同的长度,因此将vjust设置为-8(例如)会将标签放置在与图形相关的所有位置。

我已经包含了我的代码为下面的图表,以及一个可复制的例子与虹膜,但我想得到我的奇怪的数据集,因为虹膜看起来很干净的点.对不起,我只是没有找到正确的搜索条件,我的问题,但我想这是简单的东西.

# Now make a nicer plot of Known Behavioral Age Categories vs Display Rate 
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")

使用虹膜数据集:

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

b1zrtrql1#

vjust 设置为0,并使用 n_fun 更新版本(来自此帖子:Automatic n plotting with ggplot and stat_summary),y值设置为 max

n_fun <- function(x){
  return(data.frame(y = max(x), label = paste0("n = ",length(x))))
} 

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

wj8zmpe1

wj8zmpe12#

下面的代码最终为我工作,感谢一些修改,从昨天的答复。抱歉愚蠢的名称,但我希望这能帮助一些人。

# create function for readout values
n_fun <- function(x){
  return(data.frame(y = max(x), label = paste0("n = ",length(x))))
} 

# Now make a nicer plot of Known Behavioral Age Categories vs Display Rate 
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 = -1)+
  labs(y="Display Rate", x="")+
  scale_fill_brewer(palette="BuPu")+
  scale_y_continuous(limits = c(0,10))

相关问题