R语言下如何把qplot做出来的图合在一张图中

Python016

R语言下如何把qplot做出来的图合在一张图中,第1张

library(ggplot2)

p1 <- qplot(x1,y1,data=anscombe)

p2 <- qplot(x2,y2,data=anscombe)

p3 <- qplot(x3,y3,data=anscombe)

p4 <- qplot(x4,y4,data=anscombe)

library(gridExtra)

grid.arrange(p1,p2,p3,p4, nrow=2)

1、首先数据均一化

2、

#载入一个CSV文件,header=TRUE保证表头不被当成表格数据,sep=","用逗号把数据分开

mydata<-read.table("abc.csv",header=TRUE,sep=",")

library(ggplot2)

sample.groups<- c(rep(1, 124), rep(2, 54), rep(3, 199))

#把X那一列分成3组,rep(1,124)的意思是124个1,rep(2,54)的意思是54个2,这些都是按照X列的数字设定的,便于将A组的数据分成不同组

qplot(x=PC1,y=PC2, data=mydata,colour=factor(sample.groups))+theme(legend.position="none")+stat_ellipse(lwd=1)

#x=PC1,y=PC2的意思是设定x,y轴为PC1和PC2那两列。colour=factor(sample.groups)设定3组不同颜色。theme(legend.position="none")是把图例去掉。stat_ellipse(lwd=1)是将不同组的点加上椭圆,lwd=1设定椭圆圈的粗度为1

3、高级选项

给图中的每个点添加标签

library(ggrepel)

#图上显示每个点的标签(标签的名称是A列)

label=mydata$A

#60-141行的标签为空格

label[60:141]=""

qplot(x=PC1,y=PC2, data=mydata,colour=factor(sample.groups))+theme(legend.position="none")+stat_ellipse(lwd=1)+geom_text_repel(label=label)

应该是没有加载包。xyplot的包是lattice,qplot的包是ggplot2,试试输入运行

install.packages("lattice")

install.packages("ggplot2")

library("lattice")

library("ggplot2")