在LDA图形的顶部添加一个新的船体层- R

neskvpey  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(90)

我添加了一些人工数据到虹膜数据集(位置),并做了一个LDA图。我如何在LDA上绘制另一层,这将是locationData的船体。
谢谢
代码:

myDF <- iris
locationData <- "UK"
myDF <- cbind(myDF, locationData)
myDF[30:45,6]<- "France"
myDF[105:145,6]<- "Japan"
myDF[146:150,6]<- "France"
Species<-myDF$Species

data4LDA<-lda(Species~. ,myDF[1:4] ) #Perfrom LDA 
LD1<-predict(data4LDA)$x[,1]
LD2<-predict(data4LDA)$x[,2]

dt<-data.table(Species,LD1,LD2)
dt[order(dt$Species),]

LDAplot <- ggplot(dt,aes(x=LD1,y=LD2)) +  
  geom_point(size = 2, aes(color=Species)) +
  stat_chull(aes(color = Species, fill = Species), geom = "polygon", alpha = 0.1) 
LDAplot

字符串

ttp71kqs

ttp71kqs1#

只需在dt对象的列中包含位置,并添加另一个Map到它的stat_chull

dt <- data.table::data.table(Species, location = myDF$locationData, LD1,LD2)
dt[order(dt$Species),]

ggplot(dt,aes(LD1, LD2)) +  
  geom_point(size = 2, aes(color = Species)) +
  ggpubr::stat_chull(aes(color = Species, fill = after_scale(alpha(color, 0.1))), 
                     geom = "polygon", 
                     alpha = 0.1) +
  ggpubr::stat_chull(aes(fill = location, color = after_scale(alpha(fill, 1))), 
                     geom = "polygon", alpha = 0.1) +
  scale_fill_brewer("Location", palette = "Set2") +
  scale_color_brewer("Species", palette = "Set1")

字符串


的数据

相关问题