ggplot2 PCA散点图绘制

分别利用颜色(colour)和形状(shape i.e. pch)进行分组很多属性需要单独设置。

用到的对象有

  • 数据映射(Aes,Data aesthetic mappings)
  • 几何属性(Geom,Geometric objects)
  • 统计转换(Stat,Statistical transformations)
  • 标度(Scales)
  • 坐标系(xlab, ylab, coor, coordinate system)
  • 主题(theme)

暂时未用到分面(faceting)

 

myData <- read.csv("data.csv")
myData2 <- myData[,-20]
myData3 <- na.omit(myData2)
pr<-prcomp(myData3[,5:20],scale=TRUE)

pca <- pr$x

####一下为ggplot2绘图

library(ggplot2)
pca <- as.data.frame(pca)
pca$salinity <- myData3$Salinity
pca$climate <- myData3$Climate
pca$ecotype <- myData3$Ecotype
pca$name <- myData3$Plant.name

theme<-theme(panel.background = element_blank(),
             panel.border=element_rect(fill=NA),
             panel.grid.major = element_blank(),
             panel.grid.minor = element_blank(),
             strip.background=element_blank(),
             axis.text.x=element_text(colour="black"),
             axis.text.y=element_text(colour="black"),
             axis.ticks=element_line(colour="black"),
             plot.margin=unit(c(1,1,1,1),"line"),
             legend.title=element_blank(),
             legend.key = element_rect(fill = "white")
             )
percentage <- round(pr$sdev / sum(pr$sdev) * 100, 2)
percentage <- paste( colnames(pca), "(", paste( as.character(percentage), "%", ")", sep="") )

p <- ggplot(pca, aes(x=PC1, y=PC2,shape=paste(salinity, climate), colour=ecotype,label=name))
#p <- p + geom_point()+geom_text(size=3) + theme

p <- p + geom_point(size=3) + 
  theme + 
  xlab(percentage[1])  +
  ylab(percentage[2])  +
  scale_shape_manual(values = c(15,17,0,2),labels=c("Salt in Fanggan","Salt in Panjin","Fresh in Fanggan","Fresh in Panjin"))+
  stat_ellipse(aes(shape = NULL))+
  scale_x_continuous(limits = c(-6,6))+
  scale_y_continuous(limits = c(-5,5.5))
p

 

 

posted @ 2019-02-03 12:13  LiuLyle  阅读(4718)  评论(0编辑  收藏  举报