1

I use ggbiplot regularly and can control every aspect of the plot produced using ggplot2 tools, since it inherits from ggplot2...

In ggplot2, I usually control the number of columns in the legend with a line of the form:

ggplot2::guides(fill=ggplot2::guide_legend(ncol=2))

However, this does not seem to work in ggbiplot (while everything else ggplot2-related does work).

Please check the MWE below with iris data, the only thing I want to do here is specify 2 columns for the legend (for illustration purposes, I know there are only 3 Species levels, but this was the example I had more at hand).

library(ggbiplot)
data(iris)
pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
P <- ggbiplot(pca.obj,
              obs.scale = 1,
              var.scale=1,
              ellipse=T,
              circle=F,
              varname.size=3,
              var.axes=T,
              groups=iris$Species, #no need for coloring, I'm making the points invisible
              alpha=0) + #invisible points, I add them below
ggplot2::theme_light() +
ggplot2::scale_color_manual("spec", values=c("red","black","pink"), guide=ggplot2::guide_legend(override.aes=list(shape=19, size=5, linetype=0))) +
ggplot2::guides(fill=ggplot2::guide_legend(ncol=2)) #THIS DOES NOT WORK HERE, WHY?
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test.png", height=600, width=600)
print(#or ggsave()
  P
)
dev.off()

This produces the following biplot:

test

See how the number of columns in the legend never changes... Is there a way to specify the number of legend columns in ggbiplot? Thanks

DaniCee
  • 2,397
  • 6
  • 36
  • 59
  • 1
    I haven't tried your code, but it seems like you map **color** to 'Species' (`groups=iris$Species`, `scale_color_manual`), but then you try to edit a **fill** scale (`guides(fill=`). Can you try `guides(color=`? – Henrik Jul 23 '20 at 09:55
  • that's just pretty much to prove that I can modify other aspects in the legend with `ggplot2` tools, that part works. I don't care about the coloring, it's just the legend `ncol` that doesn't work... – DaniCee Jul 23 '20 at 09:59
  • 1
    What I'm trying to say is that you _don't have a fill legend_ - you have a _color_ legend - but when you try to modify `ncol`, you refer to a (non-existing) _fill_ legend (`guides(fill=`). – Henrik Jul 23 '20 at 10:11
  • Also, the `alpha=0` + `P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers)` may be replaced by `geom_point(aes(color = iris$Species), size = 3)` – Henrik Jul 23 '20 at 11:06
  • Should I add a `scale_fill_manual` entry then? As for your second point, I really need to play with layers like that when doing these biplots, otherwise the points are drawn on top of the vector arrows masking them completely if there are many points – DaniCee Jul 24 '20 at 02:18
  • To reiterate my first comment: you map point _color_ to 'Species' (`groups=iris$Species`; "`groups`: If provided the points will be _colored_ according to `groups`"). Thus, you get a _color_ legend. If you want to make changes to the _color_ legend in `guides`, use `guides(**color** =`, i.e. `guides(color = guide_legend(ncol = 2))` (_not_ `guides(fill = `). No, don't add `scale_fill_manual`. Again, you have a _color_ scale, and you create your own color scale with `scale_color_manual`. – Henrik Jul 24 '20 at 09:23

1 Answers1

1

It'd be simpler to include the ncol = 2 in the first call to guide_legend(). I.e.

library(ggbiplot)
data(iris)
pca.obj <- prcomp(iris[,1:4], center=TRUE, scale.=TRUE)
P <- ggbiplot(pca.obj,
              obs.scale = 1,
              var.scale=1,
              ellipse=T,
              circle=F,
              varname.size=3,
              var.axes=T,
              groups=iris$Species, #no need for coloring, I'm making the points invisible
              alpha=0) + #invisible points, I add them below
ggplot2::theme_light() +
ggplot2::scale_color_manual("spec", values=c("red","black","pink"), guide=ggplot2::guide_legend(ncol = 2, override.aes=list(shape=19, size=5, linetype=0))) # +
# ggplot2::guides(fill=ggplot2::guide_legend(ncol=2)) #THIS DOES NOT WORK HERE, WHY?
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test.png", height=600, width=600)
print(#or ggsave()
  P
)
dev.off()

enter image description here

As Henrik says, it's down to a mismatch between fill = and color = .

eugene100hickey
  • 351
  • 1
  • 3
  • yes this is it! I thought I had tried it before, but I guess I probably included `ncol` inside the `override.aes=list()` – DaniCee Jul 24 '20 at 02:21