3

By default, the ggbiplot function gives a graph with loadings as red arrows and unit labels in black:

library(ggbiplot)
data("USArrests")
us <- princomp(USArrests)
ggbiplot(us, labels = rownames(us$scores))

Here is the result of that code

How can I change the colour of these labels, and incidentally their size or font?

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58

1 Answers1

1
library(ggbiplot)
library(grid)
data("USArrests")
us <- princomp(USArrests)

# Cut the third score into 4 intervals using quantiles
cols <- cut(us$scores[,3], quantile(us$scores[,3], probs=seq(0,1,0.25)), include.lowest=T)

# Change label colors using the "group" option
# Change label font size using the "label.size" option
p <- ggbiplot(us, labels = rownames(us$scores), groups=cols, labels.size=4)

# Change label font family 
g <- ggplotGrob(p)
g$grobs[[6]]$children[[4]]$gp$fontfamily <- "mono"
grid.draw(g)

enter image description here

To change label colors as a whole:

p <- ggbiplot(us, labels = rownames(us$scores), groups=1, labels.size=4) +
     theme(legend.position = "none")

# Change label colors 
g <- ggplotGrob(p)
g$grobs[[6]]$children[[4]]$gp$col <- "#FF9900"
grid.draw(g)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
  • I think what I was looking for were the _"group"_ and _"labels.size"_ options. Thank you! – KermittDuss Aug 23 '17 at 11:33
  • update: in the end, "group" doesn't seem to be the right parameter, because you need, well, groups for it to work. i would like to change the colour of labels as a whole, not depending on a factor or values. Maybe my question wasn't precise enough – KermittDuss Aug 24 '17 at 11:44