如何从情节中删除特定组但情节在 R 中保持不变?
How to remove specific group from a plot but plot stays the same in R?
data('iris')
pca.irix <- PCA(iris[ ,1:4])
gg <- factoextra::fviz_pca_biplot(X = pca.irix,
# samples
fill.ind = iris$Species, col.ind = 'black',
pointshape = 21, pointsize = 1.5,
geom.ind = 'point', repel = T,
geom.var = FALSE )
我想获得一个与上面的图完全一样的图,但没有 specie setosa
。
我开始这样做,但不知道如何继续
setosa_wo <- iris %>%
filter(Species != 'setosa')
gg + scale_x_continuous(limits = c((-2), 2)) + scale_y_continuous(limits = c((-2), 2))
如何从绘图中删除彩色组?但情节应该保持不变。
从图中删除一个或任意数量的组的一种方法是过滤用于图层的数据,例如查看 gg$layers
表明您的 PCA 图由六层组成,但是只有前两层中的组用作填充颜色。因此,我简单地过滤了这两层的数据,这给了我一个删除了 setosa
的图。
EDIT 按照 @DaveArmstrong 的建议,我添加了他的代码以修复原始范围内的轴范围并添加原始颜色
library(FactoMineR)
library(ggplot2)
pca.irix <- PCA(iris[ ,1:4])
gg <- factoextra::fviz_pca_biplot(X = pca.irix,
# samples
fill.ind = iris$Species, col.ind = 'black',
pointshape = 21, pointsize = 1.5,
geom.ind = 'point', repel = T,
geom.var = FALSE )
# First: Get the ranges
yrg <- ggplot2::layer_scales(gg)$y$range$range
xrg <- ggplot2::layer_scales(gg)$x$range$range
# Filter the data
gg$layers[[1]]$data <- dplyr::filter(gg$layers[[1]]$data, Fill. != "setosa")
gg$layers[[2]]$data <- dplyr::filter(gg$layers[[2]]$data, Fill. != "setosa")
gg +
# Set the limits to the original ones
ggplot2::coord_cartesian(xlim=xrg, ylim=yrg, expand=FALSE) +
# Add orignial colors
ggplot2::scale_fill_manual(values = scales::hue_pal()(3)[2:3])
由 reprex package (v0.3.0)
于 2020-10-16 创建
data('iris')
pca.irix <- PCA(iris[ ,1:4])
gg <- factoextra::fviz_pca_biplot(X = pca.irix,
# samples
fill.ind = iris$Species, col.ind = 'black',
pointshape = 21, pointsize = 1.5,
geom.ind = 'point', repel = T,
geom.var = FALSE )
我想获得一个与上面的图完全一样的图,但没有 specie setosa
。
我开始这样做,但不知道如何继续
setosa_wo <- iris %>%
filter(Species != 'setosa')
gg + scale_x_continuous(limits = c((-2), 2)) + scale_y_continuous(limits = c((-2), 2))
如何从绘图中删除彩色组?但情节应该保持不变。
从图中删除一个或任意数量的组的一种方法是过滤用于图层的数据,例如查看 gg$layers
表明您的 PCA 图由六层组成,但是只有前两层中的组用作填充颜色。因此,我简单地过滤了这两层的数据,这给了我一个删除了 setosa
的图。
EDIT 按照 @DaveArmstrong 的建议,我添加了他的代码以修复原始范围内的轴范围并添加原始颜色
library(FactoMineR)
library(ggplot2)
pca.irix <- PCA(iris[ ,1:4])
gg <- factoextra::fviz_pca_biplot(X = pca.irix,
# samples
fill.ind = iris$Species, col.ind = 'black',
pointshape = 21, pointsize = 1.5,
geom.ind = 'point', repel = T,
geom.var = FALSE )
# First: Get the ranges
yrg <- ggplot2::layer_scales(gg)$y$range$range
xrg <- ggplot2::layer_scales(gg)$x$range$range
# Filter the data
gg$layers[[1]]$data <- dplyr::filter(gg$layers[[1]]$data, Fill. != "setosa")
gg$layers[[2]]$data <- dplyr::filter(gg$layers[[2]]$data, Fill. != "setosa")
gg +
# Set the limits to the original ones
ggplot2::coord_cartesian(xlim=xrg, ylim=yrg, expand=FALSE) +
# Add orignial colors
ggplot2::scale_fill_manual(values = scales::hue_pal()(3)[2:3])
由 reprex package (v0.3.0)
于 2020-10-16 创建