我想为每个半提琴添加一个参考值。
这是我当前情节的代码(第一张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
...
我该如何实现?预先感谢和最诚挚的问候
我找到了我正在寻找的答案here。对于geom_point()
,仅需要加法position=position_dodge(width=0.7)
或其他数字。感谢上面链接中的人员!
以及最终代码:(我合并了第一篇文章中提到的两个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))