用不同的颜色显示 PCA
Displaying PCA with different colors
我有这个数据:
Desc ALL1 ALL2 AML1 AML2
Gene1 -214 -342 87 -172
Gene2 -153 -200 -248 -122
Gene3 -58 41 262 38
Gene4 88 328 295 31
我们有两种类型的组织 AML 和 ALL 白血病
我想将 PCA 应用于这些数据,所以我尝试这样做:
pca <- prcomp(x = all_aml_train_gct)
现在我想绘制它:
biplot(fit)
我得到这样的信息:
如何给绘图着色以区分 ALL 和 AML?
一种选择是使用名为 ggbiplot 的包,它在图形方面具有更大的灵活性。我在下面提供了一些代码来演示其功能。此外,在进行 PCA 时缩放变量也很重要,我不确定你是否这样做了,但只是想仔细检查一下。
data("mtcars")
mtcars
# using cylinder as grouping variable, and weight and hp for example
cyl <- mtcars$cyl
cyl <- as.factor(cyl)
xvars <- mtcars[, c(4,7)]
# Need to scale variables for PCA due to the fact that variables with large variance will impact PCA
xvars.scaled <- scale(xvars)
cars.pca <- prcomp(xvars.scaled)
# Create base plot
g.plot <- ggbiplot(cars.pca, obs.scale=1, var.scale=1,
groups=cyl, ellipse=TRUE)
# Add in color
g.plot <- g.plot+ scale_color_discrete(name='')
# add in legend
g.plot <- g.plot+ theme(legend.direction='horizontal',
legend.position='bottom')
g.plot
结果是:
更详尽的示例和教程也可以在以下 link 中找到:http://www.r-bloggers.com/computing-and-visualizing-pca-in-r/
希望对您有所帮助!
我有这个数据:
Desc ALL1 ALL2 AML1 AML2
Gene1 -214 -342 87 -172
Gene2 -153 -200 -248 -122
Gene3 -58 41 262 38
Gene4 88 328 295 31
我们有两种类型的组织 AML 和 ALL 白血病 我想将 PCA 应用于这些数据,所以我尝试这样做:
pca <- prcomp(x = all_aml_train_gct)
现在我想绘制它:
biplot(fit)
我得到这样的信息:
如何给绘图着色以区分 ALL 和 AML?
一种选择是使用名为 ggbiplot 的包,它在图形方面具有更大的灵活性。我在下面提供了一些代码来演示其功能。此外,在进行 PCA 时缩放变量也很重要,我不确定你是否这样做了,但只是想仔细检查一下。
data("mtcars")
mtcars
# using cylinder as grouping variable, and weight and hp for example
cyl <- mtcars$cyl
cyl <- as.factor(cyl)
xvars <- mtcars[, c(4,7)]
# Need to scale variables for PCA due to the fact that variables with large variance will impact PCA
xvars.scaled <- scale(xvars)
cars.pca <- prcomp(xvars.scaled)
# Create base plot
g.plot <- ggbiplot(cars.pca, obs.scale=1, var.scale=1,
groups=cyl, ellipse=TRUE)
# Add in color
g.plot <- g.plot+ scale_color_discrete(name='')
# add in legend
g.plot <- g.plot+ theme(legend.direction='horizontal',
legend.position='bottom')
g.plot
结果是:
更详尽的示例和教程也可以在以下 link 中找到:http://www.r-bloggers.com/computing-and-visualizing-pca-in-r/
希望对您有所帮助!