我正在这个
github repo中开发新版本的
ggbiplot
包,并且已经
为各个组中的点填充数据椭圆的问题。这在
这个问题。也许这里有人能看到什么
问题是以及如何解决它。
ggbiplot.r
第261-268行附近
我将使用 geom_path()
的代码替换为 geom_polygon()
,以便可以填充组的省略号。然而,在测试中,这没有效果(省略号未填充)。这里出了什么问题?
我想让这个可选,所以我添加一个参数,
ellipse.fill = T / F / alpha
,但我必须先让它工作。
我已经查看了相关问题,Fill superimulated ellipses in ggplot2 scatterplots但在我的例子中,我在内部计算椭圆。
代码片段是:
g <- g + geom_polygon(data = ell,
aes(color = groups,
fill = groups
),
alpha = 0.4, # MF: why doesn't this have any effect?
linewidth = ellipse.linewidth)
我的测试用例:
remotes::install_github("friendly/ggbiplot")
library(ggbiplot)
data(wine, package="ggbiplot")
wine.pca <- prcomp(wine, scale. = TRUE)
ggbiplot(wine.pca, groups=wine.class,
ellipse = TRUE,
ellipse.linewidth = 1.2,
circle = TRUE,
varname.color = "darkred",
varname.size = 4) +
theme_minimal() +
theme(legend.direction = 'horizontal', legend.position = 'top')
这会产生:
您的问题是由每组中都有省略号的双重副本引起的。每个椭圆有 100 行数据,但每个椭圆圆周周围只有 50 个点。这意味着每个椭圆周围有两圈纵坐标,并且根据多边形填充规则,它们会相互抵消。我们可以通过过滤掉多边形层中每个椭圆的后 50 行来显示这一点:
p <- ggbiplot(wine.pca, groups=wine.class,
ellipse = TRUE,
ellipse.linewidth = 1.2,
circle = TRUE,
varname.color = "darkred",
varname.size = 4) +
theme_minimal() +
theme(legend.direction = 'horizontal', legend.position = 'top')
p$layers[[4]]$data <- p$layers[[4]]$data[c(1:50, 101:150, 201:250),]
p
您没有在问题中包含每个椭圆的计算,但问题出在这个计算中。解决这个问题,你的饱腹感就会恢复。