Q: ggbiplot _ Foreground arrows; change arrow color

I have a large dataset and am trying to build a PCA. I'm pretty happy with the plot I got, but I would like to change a couple of things:

arrows: these appear to be in the background and are covered by data points. 1) how can i get around them? 2) how can i change the color and size?

ellipses: 3) how to make the lines thicker?

legend 4) how to put the upper part of the plot correctly?

Thanks in advance!

This is how I got this graph:

g <- ggbiplot(LS_3.pca, choices = 1:2, scale = 1, pc.biplot =
       TRUE, obs.scale = 1, var.scale = 1, groups =
       LS.loc, ellipse = TRUE, ellipse.prob = 0.98, labels =
       NULL, labels.size = 5, alpha = 0.25, circle
     = FALSE, circle.prob = 0.69, varname.size = 5,
     varname.adjust = 1.5, varname.abbrev = FALSE, var.axes = TRUE)
g <- g + xlim(-8, 6) + ylim(-4, 6)
g <- g + theme_classic()

print(g)

      

+3


source to share


2 answers


ggbiplot returns an object of the "ggplot" class. If you examine this object, you will find that it contains a list called "layers". The geom_segment level is displayed in front of the geom_point layer. You want to change the order. If the geom_segment layer is layer 2 of 4, you can do (for example):

myplot$layers <- c(myplot$layers, myplot$layers[[2]])

      

This will add an extra layer (which draws arrows) at the end of the list, and this layer will be drawn after the dots are drawn (on the previous layer). This hack is sufficient to provide the required functionality.



EDIT: I understand that this answer only addresses the question of putting arrows in front. This was the problem I ran into and so I am offering my solution here for others. To fix other problems, I suggest entering in the console:

ggbiplot

      

without parentheses in which the body of the function will appear. Cut-paste into a new R script that defines a new function (like my_ggbiplot) and hack to get what you want. It's not a long function, and it's pretty easy to figure out what everything is doing. However, for the foreground arrow, the one-line fix above will work.

+2


source


I was disappointed with the limitations ggbiplot

, so I copied sections of code from the function for use in a standard call ggplot

.

# Load data
DATA <- data.frame(iris)

# Do PCA
PCA <- prcomp(iris[,1:4])

# Extract PC axes
PCAvalues <- data.frame(Species = iris$Species, PCA$x)

# Extract loadings of the variables
PCAloadings <- data.frame(Variables = rownames(PCA$rotation), PCA$rotation)

# Calculate the angles and the label offset
PCAloadings$Angle = ((180/pi) * atan(PCAloadings$PC2/PCAloadings$PC1))
PCAloadings$Offset <- ((-2 * sign(PCAloadings$PC1))/2)

# Plot
ggplot(PCAvalues, aes(x = PC1, y = PC2, colour = Species)) +
  stat_ellipse(level = 0.95, size = 2, show.legend = FALSE) +
  geom_point(size = 3) +
  geom_segment(data = PCAloadings, aes(x = 0, y = 0, xend = (PC1), yend = (PC2)),
     arrow = arrow(length = unit(1/2, "picas")), color = "black") +
 geom_text(data = PCAloadings, aes(label = Variables, x = (PC1), y = (PC2)), 
    color = "black", size = 4, angle = PCAloadings$Angle, hjust = 
    PCAloadings$Offset) +
      theme_classic() +
      theme(legend.justification = c(1,1), legend.position = c(1,1))

      

Answers:

Q1. - as @Andrew John Lowe says in his answer, the layers are created in the order you enter them, so if the line is geom_segment

last, it will appear on top.



Q2. - change the color of the arrow using the argument colour

in the call geom_segment

. Change the font of the size

labels in the call geom_text

.

Q3. - change the argument size

in the call again stat_ellipse

.

Q4. - the call will theme

resolve it, c(1,1)

indicates where the legend should appear on the chart, 0

left and 1

right.

This is not entirely ideal, as you have to PCAloadings$Offset

manually specify the arrows for the labels, and it may take a little trial and error to get the desired distance, or even require you to enter custom numbers into the framework for each label. However, you can now use the rest ggplot

as usual.

0


source







All Articles