Home > database >  Plotting NMDS A continuous variable can not be mapped to shape ggplot2 but group variable seems to b
Plotting NMDS A continuous variable can not be mapped to shape ggplot2 but group variable seems to b

Time:04-30

I am trying to plot NMDS results using an example I found online. When I plot my group variable (which are 'S' and 'E') I get "Error in 'scale_f()': A continuous variable can not be mapped to shape. I tried doing the same thing using a different group (month) and I continue to get the same error.

library(ggplot2)
library(vegan)
library(dplyr)

Data used for NMDS (I apologize this is long):

    structure(list(d.lept = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.014625576, 0, 0, 0, 0.008037479, 0.155240934, 
0.089817793, 0.160428397, 0.55430068, 0.170641857, 0.081105468, 
0.110965192, 0.119195485, 0.389720492, 0.128494423, 0.025249815, 
0.053921767, 0.012391113, 0.065359717, 0.084967633, 0, 0, 0, 
0, 0.059956999, 0.05310477, 0.027149068, 0, 0.174208785, 0.124913669, 
0.032951436, 0.218108878, 0.01964149, 0.051629638, 0.064649286, 
0.060485433, 0, 0.043224813, 0.057558719, 0.069338871, 0.022259485, 
0.013767903, 0.046661095, 0, 0.105882742, 0.104713632, 0.008348348, 
0.094709145, 0, 0.017608424, 0.014949064, 0.026937202, 0.037461505, 
0.041380341, 0.023943191, 0, 0.041659777, 0.091036749, 0, 0.177957475, 
0.140571451, 0.075287776, 0.049019788, 0.063363647, 0.083140372, 
0.157582634, 0.021133531, 0.178249209, 0.049649323, 0.049762937, 
0.071948399, 0.204249117, 0, 0.039129831, 0.0312381, 0, 0.079657156, 
0.209427264, 0.110415859), d.byths = c(0, 0, 0, 0, 0, 0, 0.037173339, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.007392425, 
0.017963559, 0.011883585, 0.021996059, 0, 0, 0, 0, 0.005567436, 
0, 0, 0, 0, 0, 0, 0.069700011, 0, 0.01498925, 0.028928669, 0.029978499, 
0, 0, 0, 0.002639527, 0.004570012, 0.002745953, 0.001896599, 
0, 0, 0, 0, 0, 0, 0, 0, 0.044518969, 0.013767903, 0.015553698, 
0.009860302, 0, 0.041885453, 0, 0.023677286, 0.071145147, 0, 
0, 0, 0, 0, 0, 0, 0.020829888, 0, 0, 0, 0, 0.037643888, 0, 0.063363647, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.022083172), d.daph = c(0.140036552, 
1.010093452, 1.629907953, 2.762608821, 2.357448478, 1.454608928, 
0.278800045, 0.346222278, 2.703515584, 0.805822064, 1.450666899, 
4.348343553, 12.42577355, 3.294403721, 2.503983769, 2.508770817, 
0.706293446, 1.111704253, 0.819032262, 0.655258862, 1.130338642, 
1.311853781, 0.031419235, 0.029569702, 0.002245445, 0.029708962, 
0.017596847, 0.022257633, 0.770501941, 0.510439883, 0.021671906, 
0.100213841, 0.0525659, 0.084166051, 0.024509894, 0.049564452, 
0.049019788, 0.651418517, 1.81220029, 0.37720006, 0.344752743, 
0.216965015, 0.389720492, 0.424838163, 0.13574534, 0.12265315, 
0.010558108, 0.003046675, 0, 0.003793198, 0.039282981, 0, 0, 
0, 0.015234975, 0.010806203, 0.134303677, 0.104008307, 0.133556908, 
0.082607421, 0.062214794, 0.009860302, 0, 0.041885453, 0, 0.071031858, 
0.071145147, 0.070433695, 0.07474532, 0.134686012, 0.299692038, 
0.206901703, 0.095772765, 0.108535297, 0.020829888, 0.273110248, 
0.261375042, 0.533872426, 0.04685715, 0.075287776, 0, 0, 0, 0, 
0, 0.118832806, 0, 0.033175291, 0.0239828, 0.081699647, 0.210273434, 
0, 0.093714301, 0.04998096, 0.079657156, 0.041885453, 0.066249516
), d.diaph = c(0, 0, 0.014297438, 0, 0, 0, 0, 0, 0, 0, 0, 0.099961921, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.002245445, 0.005941792, 
0, 0, 0, 0.022193038, 0.003611984, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0.002639527, 0.001523337, 0.002745953, 0, 0.039282981, 
0.129074095, 0.064649286, 0.020161811, 0, 0.010806203, 0, 0, 
0, 0, 0, 0, 0.023529498, 0, 0.008348348, 0.047354572, 0.035572573, 
0, 0, 0, 0.037461505, 0, 0, 0, 0.041659777, 0, 0, 0.059319158, 
0.04685715, 0.037643888, 0.032679859, 0.09504547, 0.020785093, 
0.031516527, 0.042267062, 0.059416403, 0.024824662, 0.049762937, 
0.047965599, 0, 0.030039062, 0.078259662, 0.124952401, 0.549790564, 
0.238971467, 0.041885453, 0), d.bosm = c(1.043908843, 1.923987528, 
1.629907953, 2.356342818, 5.51953506, 5.712645973, 2.156053678, 
1.968000315, 4.81225774, 3.885213525, 4.442667377, 11.54560185, 
3.483375849, 3.383441659, 2.17939328, 2.989904947, 1.56128025, 
1.908775227, 4.036659007, 2.293406017, 4.386790445, 6.982447546, 
0, 0, 0.00449089, 0, 0, 0.003709606, 0.040552734, 0, 0.007223969, 
0, 0.029203278, 0.042083026, 0.024509894, 0.068151122, 0.016339929, 
0.424838163, 1.045500167, 0.295200047, 0.029978499, 0.072321672, 
0.029978499, 0.05310477, 0.027149068, 0, 0.013197635, 0.010663362, 
0.016475718, 0.013276193, 3.005148035, 1.277833538, 0.549518929, 
0.070566339, 0, 0.09725583, 0.057558719, 0.832066454, 0.333892269, 
0.330429682, 0.979883002, 0.049301511, 0.062745329, 0.314140895, 
0.025045045, 0.544577582, 0.035572573, 0.052825272, 0.179388769, 
0.10774881, 0.149846019, 0.165521362, 0, 0.054267649, 0, 0.091036749, 
0.087125014, 0.77114906, 0.187428601, 0.188219439, 0.653597174, 
1.489045693, 0.041570186, 0.409714848, 0.97918694, 0.118832806, 
0.024824662, 2.073455709, 0.719483986, 2.41013958, 5.076601486, 
2.934737312, 2.280381317, 21.99162257, 3.62440058, 5.612650663, 
3.776222386), d.chydo = c(0, 0, 0, 0, 0.056465832, 0.05289487, 
0, 0, 0, 0, 0, 0, 0.103981369, 0, 0, 0.068733447, 0, 0.041951104, 
0.073127881, 0, 0, 0.042317864, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.235697885, 
0.077444457, 0, 0.015121358, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.09504547, 0, 
0, 0, 0, 0, 0, 0, 0, 0.030039062, 0, 0.0312381, 0.04998096, 0, 
0.125656358, 0.110415859), d.calan = c(1.088465928, 1.555223252, 
2.015938784, 4.103286631, 1.72220787, 1.692635844, 2.100293669, 
1.858666964, 2.379093714, 3.136950179, 1.269333536, 3.248762425, 
3.899301323, 2.077551896, 3.153164746, 3.917806482, 2.06312033, 
2.747797305, 1.213922817, 0.688021805, 1.318728416, 2.496753971, 
0.075260028, 2.476462517, 0.27394427, 0.772433024, 0.312344034, 
0.396927797, 3.825474551, 5.104398827, 0.487617892, 0.634687659, 
0.624950149, 1.026825825, 0.281863781, 2.001164765, 2.124190816, 
27.10467481, 21.46760343, 4.526400724, 4.182000669, 7.145381169, 
18.34684165, 12.42651627, 7.330248364, 4.415513397, 0.314103719, 
0.15385708, 0.310292694, 0.08914015, 1.37490433, 1.47144468, 
4.137554285, 0.504045278, 1.698699725, 1.3183568, 3.818061686, 
2.553981756, 4.585453827, 3.717333928, 4.821646518, 1.745273489, 
0.45098205, 3.685919839, 0.73465464, 3.930429504, 5.869474623, 
4.82470814, 2.197512416, 4.633198809, 7.267531912, 4.510457122, 
4.070342517, 5.643835453, 2.499586607, 6.918792944, 5.663125906, 
3.737106981, 5.201143689, 3.689101012, 1.846412017, 3.865182437, 
2.327930408, 3.655917107, 0.542427298, 8.971876845, 2.606589458, 
1.857816316, 4.388852315, 8.129114854, 6.969062395, 10.9172228, 
5.372953241, 1.749333613, 4.938743647, 4.984368873, 4.41663437
), d.cyclo = c(0.305534295, 0.945960535, 1.501231009, 2.68135562, 
1.581043291, 1.481056363, 0.650533437, 0.67422233, 1.865425753, 
1.669202848, 1.414400226, 3.99847683, 5.770965958, 2.047872583, 
1.993913001, 2.302570476, 1.078026839, 1.887799675, 1.608813372, 
1.572621269, 0.888123219, 1.904303876, 0.006576119, 0.266127315, 
0.026945338, 0.083185095, 0.013197635, 0.081611323, 0.216281247, 
0.221930384, 0.05779175, 0.167023068, 0.110972456, 0.235664943, 
0.046568799, 0.229235592, 0.588237457, 6.65579789, 10.10650162, 
2.558400409, 0.479655991, 2.45893684, 3.387570434, 1.672800268, 
1.547496877, 1.251062129, 0.110860136, 0.094446921, 0.131805746, 
0.055001369, 0.589244713, 0.593740836, 2.876893214, 0.246982186, 
1.005508358, 1.199488564, 3.587826811, 4.067880444, 9.504799924, 
5.507161375, 5.568224043, 1.834016209, 0.313726644, 4.544571619, 
0.9183183, 4.830166378, 6.972224401, 3.380817383, 2.436697441, 
4.363826785, 10.11460627, 4.634598144, 5.530877186, 6.783456073, 
2.791205044, 8.102270684, 8.581813873, 7.53353312, 4.592000735, 
4.44197877, 1.584973147, 1.489045693, 1.891443457, 2.74193783, 
0.39449258, 4.337397415, 1.415005706, 1.327011654, 1.319053974, 
6.862770329, 5.767499913, 2.465179342, 6.778667751, 2.69897186, 
3.863372047, 2.764439879, 6.823700102)), class = "data.frame", row.names = c("1111E", 
"1111S", "116E", "116S", "117E", "117S", "111E", "111S", "1110E", 
"1110S", "112E", "112S", "113E", "113S", "114E", "114S", "115E", 
"115S", "118E", "118S", "119E", "119S", "710E", "710S", "711E", 
"711S", "712E", "712S", "78E", "78S", "79E", "79S", "713E", "713S", 
"72S", "73S", "714E", "714S", "715E", "715S", "716E", "716S", 
"717E", "717S", "718E", "718S", "719E", "719S", "720S", "721S", 
"81E", "81S", "82E", "82S", "83E", "83S", "84E", "84S", "85E", 
"85S", "86E", "86S", "87E", "87S", "88E", "88S", "813E", "813S", 
"814E", "814S", "815E", "815S", "816E", "816S", "817E", "817S", 
"818E", "818S", "819E", "819S", "820E", "820S", "821E", "822E", 
"822S", "823E", "823S", "824E", "824S", "810E", "810S", "811E", 
"811S", "812E", "812S", "89E", "89S"))

Row names are my site names.

NMDS:

znmds <- metaMDS(allzp, distance = 'bray', k=2)

Following example I found to plot nmds using ggplot:

data.scores <- as.data.frame(scores(znmds))
data.scores$site <- rownames(data.scores)

Group variable:

    structure(list(Tow = c("E", "S", "E", "S", "E", "S", "E", "S", 
"E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", 
"S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", 
"S", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", 
"S", "S", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", 
"E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S", "E", 
"S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "E", "S", "E", 
"S", "E", "S", "E", "S", "E", "S", "E", "S", "E", "S")), row.names = c(NA, 
-97L), class = "data.frame")

Adding my group variable:

data.scores$grp <- grp

Using the scores function from vegan package to extract the species scores and convert to a data frame

species.scores <- as.data.frame(scores(znmds, "species"))
species.scores$species <- rownames(species.scores)

ggplot:

    ggplot()   
  geom_text(data=species.scores,aes(x=NMDS1,y=NMDS2,label=species),alpha=0.5)    # add the species labels
  geom_point(data=data.scores,aes(x=NMDS1,y=NMDS2,shape=grp,colour=grp),size=3)   # add the point markers
  geom_text(data=data.scores,aes(x=NMDS1,y=NMDS2,label=site),size=6,vjust=0)    # add the site labels
  scale_colour_manual(values=c("S" = "red", "E" = "blue"))  
  coord_equal()  
  theme_bw()

This gives me the following error: Error in scale_f(): ! A continuous variable can not be mapped to shape

When I check my group (grp) class:

class(data.scores$grp)
[1] "data.frame"

is.vector(as.character(data.scores[,4]))
[1] TRUE

Doing this a little bit differently, NOT using ggplot, works:

mds.fig <- ordiplot(znmds, type = "none")
points(mds.fig, "sites", pch = 19, col = "green", select = zp1$Tow == 
         "S")
points(mds.fig, "sites", pch = 19, col = "blue", select = zp1$Tow == 
         "E")
ordiellipse(znmds, zp1$Tow, conf = 0.95, label = TRUE)

This one works. This plot shows that the 'S' and 'E' grouping is very similar therefore I tried doing the same thing but making the months ('11', '7', '8') my group, but I am still getting the same error when trying to use ggplot to plot this. I do not understand where the problem is coming from. I did not include my 'zp1' data in here, but it is basically all the data for my field season (I used it to extract the Tow groups for the last code to plot it). I looked at the following posts: here, here, here, here. Thank you.

CodePudding user response:

Instead of data.scores$grp <- grp, try data.scores$grp <- grp$Tow. With the former, you are assigning data.scores$grp to be a data frame, and with the latter, it is an actual character vector which is able to be mapped to the color and shape scales.

  • Related