如何在R中突出显示特定变量或个人的特定变量或个人

发布于 2025-02-13 23:38:04 字数 357 浏览 2 评论 0原文

我目前正在研究我执行PCA的大型数据集(用物种X样品计数数据)。我得到的是巨大的点云,我想为一个给定的物种上色以显示它位于云中的位置(物种是我在这里的变量)。以下是:

”在此处输入图像描述”

我使用factoextra的软件包,并将变量可视化FVIZ_PCA_VAR。有没有办法选择一种特定物种并以与其他物种不同的颜色显示?

感谢您的帮助

I am currently working on a large dataset (count data with species x samples) from which I performed a PCA. What I get is a massive cloud of points, and I would like to color one given species to show where it is located in this cloud (species are my variables here). Here is what it looks like :

enter image description here

I use the package factoextra, and visualize the variables with fviz_pca_var. Is there a way to select one particular species and display it with a color different than the others ?

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黎歌 2025-02-20 23:38:04

如果只是您要着色的一个点,也许是:

library(tidyverse)
library(factoextra)
library(FactoMineR)

data("iris")

iris$assigned_colors <- NA
# Change the color of the 'individual of interest'
iris[9,]$assigned_colors <- "red"

iris.pca <- PCA(iris[,-c(5,6)], graph = FALSE)

fviz_pca_ind(iris.pca,
             geom = "point",
             geom.ind = "point") +
  geom_point(aes(color = iris$assigned_colors)) +
  scale_color_identity()
#> Warning: Removed 149 rows containing missing values (geom_point).

“”

在2022-07-08创建的a href =“ https://reprex.tidyverse.org” rel =“ nofollow noreferrer”> reprex软件包 (v2.0.1)

您还可以使用

library(tidyverse)
library(factoextra)
library(FactoMineR)

data("iris")

iris$assigned_colors <- NA
iris[9,]$assigned_colors <- "red"

iris$labels <- NA
iris[9,]$labels <- "point of interest"

iris.pca <- PCA(iris[,-c(5,6, 7)], graph = FALSE)

fviz_pca_ind(iris.pca,
             geom = "point",
             geom.ind = "point") +
  geom_point(aes(color = iris$assigned_colors)) +
  geom_text(aes(label = iris$labels), nudge_y = -0.2) +
  scale_color_identity()
#> Warning: Removed 149 rows containing missing values (geom_point).
#> Warning: Removed 149 rows containing missing values (geom_text).

“”

If it's just a single point you want to color, perhaps:

library(tidyverse)
library(factoextra)
library(FactoMineR)

data("iris")

iris$assigned_colors <- NA
# Change the color of the 'individual of interest'
iris[9,]$assigned_colors <- "red"

iris.pca <- PCA(iris[,-c(5,6)], graph = FALSE)

fviz_pca_ind(iris.pca,
             geom = "point",
             geom.ind = "point") +
  geom_point(aes(color = iris$assigned_colors)) +
  scale_color_identity()
#> Warning: Removed 149 rows containing missing values (geom_point).

Created on 2022-07-08 by the reprex package (v2.0.1)

You can also label specific points (i.e. just the point of interest) using this approach, e.g.

library(tidyverse)
library(factoextra)
library(FactoMineR)

data("iris")

iris$assigned_colors <- NA
iris[9,]$assigned_colors <- "red"

iris$labels <- NA
iris[9,]$labels <- "point of interest"

iris.pca <- PCA(iris[,-c(5,6, 7)], graph = FALSE)

fviz_pca_ind(iris.pca,
             geom = "point",
             geom.ind = "point") +
  geom_point(aes(color = iris$assigned_colors)) +
  geom_text(aes(label = iris$labels), nudge_y = -0.2) +
  scale_color_identity()
#> Warning: Removed 149 rows containing missing values (geom_point).
#> Warning: Removed 149 rows containing missing values (geom_text).

Created on 2022-07-08 by the reprex package (v2.0.1)

禾厶谷欠 2025-02-20 23:38:04

我不会标记每个数据点。只需使用传说,并突出显示您的物种,例如。红色和所有其他物种绿色。

您没有提供示例数据,因此我为您提供了其他示例数据的解决方案。请参阅下面的代码。使用FactoExtra(和FactoMiner)为所有数值列制作PCA。然后在绘制PCA的两个维度时,添加一个因子变量作为物种的荧光笔。只需使用简单的iFelse列制作一个新的因子VAR即可将您的物种与其他物种分开。使用此因子列在FVIZ_PCA_IND图中突出显示。请参阅下面的代码:

library(FactoMineR)
library(ggplot2)
library(factoextra)

data("iris")
iris2 <- iris[1:4]
head(iris2)

# PCA analysis to get PCs
iris.pca <- PCA(iris2, scale.unit = TRUE, graph = FALSE)

# use Species from iris to change habillage
fviz_pca_ind(iris.pca, label="none", habillage = iris$Species)


library("FactoMineR")
res.pca <- PCA(df,  graph = FALSE)

iris$new_species <- as.factor(ifelse(iris$Species == "virginica", 
"my_species", "other_species"))

# Only highlight one species - rest black
fviz_pca_ind(iris.pca, label="none", habillage = 
iris$new_species)

”在此处输入图像说明”

I would not label every data point. Just use a legend and highlight your species with, eg. red color, and all other species green.

You did not provide example data, so I give you a solution with other sample data. See the code below. Using factoextra (and factominer) make your pca for all numerical columns. Then add a factor variable as a highlighter of your species when plotting the 2 dimensions of the PCA. Just make a new factor var with a simple ifelse column to separate your species from the rest. Use this factor column for highlighting in the fviz_pca_ind plot. See code below for an example:

library(FactoMineR)
library(ggplot2)
library(factoextra)

data("iris")
iris2 <- iris[1:4]
head(iris2)

# PCA analysis to get PCs
iris.pca <- PCA(iris2, scale.unit = TRUE, graph = FALSE)

# use Species from iris to change habillage
fviz_pca_ind(iris.pca, label="none", habillage = iris$Species)


library("FactoMineR")
res.pca <- PCA(df,  graph = FALSE)

iris$new_species <- as.factor(ifelse(iris$Species == "virginica", 
"my_species", "other_species"))

# Only highlight one species - rest black
fviz_pca_ind(iris.pca, label="none", habillage = 
iris$new_species)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文