I'm working on a new version of the ggbiplot
package in this github repo and have
a problem with filling data ellipses for the points in various groups. This is described in
this issue. Perhaps someone here can see what the
problem is and how to solve it.
In ggbiplot.r
around lines 261-268
I replaced code using geom_path()
with geom_polygon()
so that the ellipses for the groups could be filled. Yet, in testing, this has no effect (ellipses are unfilled). What is wrong here?
I want to make this optional, so I'd add an argument, ellipse.fill = T / F / alpha
, but I have to get this working first.
I've looked at the related question, Fill superimposed ellipses in ggplot2 scatterplots but in my case I'm calculating the ellipses internally.
The snippet of code is:
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)
My test case:
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')
This produces:
Your problem is caused by having a double copy of the ellipses in each group. There are 100 rows of data for each ellipse, but only 50 points around each ellipse circumference. This means there are two "laps" of vo-ordinates around each ellipse, and by polygon filling rules, these cancel each other out. We can show this by filtering out the second 50 rows of each ellipse in the polygon layer:
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
You haven't included the calculation for each ellipse in your question, but it is in this calculation that the problem lies. Fix this, and your fill will be restored.