How do I add vector labels?

Hi,

I've had assistance creating this stupidly complex NMDS plot. Because I am still learning how to use vectors, I don't know how to add the species name to their respective vectors (it should be the top 12 most abundant species). I was hoping that one of you awesome people could please help me understand how to add text to vectors. If it helps, I've attached the dput and codes below. Thanks in advance. Have a great rest of your day!

Cheers,
Terrible_Coder


DPUT

structure(list(c__Actinobacteria = c(25.533, 33.218, 39.028, 
37.187, 36.828, 34.838, 28.097, 27.552, 29.992, 22.224, 32.853, 
29.845), c__Alphaproteobacteria = c(15.81, 16.29, 19.782, 20.998, 
19.288, 21.472, 23.169, 22.302, 18.005, 18.726, 15.635, 18.228
), c__Betaproteobacteria = c(13.048, 11.506, 8.502, 6.898, 7.147, 
6.977, 7.033, 6.618, 2.659, 3.414, 2.991, 4.203), c__Deltaproteobacteria = c(8.608, 
6.485, 3.675, 4.092, 6.175, 5.218, 7.93, 6.87, 4.883, 2.026, 
3.543, 2.482), c__Gammaproteobacteria = c(5.402, 5.049, 3.326, 
3.693, 5.416, 5.311, 6.582, 5.933, 3.599, 2.87, 2.701, 3.819), 
    c__Sphingobacteriia = c(3.468, 3.674, 3.033, 3.655, 2.499, 
    1.888, 1.923, 2.215, 1.89, 1.498, 1.154, 2.422), c__Acidobacteria_Gp6 = c(3.364, 
    2.445, 2.282, 2.601, 2.179, 2.739, 2.305, 2.756, 2.554, 2.763, 
    2.669, 3.454), c__Bacteria_unclassified = c(2.38, 1.869, 
    1.579, 1.247, 2.3, 2.108, 1.36, 1.193, 3.126, 1.885, 2.987, 
    2.37), c__Gemmatimonadetes = c(1.955, 1.482, 0.903, 0.881, 
    0.968, 0.91, 0.512, 0.615, 4.553, 3.568, 3.094, 1.909), c__Chloroflexi_unclassified = c(1.918, 
    2.012, 1.989, 2.041, 2.782, 3.152, 3.559, 3.14, 2.747, 1.918, 
    3.323, 3.736), c__Proteobacteria_unclassified = c(1.788, 
    1.457, 1.166, 1.046, 1.26, 1.059, 1.13, 1.313, 0.869, 0.547, 
    1.701, 2.474), c__Gemmatimonadetes_unclassified = c(1.574, 
    0.626, 0.31, 0.312, 0.566, 0.529, 0.494, 0.556, 1.232, 0.539, 
    0.904, 0.836), c__Flavobacteriia = c(1.545, 2.127, 0.92, 
    0.907, 0.523, 0.268, 1.042, 1.298, 0.274, 0.231, 0.098, 0.049
    ), c__Acidobacteria_Gp3 = c(1.264, 1.152, 0.586, 0.703, 0.911, 
    1.129, 0.645, 0.714, 0.749, 0.669, 0.677, 0.85), c__Cytophagia = c(1.234, 
    1.005, 0.925, 1.255, 0.872, 0.837, 1.151, 1.079, 1.045, 0.559, 
    0.906, 0.479), c__Bacteroidetes_unclassified = c(1.097, 1.104, 
    0.222, 0.247, 0.601, 0.385, 0.345, 0.409, 0.358, 0.231, 0.284, 
    0.127), c__Actinobacteria_unclassified = c(0.898, 0.956, 
    1.223, 1.099, 1.573, 1.617, 1.09, 0.972, 0.972, 0.669, 1.044, 
    0.708), c__Acidobacteria_Gp16 = c(0.868, 1.043, 1.757, 1.614, 
    0.716, 0.954, 0.848, 1.092, 0.904, 1.213, 1.346, 1.588), 
    c__Acidobacteria_Gp4 = c(0.828, 0.607, 0.381, 0.566, 1.042, 
    1.203, 0.666, 0.751, 3.325, 3.869, 1.634, 3.472), c__Acidobacteria_Gp17 = c(0.654, 
    0.357, 0.251, 0.281, 0.252, 0.342, 0.403, 0.385, 0.207, 0.374, 
    0.236, 0.48), c__Nitrospira = c(0.462, 0.244, 0.198, 0.208, 
    0.016, 0.032, 0.024, 0.042, 0.296, 0.103, 0.229, 0.211), 
    c__Bacilli = c(0.423, 0.362, 2.233, 2.468, 0.363, 0.463, 
    0.942, 1.303, 0.505, 0.562, 0.764, 0.683), c__Acidobacteria_Gp7 = c(0.421, 
    0.238, 0.122, 0.182, 0.328, 0.422, 0.327, 0.237, 1.964, 1.014, 
    1.364, 0.475), c__Saccharibacteria_genera_incertae_sedis = c(0.412, 
    0.498, 0.444, 0.54, 0.503, 0.564, 0.821, 1.001, 0.664, 0.61, 
    0.451, 0.717), c__Firmicutes_unclassified = c(0.382, 0.301, 
    0.256, 0.247, 0.174, 0.212, 0.188, 0.145, 0.471, 0.352, 0.389, 
    0.432), c__Anaerolineae = c(0.364, 0.467, 0.137, 0.225, 0.537, 
    0.442, 0.509, 0.422, 0.399, 0.179, 0.279, 0.194), c__Planctomycetia = c(0.305, 
    0.221, 0.325, 0.265, 0.217, 0.31, 0.239, 0.245, 0.264, 0.339, 
    0.517, 0.346), c__Acidobacteria_Gp10 = c(0.288, 0.267, 0.193, 
    0.22, 0.326, 0.593, 0.436, 0.589, 0.141, 0.225, 0.355, 0.566
    ), c__Acidobacteria_Gp5 = c(0.246, 0.149, 0.146, 0.168, 0.014, 
    0.02, 0.048, 0.028, 0.117, 0.079, 0.105, 0.155), c__Subdivision3 = c(0.236, 
    0.084, 0.056, 0.053, 0.059, 0.049, 0.033, 0.064, 0.109, 0.059, 
    0.092, 0.066), c__Acidobacteria_unclassified = c(0.222, 0.13, 
    0.09, 0.109, 0.092, 0.124, 0.07, 0.075, 0.173, 0.059, 0.169, 
    0.086), c__Thermoleophilia = c(0.207, 0.242, 0.383, 0.354, 
    0.642, 0.61, 0.412, 0.445, 0.328, 0.328, 0.417, 0.311), c__Spartobacteria = c(0.2, 
    0.132, 0.168, 0.185, 0.086, 0.083, 0.076, 0.057, 0.189, 0.315, 
    0.263, 0.268), c__Clostridia = c(0.181, 0.141, 0.264, 0.242, 
    0.322, 0.578, 0.512, 0.582, 0.117, 0.126, 0.14, 0.148), c__Bacteroidia = c(0.179, 
    0.252, 0.259, 0.149, 0.211, 0.229, 0.176, 0.184, 0.237, 0.197, 
    0.222, 0.175), c__Fibrobacteres_unclassified = c(0.14, 0.074, 
    0.007, 0.009, 0.072, 0.044, 0.136, 0.079, 0.117, 0.018, 0.167, 
    0.065), c__Acidobacteria_Gp22 = c(0.139, 0.074, 0.01, 0.007, 
    0, 0.005, 0, 0.007, 0, 0.002, 0.002, 0.001), c__Chloroflexia = c(0.133, 
    0.183, 0.342, 0.319, 0.271, 0.278, 0.618, 0.468, 0.161, 0.172, 
    0.401, 0.49), c__Rubrobacteria = c(0.131, 0.17, 0.198, 0.168, 
    0.359, 0.422, 0.267, 0.265, 0.481, 0.41, 0.588, 0.429), c__Oligoflexia = c(0.131, 
    0.105, 0.102, 0.08, 0.103, 0.066, 0.064, 0.096, 0.121, 0.149, 
    0.151, 0.052), c__Thermomicrobia = c(0.124, 0.179, 0.271, 
    0.227, 0.388, 0.522, 0.445, 0.46, 0.189, 0.184, 0.275, 0.26
    ), c__Candidatus_Latescibacteria_unclassified = c(0.113, 
    0.059, 0.017, 0.005, 0.004, 0.017, 0.015, 0.009, 0, 0.011, 
    0.007, 0.018), c__Caldilineae = c(0.102, 0.118, 0.176, 0.19, 
    0.146, 0.163, 0.17, 0.121, 0.147, 0.192, 0.183, 0.225), c__Opitutae = c(0.102, 
    0.05, 0.01, 0.007, 0.059, 0.024, 0.03, 0.048, 0.087, 0.303, 
    0.043, 0.021), c__Verrucomicrobiae = c(0.096, 0.114, 0.078, 
    0.085, 0.062, 0.061, 0.036, 0.07, 0.111, 0.033, 0.085, 0.027
    ), c__Latescibacteria_genera_incertae_sedis = c(0.085, 0.04, 
    0.01, 0.004, 0.012, 0.015, 0.033, 0.015, 0.012, 0.016, 0.011, 
    0.018), c__Acidobacteria_Gp1 = c(0.079, 0.057, 0.071, 0.099, 
    0.012, 0.022, 0.018, 0.007, 0.431, 0.362, 0.346, 0.552), 
    c__Thermodesulfobacteria_unclassified = c(0.068, 0.057, 0.115, 
    0.103, 0.008, 0.01, 0.015, 0.007, 0.01, 0.003, 0.002, 0.013
    ), c__Phycisphaerae = c(0.065, 0.044, 0.024, 0.02, 0.051, 
    0.039, 0.039, 0.028, 0.066, 0.092, 0.034, 0.018), c__Chlorobi_unclassified = c(0.052, 
    0.025, 0.002, 0.012, 0.029, 0.046, 0.033, 0.04, 0.05, 0.02, 
    0.046, 0.025), c__Acidimicrobiia = c(0.05, 0.065, 0.073, 
    0.038, 0.043, 0.037, 0.015, 0.015, 0.01, 0.011, 0.016, 0.014
    ), c__Acidobacteria_Gp11 = c(0.046, 0.032, 0.02, 0.016, 0.006, 
    0, 0.015, 0.007, 0.008, 0.007, 0.011, 0.018), c__Verrucomicrobia_unclassified = c(0.042, 
    0.023, 0.007, 0.02, 0.027, 0.022, 0.012, 0.02, 0.024, 0.016, 
    0.037, 0.015), c__Elusimicrobia = c(0.041, 0.015, 0.012, 
    0.001, 0.002, 0.002, 0, 0.007, 0, 0.002, 0.005, 0.001), c__Cyanobacteria_unclassified = c(0.035, 
    0.023, 0.017, 0.014, 0.031, 0.039, 0.015, 0.039, 1.306, 0.349, 
    0.49, 0.077), c__Spirochaetia = c(0.035, 0.021, 0.002, 0.001, 
    0, 0.002, 0.024, 0.039, 0, 0, 0, 0), c__Chloroplast = c(0.031, 
    0.019, 1.042, 1.341, 0.174, 0.081, 2.581, 4.578, 4.553, 1.565, 
    9.808, 8.067), c__Parcubacteria_genera_incertae_sedis = c(0.03, 
    0.013, 0, 0, 0.01, 0.015, 0.042, 0.037, 0.032, 0.059, 0.053, 
    0.011), c__Dehalococcoidia = c(0.03, 0.019, 0.027, 0.022, 
    0.004, 0.007, 0.003, 0.013, 0, 0.018, 0.009, 0.008), c__Chlamydiia = c(0.028, 
    0.017, 0.046, 0.05, 0.014, 0.007, 0.021, 0.022, 0.07, 0.074, 
    0.08, 0.152), c__Acidobacteria_Gp2 = c(0.028, 0.011, 0.007, 
    0.015, 0, 0, 0, 0, 0.02, 0.03, 0.034, 0.024), c__Chitinophagia = c(0.028, 
    0.019, 0.002, 0.009, 0.004, 0, 0.006, 0.011, 0.048, 0.002, 
    0.027, 0.008), c__Planctomycetes_unclassified = c(0.022, 
    0.002, 0.005, 0, 0.006, 0.007, 0.006, 0.004, 0, 0.007, 0, 
    0), c__Elusimicrobia_unclassified = c(0.018, 0.006, 0, 0, 
    0.002, 0, 0.015, 0.009, 0, 0, 0, 0.004), c__Ktedonobacteria = c(0.017, 
    0.011, 0.039, 0.026, 0, 0, 0, 0, 0.026, 0.113, 0.064, 0.025
    ), c__Cyanobacteria = c(0.013, 0.006, 0.012, 0.018, 0.115, 
    0.071, 0.033, 0.072, 1.274, 21.049, 1.119, 0.622), c__Armatimonadia = c(0.013, 
    0.017, 0.007, 0.023, 0.008, 0.024, 0.012, 0.006, 0.064, 0.03, 
    0.037, 0.028), c__Mollicutes = c(0.013, 0.008, 0.002, 0.004, 
    0.006, 0.005, 0.018, 0.033, 0.006, 0.003, 0.025, 0.046), 
    c__Chthonomonadetes = c(0.011, 0.008, 0.012, 0.005, 0.002, 
    0.007, 0.003, 0.002, 0.028, 0.015, 0.018, 0.02), c__BRC1_genera_incertae_sedis = c(0.011, 
    0.008, 0.002, 0.003, 0.008, 0.015, 0.006, 0.013, 0.018, 0.026, 
    0.016, 0.006), c__Armatimonadetes_unclassified = c(0.011, 
    0.008, 0, 0.003, 0.008, 0.007, 0.006, 0, 0.008, 0.013, 0.009, 
    0.01), c__Hydrogenophilalia = c(0.011, 0.013, 0, 0, 0, 0.005, 
    0.003, 0, 0, 0, 0, 0), c__Saprospiria = c(0.009, 0.008, 0.002, 
    0.001, 0.006, 0.005, 0.006, 0.006, 0.429, 0.067, 0.117, 0.011
    ), c__Candidatus_Saccharibacteria_unclassified = c(0.009, 
    0.013, 0.012, 0.012, 0.021, 0.037, 0.021, 0.015, 0.008, 0.026, 
    0.014, 0.02), c__Acidobacteria_Gp18 = c(0.009, 0.008, 0.005, 
    0, 0.004, 0.002, 0.009, 0.002, 0.004, 0.002, 0.007, 0.007
    ), c__Epsilonproteobacteria = c(0.009, 0.002, 0.005, 0.005, 
    0.012, 0.007, 0, 0.002, 0, 0.002, 0, 0.003), c__Acidobacteria_Gp25 = c(0.007, 
    0.011, 0.005, 0.022, 0, 0.002, 0, 0.011, 0.008, 0.03, 0.021, 
    0.054), c__Erysipelotrichia = c(0.007, 0.004, 0.002, 0.005, 
    0.004, 0.007, 0.009, 0.024, 0.004, 0.003, 0.007, 0.035), 
    c__Acidobacteria_Gp15 = c(0.007, 0.011, 0.002, 0.003, 0, 
    0.002, 0, 0.002, 0.008, 0.005, 0, 0.003), c__Thermotogae = c(0.007, 
    0.002, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), c__Armatimonadetes_gp4 = c(0.006, 
    0.008, 0.017, 0.019, 0.014, 0.017, 0.009, 0.024, 0.07, 0.074, 
    0.048, 0.048), c__WPS.1_genera_incertae_sedis = c(0.006, 
    0.004, 0.024, 0.011, 0.008, 0.005, 0.009, 0.006, 0.024, 0.033, 
    0.018, 0.023), c__Armatimonadetes_gp5 = c(0.004, 0.008, 0.015, 
    0.022, 0.041, 0.037, 0.018, 0.022, 0.024, 0.021, 0.016, 0.018
    ), c__Negativicutes = c(0.004, 0, 0.005, 0, 0.002, 0.059, 
    0, 0.018, 0.002, 0.002, 0, 0.004), c__Acidobacteriia = c(0.004, 
    0.006, 0.002, 0.001, 0.002, 0.005, 0.003, 0.002, 0, 0.007, 
    0.005, 0.004), c__Deinococci = c(0.004, 0, 0.002, 0.003, 
    0.002, 0.002, 0, 0.002, 0.002, 0.008, 0, 0.003), c__Acidobacteria_Gp9 = c(0.004, 
    0.002, 0.005, 0.003, 0, 0, 0, 0, 0, 0, 0, 0), c__Ignavibacteria = c(0.002, 
    0, 0.002, 0.003, 0.004, 0.012, 0.003, 0, 0, 0, 0.002, 0), 
    c__Holophagae = c(0.002, 0, 0, 0, 0.004, 0.002, 0, 0, 0.002, 
    0.002, 0.007, 0.004), c__Fimbriimonadia = c(0.002, 0.002, 
    0, 0, 0.002, 0.002, 0, 0, 0.004, 0.007, 0, 0.001), c__Candidatus_Gracilibacteria_unclassified = c(0.002, 
    0, 0.005, 0.003, 0.002, 0, 0.003, 0, 0, 0, 0, 0.001), c__Candidatus_Microgenomates_unclassified = c(0.002, 
    0, 0.005, 0, 0, 0, 0, 0, 0, 0.002, 0, 0), c__Candidatus_Melainabacteria_unclassified = c(0.002, 
    0.002, 0, 0, 0, 0, 0, 0, 0, 0.002, 0, 0), c__Candidatus_Tectomicrobia_unclassified = c(0, 
    0, 0, 0, 0.014, 0.029, 0.024, 0.017, 0, 0, 0, 0), c__unclassified = c(0, 
    0, 0.002, 0, 0.008, 0.002, 0, 0, 0, 0.005, 0, 0.01), c__candidate_division_WWE3_unclassified = c(0, 
    0.002, 0, 0.003, 0, 0.002, 0.012, 0.002, 0, 0, 0, 0.003), 
    c__Microgenomates_genera_incertae_sedis = c(0, 0.002, 0, 
    0.001, 0, 0.005, 0.003, 0.002, 0.002, 0.003, 0, 0.001), c__Longimicrobia = c(0, 
    0, 0, 0, 0.006, 0, 0, 0, 0.008, 0, 0.002, 0.001), c__Fusobacteriia = c(0, 
    0, 0, 0, 0, 0, 0.003, 0.004, 0, 0, 0, 0.001), c__WPS.2_genera_incertae_sedis = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0.003, 0, 0.003), c__Candidatus_Hydrogenedens = c(0, 
    0, 0, 0, 0, 0.002, 0, 0.002, 0, 0.002, 0, 0)), class = "data.frame", row.names = c("D15B", 
"D610B", "D15F", "D610F", "HR15B", "HR610B", "HR15F", "HR610F", 
"C15B", "C610B", "C15F", "C610F"))

CODES

> Class.dat <- structure(list(...
> require(vegan)
>  require(tidyverse)
> require(ggplot2)
>#Create Dataframe
> x <- data.frame(tax=names(Class.dat), nsites=apply(Class.dat, 2, function(x){length(which(x>0))}))
> ##Class.dat is the array; if array is matrix, the margin "1" indicates rows and "2" indicates columns; "max" is the funtion to be applied
> ###"$" operator is used to extract or subset a specific part of a data object
> x$max <- apply(Class.dat, 2, max)
> x <- x[order(x$nsites),]
> rownames(x) <- NULL
> x
#Shows little evidence of zero inflation (large % 0s)
> y <- data.frame(site=rownames(Class.dat), ntax=apply(Class.dat, 1, function(x){length(which(x>0))}))
> y$pzero <- (ncol(Class.dat)-y$ntax)/ncol(Class.dat)
> y$shan <- diversity(Class.dat)
> y$simp <- diversity(Class.dat, index="simpson")
> 
> #Do the sites differ in terms of single variables?
> ##no
> y$x1 <- substr(y$site, 1, 1)
> kruskal.test(ntax~x1, data=y)
> kruskal.test(shan~x1, data=y)
> kruskal.test(simp~x1, data=y)
#Bray-Curtis dissimilarity
> d1 <- vegdist(Class.dat)
> 
> #Bray-Curtis Dissimilarity (Log+1-Transformed Abundances)
> ##log+1 problematic because many values < 1
> d2 <- vegdist(log1p(Class.dat))
> 
> #Bray-Curtis on logit scale to avoid issues with log+1
> logit_class <- as.matrix(Class.dat+1)/100
> d3 <- qlogis(logit_class)
> d3 <- d3+abs(min(d3))
> d3 <- vegdist(d3)
> #Create NMDS Plot
> #Ordination using NMDS (non-metric multidimensional scaling)
> m1 <- metaMDS(d1)
> m2 <- metaMDS(d2)
> m3 <- metaMDS(d3)
 #Fit vectors of classes to NMDS space
> e1 <- envfit(m3, Class.dat)
> exy <- data.frame(tax=names(Class.dat), x=e1$vectors$arrows[,1], y=e1$vectors$arrows[,2], pval=e1$vectors$pvals, r=e1$vectors$r)
> rownames(exy) <- NULL
> #Vector correlations with ordination space:
> exy <- exy[order(-exy$r),]
> #Create Dataframe
> mdat <- data.frame(m3$points)
> mdat$site <- substr(rownames(mdat), 1, 1)
> mdat$col <- ifelse(mdat$site == "D", "red", ifelse(mdat$site == "H", "blue", "green"))
> #See if there are group differences in NMDS space
> ##Use MRPP to test location
> mrpp(mdat[,1:2], mdat$site)
#There are differences
> ##A: 0.5378 <- clean separation of groups
> #Find Max Vector Radius
> ##Incorporate distance equation: r=sqrt[(x^2)+(y^2)]
> mdat$rad <- sqrt((mdat$MDS1^2) + (mdat$MDS2^2))
> max(mdat$rad)
> #Max is 0.1653488
> #Adding arrows to plot below will require some scaling
> exy$x2 <- 0.17 * exy$r * exy$x
> exy$y2 <- 0.17 * exy$r * exy$y
> #Set up some graphical parameters for plotting labels
> exy$adj <- ifelse(exy$x < 0, 1, 0)
> #Plot shows this:
> plot(mdat[,1], mdat[,2], pch=16, col=mdat$col,xlab="NMDS1", ylab="NMDS2", xlim=c(-0.2, 0.2), ylim=c(-0.2, 0.2), main= "NMDS Plot of Bray-Curtis Dissimilarity")
> legend("bottomright", legend=c("D", "HR", "C"), pch=16, col=c("red", "blue", "green"))
> segments(0,0,exy$x2[1:12], exy$y2[1:12], col="red")

Hi @Terrible_Coder,
Check out the text() function for manually adding labels to a base-R plot (like yours).
See help(text)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.