Julio Diaz Julio Diaz - 1 month ago 20
R Question

Customize border of ggbiplot points

Given the following code using the ggbiplot library available via

devtools::install.github()
:

library(ggbiplot)
data(iris)
log.ir <- log(iris[, 1:4])
ir.species <- iris[, 5]

ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE)

g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.species)
g <- g + theme(legend.direction = 'vertical', legend.position = 'right')
g <- g + scale_color_manual(values=c("blue", "red", "green"))
print(g)


what is the best way to customize the border of the data points based on the grouping? I used scale_color_manual() to customize the color of those data points, but I can't think of a way to do that for the border.

Thanks

Answer

Assuming you want to adjust the border of the data points themselves...

The ggbiplot() call itself won't give you this flexibility but setting alpha = 0 will make the points plotted by ggbiplot invisible or really 100% transparent. Then you can make a separate layer with a geom_point() call where you specify shape as one of the 5 shapes (21-25) that have a fill (the middle) and a color (the boarder) aesthetic.

ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.species, alpha = 0) +
    theme(legend.direction = 'vertical', legend.position = 'right') + 
    scale_color_manual(values=c("blue", "red", "green")) +
    scale_fill_manual(values = c("red", "green", "blue")) + # just offset by one to show
    geom_point(size = 3, shape = 21, aes(fill = groups, color = groups))

enter image description here

PS It's probably always a good idea to include in your question that the package you are using is only available via devtools::install.github() and not the standard install.packages()