如何基于组将单个值(形状)添加到geom_split_violin()?

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

我想为每个半提琴添加一个参考值。

enter image description here

它看起来像这样的形状:enter image description here

这是我当前情节的代码(第一张img):

ggplot(data = csi, aes(x=species,y=CSI, fill=time))+
  geom_split_violin(stat = "ydensity", trim = T,scale = "width")+
  scale_fill_manual(values=c("grey40","grey60"))+
  theme(axis.text.x=element_text(angle=35,hjust = 1))

这就是我的数据集的样子:

str(csi)
'data.frame':   265196 obs. of  3 variables:
 $ species: Factor w/ 17 levels "Tilia europaea",..: 8 8 8 8 8 8 8 8 8 8 ...
 $ time   : Factor w/ 2 levels "present","future": 1 1 1 1 1 1 1 1 1 1 ...
 $ CSI    : num  0.395 0.66 0.615 0.612 0.808 ...

head(csi)
           species    time       CSI
1 Acer platanoides present 0.3953996
2 Acer platanoides present 0.6603609
3 Acer platanoides present 0.6148618
...

我有一个附加的数据框,其中包含我想添加到绘图中的每个物种和时间的值:

                 species    time      mean
1       Acer platanoides present 0.7069132
2       Acer platanoides  future 0.4984167
3            Acer rubrum present 0.2257700
4            Acer rubrum  future 0.1622086
...

我该如何实现?预先感谢和最诚挚的问候

r ggplot2 shapes violin-plot
1个回答
0
投票

我找到了我正在寻找的答案here。对于geom_point(),仅需要加法position=position_dodge(width=0.7)或其他数字。感谢上面链接中的人员!

这是我的情节:enter image description here

以及最终代码:(我合并了第一篇文章中提到的两个df,并在第一个df中添加了另一个col "mean"

  geom_split_violin(aes(x = species,y = CSI, fill = time),
                    stat = "ydensity", trim = T, scale = "width")+
  geom_point(mapping = aes(x = species, y = mean, shape = time), position = position_dodge(width = 0.7))+
  theme(axis.text.x=element_text(angle=35,hjust = 1))
© www.soinside.com 2019 - 2024. All rights reserved.