behrooz Akbari
behrooz Akbari

Reputation: 19

How do I resolve the overlap in fviz_pca_biplot in factoextra packages

In the following command, whose shape output is also known, I have the problem of overlap in the specified area. I want all correlation lines to have labels without overlapping.

I used the geom_label_repel command, but I got an error.

fviz_pca_biplot(PCA.Res01, # Individuals
                                    geom.ind = "point",
                                    fill.ind = DataPCA01$CitysNumb,
                                    pointshape = 21, pointsize = 1,
                                    palette = "ucscgb",
                                    repel = TRUE,
                                    addEllipses = TRUE,
                                    ellipse.level=0.95,
                                    labelsize = 6, # Variables
                                    col.var = "contrib",
                                    gradient.cols = c("blue", "red", "black"), ggtheme = theme_minimal())+
  labs(fill = "Citys", color = "Correlation", caption = "(Logical Value = 0.95)")+ # Change legend title
  theme(text = element_text(size = 14),
        axis.title = element_text(size = 14, face = 'bold'),
        axis.text = element_text(size = 14, face = 'bold'))

Output: graph output

I applied the following command:

+ geom_label_repel(aes(label = names, label.size = 6), min.segment.length = 0.5)

But it was not done and it showed the following error:

Don't know how to automatically pick scale for object of type <function>. Defaulting to continuous.
Error in `geom_label_repel()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 10th layer.
Caused by error in `compute_aesthetics()`:
! Aesthetics are not valid data columns.
✖ The following aesthetics are invalid:
✖ `label = names`
ℹ Did you mistype the name of a data column or forget to add `after_stat()`?
Run `rlang::last_error()` to see where the error occurred.
Warning message:
In geom_label_repel(aes(label = names, label.size = 6), min.segment.length = 0.5):
   Ignoring unknown aesthetics: label.size

I know that the above command cannot be used.

My question is, is there another command that can change the overlay more easily and conveniently?

Upvotes: 1

Views: 560

Answers (1)

I have a similar problem with fviz. For PCA it doesn´t recognize the label neither the text for ggrepel, so you can't use it right away.

What you can do is create your own labels, for that you omit the labels text and then you call the text from another database, for example:

    dat1 <- facto_summarize(PCA.Res01, element = "ind", axes = c(1, 2), result = c("coord", "contrib", "cos2"))
    
    fviz_pca_biplot(PCA.Res01, # Individuals
                                        geom.ind = "point",
                                        geom.var = "arrow",
                                        fill.ind = DataPCA01$CitysNumb,
                                        pointshape = 21, pointsize = 1,
                                        palette = "ucscgb",
                                        repel = TRUE,
                                        addEllipses = TRUE,
                                        ellipse.level=0.95,
                                        labelsize = 6, # Variables
                                        col.var = "contrib",
                                        gradient.cols = c("blue", "red", "black"), ggtheme = theme_minimal()) +
  geom_text_repel(data = dat1, 
                  aes(x=Dim.1,y=Dim.2, label = rownames(dat1)), 
                  size = 5,
                  box.padding = 1,
                  nudge_y = 3,
                  nudge_x = 2)

Upvotes: 0

Related Questions