rbiplotfactoextra

How do I label only a subset of points in a biplot?


I am generating biplots using fviz_pca_biplot in factoextra. The following code provides an example of my datset and the biplots. I'd like to label a subset of points, rather than all of them. For example, only species in a single genus like Eucalyptus. I can't figure out how, or even if it is possible. Can anyone suggest a solution?


data <- structure(list(Specific.gravity = c(0.42, 0.43, 0.41, 0.35, 0.39, 
                                            1.04, 1.2, 1.23, 0.67, 0.57, 0.65, 1.24, 1.15, 0.48, 0.48, 0.55, 
                                            0.62, 0.57, 0.5, 0.54, 0.55, 0.51, 0.47, 0.63, 0.71, 0.38, 0.79, 
                                            0.78, 0.84, 0.73, 0.9, 0.41, 0.48, 0.8, 1, 0.56, 0.62, 0.62, 
                                            0.71, 0.65, 0.6, 0.55, 0.55, 0.97, 0.95, 1.2, 0.98, 0.81, 0.35, 
                                            0.37, 0, 0.72, 0.66, 0.75, 0.75, 0.66, 0.66, 0.69, 0.69, 0.6, 
                                            0.8, 0.72, 0.72, 0.48, 0.43, 1.29, 0.48, 0.53, 0.8, 0.43, 0.35, 
                                            0.98, 0.85, 0.87, 0.8, 0.85, 0.92, 1.31, 1.07, 1.04, 0.98, 0.76, 
                                            0.78, 0.72, 0.65, 0.98, 0.9, 1.13, 0.9, 0.8, 1.03, 0.77, 0.99, 
                                            0.82, 0.85, 0.68, 0.78, 1.1, 0.9, 0.66), Modulus.of.elasticity.Mpa = c(10552, 
                                                                                                                   12100, 8620, 10000, 10300, 18500, 18500, 21500, 10830, 11060, 
                                                                                                                   13000, 21500, 18500, 10000, 10000, 7240, 11189, 11200, 8300, 
                                                                                                                   11300, 11327, 7894, 7900, 12600, 12620, 8130, 11860, 12100, 14000, 
                                                                                                                   9356, 14000, 9500, 9520, 8480, 16580, 9100, 13900, 13900, 14906, 
                                                                                                                   15000, 10974, 11000, 7816, 15600, 16300, 16300, 16775, 12100, 
                                                                                                                   7170, 7200, 8920, 7375, 12300, 15600, 15600, 11900, 11900, 13000, 
                                                                                                                   13000, 11700, 14900, 14900, 15300, 8480, 8500, 21500, 9230, 8200, 
                                                                                                                   15760, 11700, 6423, 14560, 12710, 11860, 10640, 17000, 12280, 
                                                                                                                   20550, 12970, 17200, 12454, 9875, 14151, 11720, 10940, 19000, 
                                                                                                                   11000, 18500, 19000, 16505, 16000, 13450, 23000, 13000, 13000, 
                                                                                                                   14020, 12100, 17000, 15230, 11572), Modulus.of.rupture.Mpa = c(66, 
                                                                                                                                                                                  75.8, 53, 63, 68, 130, 130, 150, 77, 88, 99, 150, 130, 74, 74, 
                                                                                                                                                                                  55, 92, 92, 66, 92, 92, 62, 61, 109, 109, 53, 121, 78, 94, 98, 
                                                                                                                                                                                  94, 68, 68, 72, 127, 55, 114, 114, 116, 117, 86, 85, 67, 137, 
                                                                                                                                                                                  149, 149, 165, 78, 55, 55, 66, 83, 118, 139, 139, 94, 94, 125, 
                                                                                                                                                                                  125, 114, 139, 139, 132, 59, 59, 150, 80, 76, 123, 88, 47, 145, 
                                                                                                                                                                                  109, 109, 105, 125, 117, 213, 130, 167, 76, 54, 125, 111, 105, 
                                                                                                                                                                                  179, 101, 130, 132, 115, 125, 98, 150, 112, 99, 97, 78, 142, 
                                                                                                                                                                                  114, 101), MOR.MOE = c(0.625473843821077, 0.626446280991736, 
                                                                                                                                                                                                         0.614849187935035, 0.63, 0.660194174757281, 0.702702702702703, 
                                                                                                                                                                                                         0.702702702702703, 0.697674418604651, 0.710987996306556, 0.795660036166365, 
                                                                                                                                                                                                         0.761538461538461, 0.697674418604651, 0.702702702702703, 0.74, 
                                                                                                                                                                                                         0.74, 0.759668508287293, 0.822236124765395, 0.821428571428571, 
                                                                                                                                                                                                         0.795180722891566, 0.814159292035398, 0.812218592743003, 0.785406637952876, 
                                                                                                                                                                                                         0.772151898734177, 0.865079365079365, 0.863708399366086, 0.651906519065191, 
                                                                                                                                                                                                         1.02023608768971, 0.644628099173554, 0.671428571428571, 1.04745617785378, 
                                                                                                                                                                                                         0.671428571428571, 0.715789473684211, 0.714285714285714, 0.849056603773585, 
                                                                                                                                                                                                         0.765983112183353, 0.604395604395604, 0.820143884892086, 0.820143884892086, 
                                                                                                                                                                                                         0.778210116731518, 0.78, 0.78367049389466, 0.772727272727273, 
                                                                                                                                                                                                         0.857215967246673, 0.878205128205128, 0.914110429447853, 0.914110429447853, 
                                                                                                                                                                                                         0.983606557377049, 0.644628099173554, 0.767085076708508, 0.763888888888889, 
                                                                                                                                                                                                         0.739910313901345, 1.12542372881356, 0.959349593495935, 0.891025641025641, 
                                                                                                                                                                                                         0.891025641025641, 0.789915966386555, 0.789915966386555, 0.961538461538462, 
                                                                                                                                                                                                         0.961538461538462, 0.974358974358974, 0.932885906040268, 0.932885906040268, 
                                                                                                                                                                                                         0.862745098039216, 0.695754716981132, 0.694117647058824, 0.697674418604651, 
                                                                                                                                                                                                         0.866738894907909, 0.926829268292683, 0.780456852791878, 0.752136752136752, 
                                                                                                                                                                                                         0.731745290362759, 0.995879120879121, 0.857592446892211, 0.919055649241147, 
                                                                                                                                                                                                         0.986842105263158, 0.735294117647059, 0.952768729641694, 1.03649635036496, 
                                                                                                                                                                                                         1.00231303006939, 0.970930232558139, 0.610245704191424, 0.546835443037975, 
                                                                                                                                                                                                         0.883329800014133, 0.947098976109215, 0.959780621572212, 0.942105263157895, 
                                                                                                                                                                                                         0.918181818181818, 0.702702702702703, 0.694736842105263, 0.696758558012723, 
                                                                                                                                                                                                         0.78125, 0.728624535315985, 0.652173913043478, 0.861538461538461, 
                                                                                                                                                                                                         0.761538461538461, 0.691868758915835, 0.644628099173554, 0.835294117647059, 
                                                                                                                                                                                                         0.748522652659225, 0.872796405115797)), row.names = c("Abies_amabilis", 
                                                                                                                                                                                                                                                               "Abies_amabilis__a", "Abies_balsamea", "Abies_balsamea__a", "Abies_concolor", 
                                                                                                                                                                                                                                                               "Acacia_acuminata", "Acacia_aneura", "Acacia_grasby", "Acacia_koa", 
                                                                                                                                                                                                                                                               "Acacia_mearnsii", "Acacia_melanoxylon", "Acacia_papyrocarpa", 
                                                                                                                                                                                                                                                               "Acacia_pruinocarpa", "Acer_macrophyllum", "Acer_macrophyllum__a", 
                                                                                                                                                                                                                                                               "Acer_negundo", "Acer_nigrum", "Acer_nigrum__a", "Acer_pseudoplatanus", 
                                                                                                                                                                                                                                                               "Acer_rubrum", "Acer_rubrum__a", "Acer_saccharinum", "Acer_saccharinum__a", 
                                                                                                                                                                                                                                                               "Acer_saccharum", "Acer_saccharum__a", "Aesculus_octandra", "Afzelia_xylocarpa", 
                                                                                                                                                                                                                                                               "Agonis_flexuosa", "Allocasuarina_decussata_", "Allocasuarina_fraseriana", 
                                                                                                                                                                                                                                                               "Allocasuarina_huegeliana", "Alnus_sp", "Alnus_sp__a", "Arbutus_menziesii", 
                                                                                                                                                                                                                                                               "Astronium_fraxinifolium", "Banksia_seminuda", "Betula_alleghaniensis", 
                                                                                                                                                                                                                                                               "Betula_alleghaniensis__a", "Betula_lenta", "Betula_lenta__a", 
                                                                                                                                                                                                                                                               "Betula_papyrifera", "Betula_papyrifera__a", "Betula_populifolia", 
                                                                                                                                                                                                                                                               "Borassus_flabellifer", "Brosimum_paraense", "Brosimum_rubescens", 
                                                                                                                                                                                                                                                               "Caesalpinia_ferrea", "Callistachys_lanceolata", "Calocedrus_decurrens", 
                                                                                                                                                                                                                                                               "Calocedrus_decurrens__a", "Cardwellia_sublimis", "Carpinus_caroliniana", 
                                                                                                                                                                                                                                                               "Carya_cordiformis", "Carya_glabra", "Carya_glabra__a", "Carya_illinoinensis", 
                                                                                                                                                                                                                                                               "Carya_illinoinensis__a", "Carya_laciniosa", "Carya_laciniosa__a", 
                                                                                                                                                                                                                                                               "Carya_myristiciformis", "Carya_ovata", "Carya_ovata__a", "Carya_tomentosa", 
                                                                                                                                                                                                                                                               "Castanea_dentata", "Castanea_dentata__a", "Casuarina_pauper", 
                                                                                                                                                                                                                                                               "Cedrela_odorata", "Celtis_occidentalis", "Centrolobium_microchaete", 
                                                                                                                                                                                                                                                               "Chamaecyparis_lawsoniana", "Chamaecyparis_thvoides", "Chloroxylon_swietenia", 
                                                                                                                                                                                                                                                               "Cocos_nucifera", "Cordia_dodecandra", "Cornus_florida", "Corymbia_calophylla", 
                                                                                                                                                                                                                                                               "Dalbergia_latifolia", "Dalbergia_melanoxylon", "Dalgergia_retusa", 
                                                                                                                                                                                                                                                               "Diospyros_crassiflora", "Diospyros_kursii", "Diospyros_melanida", 
                                                                                                                                                                                                                                                               "Diospyros_virginiana", "Entandrophragma_cylindricum", "Erythroxylon_mexicanum", 
                                                                                                                                                                                                                                                               "Eucalyptus_astringens", "Eucalyptus_camaldulensis", "Eucalyptus_clelandii", 
                                                                                                                                                                                                                                                               "Eucalyptus_diversicolor__a", "Eucalyptus_globulus", "Eucalyptus_gomphocephala", 
                                                                                                                                                                                                                                                               "Eucalyptus_jacksonii", "Eucalyptus_maculata", "Eucalyptus_marginata", 
                                                                                                                                                                                                                                                               "Eucalyptus_patens", "Eucalyptus_regnans", "Eucalyptus_rudis", 
                                                                                                                                                                                                                                                               "Eucalyptus_wandoo", "Euxylophora_paraensis", "Fagus_grandifolia_or_Fagus_americana"
                                                                                                                                                                                                         ), class = "data.frame")
  
res.pca <- prcomp(data, scale = T, center = T)

fviz_pca_biplot(res.pca)


Solution

  • The object from fviz_pca_biplot() is a ggplot object. This means you can add to it with additional commands preceded by the +.

    Using your data and starting from the res.pca object.

    library(tidyverse)
    

    First remove all labels.

    p<- fviz_pca_biplot(res.pca, label = "none")
    

    Then get labels from the plot object.

    plotnames <- p$data$name %>% as.character()
    

    Then use some logic to make most empty, but retain the ones you want to add to the graph.

    mylabels <- sapply(plotnames, function(x) ifelse(is.na(str_locate(x,"Eucalyptus")[1]), "", x)) %>% as.vector()
    

    Last, add to the original plot object your new label vector

    p + geom_text(aes(label = mylabels))
    

    enter image description here