使用 ggplot2 在 R 中绘制 PCA 的问题
Problems Plotting PCA in R with ggplot2
我目前正在尝试为我的数据绘制 PCA,当我 运行 代码时遇到以下问题。
此外,任何人都可以帮助获取我的数据和代码并生成 PLS-DA 吗?像图片一样吗?我找不到任何好的教程。
我该如何解决这个问题?这些图应如下所示:
经过一些帮助,我走到了这一步:
我的代码:
library(ggplot2)
library(ggforce)
all_datanoT <- cbind(amino,sphingo,hexose,phospha,lyso,cleaned_xl_Kopie)
all_datawT <- cbind(aminotnos,sphingo,hexose,phospha,lyso,cleaned_xl_Kopie)
rownames(all_datawT) <- sample_id$`Sample Identification`
alldata_naomit <-na.omit(all_datanoT)
all_datawTnaomit <-na.omit(all_datawT)
mypr <- prcomp(log2(alldata_naomit), scale = TRUE)
summary(mypr)
str(mypr)
mypr$x
PC1 <- mypr$x[, 1]
PC2 <- mypr$x[, 2]
pcat <- cbind(all_datawTnaomit, PC1, PC2)
ggplot(
data = pcat,
aes(
x = PC1,
y = PC2,
fill = 'Time point',
line = 1
),
shape = 1
) +
geom_point(
shape = 21,
colour = "black",
size = 2,
stroke = 0.5,
alpha = 0.6
) +
scale_fill_brewer(palette = "Set1") +
scale_color_brewer(palette = "Set1") +
geom_mark_ellipse(
aes(
fill = 'Time point',
color = 'Time point'
),
alpha = 0.05
)
产生以下情节:
如何让它对两个椭圆 T0 和 T1 使用两个不同的时间值?以及我如何轻松地估算我的数据,以便将 Na 替换为列手段,而不是仅仅为了绘制而忽略它们?
带有 dput() 的原始样本数据
dput(pcat[sample(nrow(pcat),50)])
https://gist.github.com/bicvn/47d97929a63ff99e9b260e8658407ae3
新dput
https://gist.github.com/bicvn/b06279c6bfa641303b57a3ad2cc07a21
您的代码与输出之间似乎存在差异:
pcat <- cbind(all_datawT, mypr$x[, 1:2])
将 mypr$x 的前两列添加到数据框。但输出显示:
mypr$x[1:2]
这是矩阵x的前两个值。如果查看该列,您会发现这两个值在数据中重复出现。在 R 中,这是回收,当 cbind
用于组合不同长度的向量时,这是默认过程。
未找到变量 PC1
和 PC2
,因为您从未使用这些值创建任何对象,例如
PC1 <- mypr$x[, 1]
PC2 <- mypr$x[, 2]
pcat <- cbind(all_datawT, PC1, PC2)
应该可以。
还要检查这个,这里我举了一个例子。该技巧使用 Comps <- as.data.frame(mypca$x)
来隔离组件,然后添加到原始数据中。之后,您可以使用 cbind()
和 Comps[,c(1,2)]
来仅提取前两个组件。在这里,我使用了 iris
数据集:
library(ggplot2)
library(ggforce)
#Data
data("iris")
#PCA
mypca <- prcomp(iris[,-5])
#Isolate components
Comps <- as.data.frame(mypca$x)
#Extract components and bind to original data
newiris <- cbind(iris,Comps[,c(1,2)])
#Plot
ggplot(newiris, aes(x=PC1, y=PC2, col = Species, fill = Species)) +
stat_ellipse(geom = "polygon", col= "black", alpha =0.5)+
geom_point(shape=21, col="black")
输出:
在共享数据的情况下,仅不应用NA动作。这里是您分享的代码和输出数据:
#Code
ggplot(pcat, aes(x=PC1, y=PC2, col = `Time point`, fill = `Time point`)) +
stat_ellipse(geom = "polygon", col= "black", alpha =0.5)+
geom_point(shape=21, col="black")
输出:
我目前正在尝试为我的数据绘制 PCA,当我 运行 代码时遇到以下问题。
此外,任何人都可以帮助获取我的数据和代码并生成 PLS-DA 吗?像图片一样吗?我找不到任何好的教程。
我该如何解决这个问题?这些图应如下所示:
经过一些帮助,我走到了这一步:
我的代码:
library(ggplot2)
library(ggforce)
all_datanoT <- cbind(amino,sphingo,hexose,phospha,lyso,cleaned_xl_Kopie)
all_datawT <- cbind(aminotnos,sphingo,hexose,phospha,lyso,cleaned_xl_Kopie)
rownames(all_datawT) <- sample_id$`Sample Identification`
alldata_naomit <-na.omit(all_datanoT)
all_datawTnaomit <-na.omit(all_datawT)
mypr <- prcomp(log2(alldata_naomit), scale = TRUE)
summary(mypr)
str(mypr)
mypr$x
PC1 <- mypr$x[, 1]
PC2 <- mypr$x[, 2]
pcat <- cbind(all_datawTnaomit, PC1, PC2)
ggplot(
data = pcat,
aes(
x = PC1,
y = PC2,
fill = 'Time point',
line = 1
),
shape = 1
) +
geom_point(
shape = 21,
colour = "black",
size = 2,
stroke = 0.5,
alpha = 0.6
) +
scale_fill_brewer(palette = "Set1") +
scale_color_brewer(palette = "Set1") +
geom_mark_ellipse(
aes(
fill = 'Time point',
color = 'Time point'
),
alpha = 0.05
)
产生以下情节:
如何让它对两个椭圆 T0 和 T1 使用两个不同的时间值?以及我如何轻松地估算我的数据,以便将 Na 替换为列手段,而不是仅仅为了绘制而忽略它们?
带有 dput() 的原始样本数据
dput(pcat[sample(nrow(pcat),50)])
https://gist.github.com/bicvn/47d97929a63ff99e9b260e8658407ae3
新dput
https://gist.github.com/bicvn/b06279c6bfa641303b57a3ad2cc07a21
您的代码与输出之间似乎存在差异:
pcat <- cbind(all_datawT, mypr$x[, 1:2])
将 mypr$x 的前两列添加到数据框。但输出显示:
mypr$x[1:2]
这是矩阵x的前两个值。如果查看该列,您会发现这两个值在数据中重复出现。在 R 中,这是回收,当 cbind
用于组合不同长度的向量时,这是默认过程。
未找到变量 PC1
和 PC2
,因为您从未使用这些值创建任何对象,例如
PC1 <- mypr$x[, 1]
PC2 <- mypr$x[, 2]
pcat <- cbind(all_datawT, PC1, PC2)
应该可以。
还要检查这个,这里我举了一个例子。该技巧使用 Comps <- as.data.frame(mypca$x)
来隔离组件,然后添加到原始数据中。之后,您可以使用 cbind()
和 Comps[,c(1,2)]
来仅提取前两个组件。在这里,我使用了 iris
数据集:
library(ggplot2)
library(ggforce)
#Data
data("iris")
#PCA
mypca <- prcomp(iris[,-5])
#Isolate components
Comps <- as.data.frame(mypca$x)
#Extract components and bind to original data
newiris <- cbind(iris,Comps[,c(1,2)])
#Plot
ggplot(newiris, aes(x=PC1, y=PC2, col = Species, fill = Species)) +
stat_ellipse(geom = "polygon", col= "black", alpha =0.5)+
geom_point(shape=21, col="black")
输出:
在共享数据的情况下,仅不应用NA动作。这里是您分享的代码和输出数据:
#Code
ggplot(pcat, aes(x=PC1, y=PC2, col = `Time point`, fill = `Time point`)) +
stat_ellipse(geom = "polygon", col= "black", alpha =0.5)+
geom_point(shape=21, col="black")
输出: