gpt4 book ai didi

r - ggplot2 & stat_ellipse : Draw ellipses around multiple groups of points

转载 作者:行者123 更新时间:2023-12-05 09:23:03 25 4
gpt4 key购买 nike

这可能很简单,但我试图在我的 PCoA 图上围绕我的治疗绘制椭圆。

我的数据框 (sc) 是:

             MDS1        MDS2 Treatment
X1xF1 -0.19736183 -0.24299825 1xFlood
X1xF2 -0.17409568 -0.29727596 1xFlood
X1xF3 -0.15272444 -0.28553837 1xFlood
S1 -0.06643271 0.47049959 Start
S2 -0.15143350 0.31152966 Start
S3 -0.26156297 0.12296849 Start
X3xF1 0.29840827 0.04581617 3xFloods
X3xF2 0.50503749 -0.07011503 3xFloods
X3xF3 0.20016537 -0.05488630 3xFloods

我的代码是:

ggplot(data=sc,(aes(x=MDS1,y=MDS2,colour = Treatment)))+geom_point(size=3)+
ggtitle("PCoA of samples at 'class' level(method='Bray')\n",sep=''))+
theme_bw()+guides(colour = guide_legend(override.aes = list(size=3)))+
stat_ellipse()

它绘制 PCoA 直到 stat_ellipse()。我已经用各种参数尝试过,最多只能为整个图得到一个椭圆(尽管我现在似乎无法重现)。

我所追求的是三种处理的三个 CI 椭圆,颜色与处理相同。任何帮助将不胜感激!

谢谢。

最佳答案

ggplot 包中没有stat_ellipse(...),所以您一定是从其他地方取回的。关心分享??我知道至少有两个版本,here , 和 here .这些似乎都不适用于您的数据集,这很奇怪,因为它们都适用于其他数据集。

我最终选择了在 ggplot 外部生成省略号的选项,这实际上并不难。

library(ggplot2)
library(ellipse)
centroids <- aggregate(cbind(MDS1,MDS2)~Treatment,sc,mean)
conf.rgn <- do.call(rbind,lapply(unique(sc$Treatment),function(t)
data.frame(Treatment=as.character(t),
ellipse(cov(sc[sc$Treatment==t,1:2]),
centre=as.matrix(centroids[t,2:3]),
level=0.95),
stringsAsFactors=FALSE)))

ggplot(data=sc,(aes(x=MDS1,y=MDS2,colour = Treatment)))+
geom_point(size=3)+
geom_path(data=conf.rgn)+
ggtitle(paste("PCoA of samples at 'class' level(method='Bray')\n",sep=''))+
theme_bw()+
guides(colour = guide_legend(override.aes = list(size=3)))

关于r - ggplot2 & stat_ellipse : Draw ellipses around multiple groups of points,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24268843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com