R 是否有方法标记lda()生成的每个集群

R 是否有方法标记lda()生成的每个集群,r,ggplot2,lda,R,Ggplot2,Lda,使用lda()和ggplot2我可以用置信椭圆绘制标准图。是否有方法为绘图上的每个组添加标签(使用图例中的组标记每个簇) #对于普遍性lda(物种~,数据=iris)将是类似的 m、 lda%作为矩阵 CVA.scores标签可以放置在geom_文本或geom_标签上。在以下情况下,我将使用geom_标签,通过添加popn.radii外圆半径来调整y坐标 问题中的代码适用于使用内置数据集iris,就像问题本身所说的那样 m.cva.plot2 + geom_label(data = CIre

使用
lda()
ggplot2
我可以用置信椭圆绘制标准图。是否有方法为绘图上的每个组添加标签(使用图例中的组标记每个簇)

#对于普遍性lda(物种~,数据=iris)将是类似的
m、 lda%作为矩阵

CVA.scores标签可以放置在
geom_文本
geom_标签
上。在以下情况下,我将使用
geom_标签
,通过添加
popn.radii
外圆半径来调整
y
坐标

问题中的代码适用于使用内置数据集
iris
,就像问题本身所说的那样

m.cva.plot2 +
  geom_label(data = CIregions.mean.and.pop,
             mapping = aes(x = CV1.mean, 
                           y = CV2.mean + popn.radii, 
                           label = Species),
             label.padding = unit(0.20, "lines"),
             label.size = 0)

可复制代码

library(dplyr)
library(ggplot2)
library(ggforce)
library(MASS)

b <- iris
m.lda <- lda(Species~., data=iris) #would be analogous
#m.lda <- lda(Diet ~ ., data = b) 
m.sub <- b %>% dplyr::select(-Species) %>% as.matrix  
CVA.scores <- m.sub %*% m.lda$scaling
m.CV <- data.frame(CVA.scores)
m.CV$Species <- b$Species

m.cva.plot <-
ggplot(m.CV, aes(x = LD1, y = LD2)) + 
geom_point(aes(color=Species), alpha=0.5) + 
labs(x = "CV1", y = "CV2") +
coord_fixed(ratio=1) 

chi2 = qchisq(0.05,2, lower.tail=FALSE)
CIregions.mean.and.pop <-
m.CV %>%
group_by(Species) %>%
summarize(CV1.mean = mean(LD1),
        CV2.mean = mean(LD2),
        mean.radii = sqrt(chi2/n()),
        popn.radii = sqrt(chi2))

m.cva.plot2 <-
m.cva.plot + 
 geom_circle(data = CIregions.mean.and.pop,
          mapping = aes(x0 = CV1.mean, y0 = CV2.mean, r = mean.radii),
          inherit.aes = FALSE) +
geom_circle(data = CIregions.mean.and.pop,
          mapping = aes(x0 = CV1.mean, y0 = CV2.mean, r = popn.radii),
          linetype = "dashed", 
          inherit.aes = FALSE) 
库(dplyr)
图书馆(GG2)
图书馆(警队)
图书馆(弥撒)

b如果你有
x
y
坐标,为什么不
?几何图形文本
几何图形标签
?太棒了!非常感谢。
library(dplyr)
library(ggplot2)
library(ggforce)
library(MASS)

b <- iris
m.lda <- lda(Species~., data=iris) #would be analogous
#m.lda <- lda(Diet ~ ., data = b) 
m.sub <- b %>% dplyr::select(-Species) %>% as.matrix  
CVA.scores <- m.sub %*% m.lda$scaling
m.CV <- data.frame(CVA.scores)
m.CV$Species <- b$Species

m.cva.plot <-
ggplot(m.CV, aes(x = LD1, y = LD2)) + 
geom_point(aes(color=Species), alpha=0.5) + 
labs(x = "CV1", y = "CV2") +
coord_fixed(ratio=1) 

chi2 = qchisq(0.05,2, lower.tail=FALSE)
CIregions.mean.and.pop <-
m.CV %>%
group_by(Species) %>%
summarize(CV1.mean = mean(LD1),
        CV2.mean = mean(LD2),
        mean.radii = sqrt(chi2/n()),
        popn.radii = sqrt(chi2))

m.cva.plot2 <-
m.cva.plot + 
 geom_circle(data = CIregions.mean.and.pop,
          mapping = aes(x0 = CV1.mean, y0 = CV2.mean, r = mean.radii),
          inherit.aes = FALSE) +
geom_circle(data = CIregions.mean.and.pop,
          mapping = aes(x0 = CV1.mean, y0 = CV2.mean, r = popn.radii),
          linetype = "dashed", 
          inherit.aes = FALSE)