首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >biplot princomp和biplot中的颜色

biplot princomp和biplot中的颜色
EN

Stack Overflow用户
提问于 2021-03-16 01:52:03
回答 2查看 76关注 0票数 0

如何使用princomp和biplot将双图中的虹膜数据集的物种着色为不同的颜色。最好的

代码语言:javascript
复制
data(iris)


fit <- princomp(iris[,c(1:3)], cor=TRUE)
biplot(fit)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-16 16:20:54

用于渲染双线图的函数stats:::biplot.princompstats:::biplot.default不允许对不同的点使用多种颜色或不同的颜色。最简单的解决方案是使用包,例如另一个答案中提到的ggfortify:

代码语言:javascript
复制
library(ggfortify)
autoplot( fit, data=iris, colour="Species", loadings=TRUE )

factoextra

代码语言:javascript
复制
library(factoextra)
fviz_pca_biplot(fit, col.ind = iris$Species)

最后一种选择是重写biplot函数,如下所示,col1是数据点的颜色矢量,col2是负载的颜色:

代码语言:javascript
复制
biplot_col = function (x, y, var.axes = TRUE,col1,col2, cex = 0.8, 
    xlabs = NULL, ylabs = NULL, expand = 1, xlim = NULL, ylim = NULL, 
    arrow.len = 0.1, main = NULL, sub = NULL, xlab = NULL, ylab = NULL, 
    ...) 
{
    n <- nrow(x)
    p <- nrow(y)
    xlabs <- as.character(1L:n)
    dimnames(x) <- list(xlabs, dimnames(x)[[2L]])
    ylabs <- dimnames(y)[[1L]]
    ylabs <- as.character(ylabs)
    dimnames(y) <- list(ylabs, dimnames(y)[[2L]])
    
    unsigned.range <- function(x) c(-abs(min(x, na.rm = TRUE)), 
        abs(max(x, na.rm = TRUE)))
    rangx1 <- unsigned.range(x[, 1L])
    rangx2 <- unsigned.range(x[, 2L])
    rangy1 <- unsigned.range(y[, 1L])
    rangy2 <- unsigned.range(y[, 2L])
    if (missing(xlim) && missing(ylim)) 
        xlim <- ylim <- rangx1 <- rangx2 <- range(rangx1, rangx2)
    else if (missing(xlim)) 
        xlim <- rangx1
    else if (missing(ylim)) 
        ylim <- rangx2
    ratio <- max(rangy1/rangx1, rangy2/rangx2)/expand
    on.exit(par(op))
    op <- par(pty = "s")
    if (!is.null(main)) 
        op <- c(op, par(mar = par("mar") + c(0, 0, 1, 0)))
    plot(x, type = "n", xlim = xlim, ylim = ylim, col = col1, 
        xlab = xlab, ylab = ylab, sub = sub, main = main, ...)
    text(x, xlabs, cex = cex[1L], col = col1, ...)
    par(new = TRUE)
    dev.hold()
    on.exit(dev.flush(), add = TRUE)
    plot(y, axes = FALSE, type = "n", xlim = xlim * ratio, ylim = ylim * 
        ratio, xlab = "", ylab = "", col = col1, ...)
    axis(3, col = col2, ...)
    axis(4, col = col2, ...)
    box(col = col1)
    text(y, labels = ylabs, cex = cex[2L], col = col2, ...)
    if (var.axes) 
        arrows(0, 0, y[, 1L] * 0.8, y[, 2L] * 0.8, col = col2, 
            length = arrow.len)
    invisible()
}

然后像这样绘制:

代码语言:javascript
复制
lam <- fit$sdev[1:2]
lam <- lam * sqrt(fit$n.obs)
scores <- fit$scores
species2col = c("#c15050","#d97642","#d49d42")
names(species2col) = unique(iris$Species)
col1 = species2col[as.character(iris$Species)]
col2 = "#693c72"

par(mar=rep(2.2,4))
biplot_col(t(t(scores[,1:2])/lam), t(t(fit$loadings[,1:2]) * lam),
           col1 = col1, col2 = col2)

票数 0
EN

Stack Overflow用户

发布于 2021-03-16 02:16:26

不幸的是,它不支持它。你必须编写自己的biplot函数,并添加一个可能性,以便为每个样本赋予不同的颜色,源代码相当直接。

https://github.com/SurajGupta/r-source/blob/master/src/library/stats/R/biplot.R

或者使用更现代化的功能,例如autoplot

代码语言:javascript
复制
autoplot( fit, data=iris, colour="Species", loadings=TRUE )

如下所示:

https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66643056

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档