R,ggbiplot中的主成分分析
我是一个试图使用R的生物学家,我正在与它斗争 我正在尝试为这些数据生成主成分分析:R,ggbiplot中的主成分分析,r,pca,ggbiplot,R,Pca,Ggbiplot,我是一个试图使用R的生物学家,我正在与它斗争 我正在尝试为这些数据生成主成分分析: 1,26.96,37.31,35.74 1,24.27,38.48,37.24 1,23.58,35.64,40.78 1,24.29,35.72,39.99 1,26.43,37.72,35.85 1,28.80,46.96,24.24 2,30.05,44.86,25.09 2,26.59,47.24,26.17 2,30.55,45.70,23.75 2,25.95,48.77,25.28 2,23.31,
1,26.96,37.31,35.74
1,24.27,38.48,37.24
1,23.58,35.64,40.78
1,24.29,35.72,39.99
1,26.43,37.72,35.85
1,28.80,46.96,24.24
2,30.05,44.86,25.09
2,26.59,47.24,26.17
2,30.55,45.70,23.75
2,25.95,48.77,25.28
2,23.31,50.11,26.59
2,31.29,43.88,24.82
3,14.70,37.65,47.65
3,17.11,36.14,46.75
3,15.83,36.83,47.34
3,13.77,38.39,47.84
3,17.76,35.02,47.22
3,12.90,38.29,48.81
第一列对应于组变量,我有3个组
我看到了,如何制作我想要的图形。但每次我到达生成双批次部分时,我都会得到以下信息:
错误:提供给离散刻度的连续值
以下是我正在使用的代码:
>data(GPA2)
>head(GPA2, 3)
>log.ir <- log(GPA2[, 2:4])
>ir.group <- GPA2[, 1]
>ir.pca <- prcomp(log.ir,center = TRUE,scale = TRUE)
>print(ir.pca)
>plot(ir.pca, type = "l")
>summary(ir.pca)
>predict(ir.pca, newdata=tail(log.ir, 2))
>g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.group, ellipse = TRUE, circle = TRUE)
>g <- g + scale_color_discrete(name = '')
>g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')
>print(g)
>数据(GPA2)
>头部(GPA2,3)
>log.ir.group ir.pca打印(ir.pca)
>绘图(ir.pca,type=“l”)
>摘要(ir.pca)
>预测(ir.pca,newdata=tail(log.ir,2))
>g打印(g)
有人能帮我吗?您的分组变量需要是一个因子,而不是数字
library(ggbiplot)
GPA2 <- data.frame(
ir.group = sample(c(1,2,3),10, replace = TRUE),
x = sample(1:10),
y = sample(1:10),
z = sample(1:10)
)
data(GPA2)
head(GPA2, 3)
log.ir <- log(GPA2[, 2:4])
ir.group <- GPA2[, 1]
ir.pca <- prcomp(log.ir,center = TRUE,scale = TRUE)
print(ir.pca)
plot(ir.pca, type = "l")
summary(ir.pca)
predict(ir.pca, newdata=tail(log.ir, 2))
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = as.factor(ir.group), ellipse = TRUE, circle = TRUE)
g <- g + scale_color_discrete(name = '')
g <- g + theme(legend.direction = 'horizontal', legend.position = 'top')
print(g)
库(ggbiplot)
GPA2第一列中的分组变量是一个整数(1,2,3),它需要是一个字符串,替换为类似“group1”、“group2”、“group3”(不带“)。然后,您的数据将如下所示:
group 1,26.96,37.31,35.74
group 1,24.27,38.48,37.24
group 1,23.58,35.64,40.78
group 1,24.29,35.72,39.99
group 1,26.43,37.72,35.85
group 1,28.80,46.96,24.24
group 1,30.05,44.86,25.09
group 2,26.59,47.24,26.17
group 2,30.55,45.70,23.75
group 2,25.95,48.77,25.28
group 2,23.31,50.11,26.59
group 2,31.29,43.88,24.82
group 3,14.70,37.65,47.65
group 3,17.11,36.14,46.75
group 3,15.83,36.83,47.34
group 3,13.77,38.39,47.84
group 3,17.76,35.02,47.22
group 3,12.90,38.29,48.81