0

I am following this tutorial for plotting ordination with vegan, BiodiversityR, and ggplot2.

Everything has been working great, but I can't get the plots in Section 5 Example 3 (adding ordispider diagrams) to work.

I am not sure what is wrong with my code or DF but I looked at the examples in that tutorial and my df and code seems to be formatted the same way.

DF with species data (df name = zpsp.hell):

structure(list(Leptodora = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0514783552756107, 
0, 0.295983653515509, 0.447076245195055, 0.664047357495223, 0.217295135332132, 
0.524039966427768, 0.297774358162658, 0.364735453138815, 0.0927349215902997, 
0.146170738859723, 0, 0, 0.132810726702271, 0.080213443207596, 
0.545094132929642, 0.268916855779092, 0.745397328336896, 0.126127608033532, 
0.175139490361829, 0.124573480483318, 0.152671480802429, 0.0781119972029288, 
0.0955020586149627, 0.218022610074077, 0.15035532659253, 0.0577887034961061, 
0.0944893094996833, 0.111527284010883, 0.0673076436717264, 0.152604460900712, 
0.157433787965431, 0.191221314623361, 0.148456857402865, 0.182219542677383, 
0.198461604029891, 0.203151677578394, 0.153555781327532, 0.152128717230044, 
0.0960104148770112, 0.0968370763605435, 0.189876052681651), Bythotrephes = c(0, 
0, 0, 0.0890687324870241, 0, 0, 0, 0, 0, 0, 0, 0.0653167373436653, 
0.162236377495539, 0.132656786551149, 0, 0.060878128385392, 0, 
0, 0, 0, 0.0918412784176455, 0.0980712016124111, 0.0697427214496397, 
0, 0.0903133799854954, 0.0782094244596514, 0.0730477390423102, 
0, 0, 0, 0, 0.0988236709509051, 0.070845392652599, 0.101022398944138, 
0.0734405376833494, 0.114669643051068, 0, 0, 0, 0.0620736917382509, 
0, 0.0831457660891652, 0.112753710619422, 0, 0, 0, 0, 0, 0, 0, 
0.0532700949678991), Daphnia = c(0.42449267992281, 0.493670262878194, 
0.471551608628823, 0.324854309153743, 0.458517140468759, 0.47737991306865, 
0.556414557322789, 0.491189411607959, 0.425884951904507, 0.406630163711583, 
0.429604051272218, 0.185181368282627, 0.167779606094742, 0.177789142972488, 
0.470752702672566, 0.277080773801118, 0.281885140972553, 0.247661981126601, 
0.183796952755557, 0.284593438653985, 0.381044578566026, 0.315840124042826, 
0.313238068623479, 0.234961791064046, 0.123867042228419, 0, 0.103256248537954, 
0.0943574961883614, 0, 0.0970963381493535, 0.204213129897631, 
0.183676262195304, 0.117977203783932, 0.101022398944138, 0.125761872333712, 
0.159163956293704, 0.203406882407371, 0.259183555460385, 0.188665785401177, 
0.219460673883874, 0.297574588053109, 0.146832661350079, 0, 0, 
0, 0.150239563286413, 0.106823351104262, 0.178602385397776, 0.110194629575929, 
0.122123988761292, 0.115496321810862), Diaphanosoma = c(0, 0.0453128519161195, 
0, 0, 0, 0.106431233795989, 0, 0, 0, 0, 0, 0, 0.0854241466742578, 
0, 0.0768059722006672, 0.0490589316380797, 0, 0, 0, 0, 0, 0, 
0, 0, 0.0686789566714733, 0.0782094244596514, 0, 0.189615507928035, 
0.145529124344689, 0.0627824495963241, 0, 0, 0, 0.0760558530129246, 
0.111774458436534, 0.0817771719683458, 0, 0.0776393085575247, 
0, 0.0873410124188497, 0.0933846760615373, 0.123191667576115, 
0.157714756851011, 0.0924818355004709, 0.13058590282257, 0.127509568420335, 
0.138356184789908, 0.0607077968458693, 0.158356432450334, 0.266739677938945, 
0.0730112866095621), Bosmina = c(0.569575610900163, 0.482059574956383, 
0.595315451122149, 0.595940930214229, 0.563135229362769, 0.580316694361079, 
0.476154393510413, 0.494695690323242, 0.512006973354646, 0.602961381390991, 
0.612812199873054, 0, 0.0633253202727827, 0.0547249497905991, 
0.103360344912224, 0.0693174710345271, 0.206625557949016, 0.247661981126601, 
0.2145683230841, 0.236126476999889, 0.326289046501747, 0.147632273351819, 
0.114641689336053, 0.080213443207596, 0.163628061381011, 0.190921942500448, 
0.192720548398676, 0.620171427463472, 0.354291857323481, 0.184489914785283, 
0.352362116363517, 0.296341555823042, 0.376192343399983, 0.282040984241167, 
0.322347666151911, 0.127303294189073, 0.234357983289025, 0.211963693988261, 
0.100590025663199, 0.127610421183581, 0.30622275161235, 0.244265183813913, 
0.486777242621488, 0.130128284166691, 0.45672951396509, 0.164266351121797, 
0.523152590142371, 0.516033361822212, 0.497649457111482, 0.633637651221134, 
0.551448265788949), Chydorid = c(0, 0, 0.121196172070502, 0, 
0, 0, 0.104275779135246, 0.0945537319324462, 0.0848206166332721, 
0.113495878321109, 0.0786658673713692, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0.250895551497334, 0.0624864611756641, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.137071905927351, 0, 
0, 0, 0, 0.0607077968458693, 0.0645728675805963, 0.0772483662203306, 
0.165933410776192), Calanoid = c(0.551681325280746, 0.532811202746103, 
0.458440623691115, 0.589938373390377, 0.511490062794191, 0.450636586826785, 
0.462089514439709, 0.529963139828854, 0.555048470784899, 0.440959821851403, 
0.484444133151001, 0.856827972146859, 0.800526896790662, 0.658465980502425, 
0.785447867147859, 0.708746420872284, 0.777595457047106, 0.793093760748284, 
0.876043239124249, 0.721148685459954, 0.642312378254447, 0.749718857289663, 
0.754676483043473, 0.781889557727033, 0.660196548921906, 0.776414477882314, 
0.490370742651721, 0.557923848370324, 0.670912916868689, 0.714106408987044, 
0.624303900753707, 0.620066849494076, 0.636191575996172, 0.637980913159245, 
0.632237780459911, 0.685911340790936, 0.669183889825719, 0.646205246813299, 
0.673853029631615, 0.661828404553669, 0.595308926983053, 0.654759644140332, 
0.627668295163134, 0.70702281427746, 0.628369175711032, 0.713431328968511, 
0.637649893522775, 0.588225626695548, 0.621568571471143, 0.499555695459076, 
0.551585706134286), Cyclopoid = c(0.437078143466517, 0.487818824382705, 
0.4454012533435, 0.42820971732822, 0.459367675190243, 0.470042563955826, 
0.489162218973039, 0.473505858570121, 0.49111864661282, 0.510991763696809, 
0.446124567787441, 0.373739331168216, 0.30578014826039, 0.270669608596975, 
0.312512154666471, 0.367925307905129, 0.42956068593583, 0.339554740939751, 
0.37964139973004, 0.567300229239601, 0.572136763019253, 0.5538532317755, 
0.544701629380669, 0.566191913855156, 0.460458913174319, 0.52552798872739, 
0.388304420323805, 0.424728443566489, 0.607121085835465, 0.653550014220099, 
0.648902145922277, 0.691443571891148, 0.652442538409524, 0.663101678624685, 
0.663368341258395, 0.681769881804385, 0.668555658001642, 0.672196294759643, 
0.704044489869559, 0.680269092937911, 0.655574692218978, 0.656817707889647, 
0.539133506305547, 0.664400871552651, 0.583190058042271, 0.619621726714942, 
0.515345457463098, 0.570339679238174, 0.56159616960745, 0.49753229064017, 
0.553681727539211)), class = "data.frame", row.names = c("11_Post", 
"6_Post", "7_Post", "1_Post", "10_Post", "2_Post", "3_Post", 
"4_Post", "5_Post", "8_Post", "9_Post", "10_Pre", "11_Pre", "12_Pre", 
"8_Pre", "9_Pre", "13_Pre", "2_Pre", "3_Pre", "14_Pre", "15_Pre", 
"16_Pre", "17_Pre", "18_Pre", "19_Pre", "20_Pre", "21_Pre", "1_Peak", 
"2_Peak", "3_Peak", "4_Peak", "5_Peak", "6_Peak", "7_Peak", "8_Peak", 
"13_Peak", "14_Peak", "15_Peak", "16_Peak", "17_Peak", "18_Peak", 
"19_Peak", "20_Peak", "21_Peak", "22_Peak", "23_Peak", "24_Peak", 
"10_Peak", "11_Peak", "12_Peak", "9_Peak"), decostand = "hellinger")

This is species abundance data. I had the raw abundance values log transformed using:

DATA1 <- log1p(DATA)

and then hellinger transformed using (to get df zpsp.hell included dput above):

zpsp.hell <- decostand(DATA1, 'hell')

Environmental variables (df name = env2):

structure(list(Depth = c(-1.28554082005847, 1.17439291881361, 
0.425717433069932, -1.17858717923794, -0.643818975135316, -0.536865334314792, 
0.960485637172556, 1.17439291881361, 1.17439291881361, -0.109050771032692, 
-0.857726256776366, -0.750772615955841, -1.49944810169952, -1.17858717923794, 
0.425717433069932, -0.750772615955841, 0.532671073890457, -0.536865334314792, 
0.960485637172556, 0.960485637172556, 1.17439291881361, 1.17439291881361, 
1.06743927799308, 0.853531996352031, -0.429911693494267, -1.39249446087899, 
-0.322958052673742, -1.17858717923794, -0.536865334314792, 0.960485637172556, 
1.17439291881361, 1.17439291881361, 1.17439291881361, 0.425717433069932, 
0.532671073890457, 0.532671073890457, 0.960485637172556, 1.17439291881361, 
1.17439291881361, 1.17439291881361, 0.960485637172556, -0.643818975135316, 
-1.60640174252004, -0.322958052673742, -1.60640174252004, -0.643818975135316, 
-1.60640174252004, -0.750772615955841, -1.17858717923794, -1.17858717923794, 
-0.750772615955841), Temp = c(-2.00202385088361, -1.6886585710476, 
-1.81702906081487, -2.05599906111249, -1.97674711644104, -1.98822650675557, 
-1.7799417997987, -1.74638665887931, -1.73347234477547, -1.91085100088553, 
-1.9711178000368, 0.421672808024158, 0.609537445671532, 0.535031787380124, 
0.404232965046318, 0.48370566722382, 0.441761741074582, 0.55545185668962, 
0.473771579451632, 0.42862666946469, 0.401804632479783, 0.468142263047392, 
0.475758397006069, 0.499158692647223, 0.701041431928683, 0.779079210316869, 
0.703359385742193, 0.468804535565538, 0.577417228541458, 0.465162036715736, 
0.45964309906452, 0.465934687986906, 0.450702420069552, 0.518585353179502, 
0.508982401666387, 0.501145510201661, 0.449709011292332, 0.435470152152197, 
0.445293861171361, 0.452799616377013, 0.467590369282271, 0.556997159231961, 
0.703800900754291, 0.556555644219863, 0.588896618855986, 0.592539117705789, 
0.609647824424556, 0.509644674184532, 0.527967547186567, 0.535142166133148, 
0.439885302273169), HypoTemperature = c(-0.632039483571147, -0.310740603494901, 
-0.444711698511755, -0.689896643356902, -0.626420959490315, -0.618198323327277, 
-0.40073448586531, -0.37199927438285, -0.35767419950271, -0.53913134566092, 
-0.605028831879817, 1.6362743573795, 1.93592937881817, 1.78142491458498, 
0.0465878573612245, 1.02269081509804, 1.840741409235, 1.73843802783685, 
-0.504278736580756, -0.61362300483864, -0.767370013987775, -0.744818818237529, 
-0.730410016420665, -0.442118677941942, 2.02949380096199, 2.10705174536549, 
1.88872128910613, -0.130417094360495, -0.591284439619038, -0.80564084446134, 
-0.825040375134106, -0.840702848434461, -0.857237198160421, -0.779526091937421, 
-0.780581627596114, -0.746858723639933, -0.795521835919555, -0.779787304582982, 
-0.776849919554728, -0.792488836552299, -0.806044379879795, -0.446556278044835, 
1.25274434898081, -0.471649239282665, 1.34524804576575, -0.327847501460662, 
1.70822468014826, -0.224239055376505, 0.399328486256581, 0.462749943190367, 
-0.0181803890405775), DO = c(1.46082176021775, 1.36148401502538, 
1.38386998577295, 1.47201474559153, 1.44962877484396, 1.42864192726811, 
1.38526910894468, 1.39226472480329, 1.38107173942951, 1.4104533260357, 
1.43283929678328, 0.531803974193337, 0.670317168193964, 0.586369777890554, 
-0.780573560883313, 0.305146020374129, 0.786444391447015, 1.08165938068068, 
-1.11496399892523, -0.788968299913654, -0.534327882659975, -0.622472642478556, 
-0.577700700983404, -0.237713770254592, 0.957137418397283, 0.827018963426997, 
0.992115497690371, -0.938674479288069, -0.937275356116345, -0.635064751024068, 
-0.746994604761948, -0.786170053570207, -0.753990220620566, -0.422398028922095, 
-0.809955147489506, -0.790367423085377, -0.604284041246151, -0.313266421527661, 
-0.292279573951809, -0.795963915772271, -0.334253269103514, -1.2562754392693, 
0.0854836824135382, -1.31643773565342, 0.515014496132655, -1.33602546005754, 
0.0602994653225151, -1.35421406128995, -1.33182809054237, -1.166731556279, 
-1.37799915520925), Season = structure(c(3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("Pre-Hypoxic", 
"Peak-Hypoxic", "Post-Hypoxic"), class = "factor")), row.names = c("11_Post", 
"6_Post", "7_Post", "1_Post", "10_Post", "2_Post", "3_Post", 
"4_Post", "5_Post", "8_Post", "9_Post", "10_Pre", "11_Pre", "12_Pre", 
"8_Pre", "9_Pre", "13_Pre", "2_Pre", "3_Pre", "14_Pre", "15_Pre", 
"16_Pre", "17_Pre", "18_Pre", "19_Pre", "20_Pre", "21_Pre", "1_Peak", 
"2_Peak", "3_Peak", "4_Peak", "5_Peak", "6_Peak", "7_Peak", "8_Peak", 
"13_Peak", "14_Peak", "15_Peak", "16_Peak", "17_Peak", "18_Peak", 
"19_Peak", "20_Peak", "21_Peak", "22_Peak", "23_Peak", "24_Peak", 
"10_Peak", "11_Peak", "12_Peak", "9_Peak"), decostand = "standardize", class = "data.frame")

Packages:

library(BiodiversityR) # also loads vegan
library(ggplot2)
library(readxl)
library(ggsci)
library(ggrepel)
library(ggforce)

I ran RDA:

rda2 <- rda(zpsp.hell ~ Depth + Temp + HypoTemperature + DO, 
            data = env2)

Then created these for the plots (as in the tutorial):

plot2 <- ordiplot(rda2, choices = c(1,2))
sites.long2 <- sites.long(plot2, env.data = env2)
species.long2 <- species.long(plot2)
axis.long2 <- axis.long(rda2, choices = c(1,2))
spec.envfit <- envfit(plot2, env=zpsp.hell)
spec.data.envfit <- data.frame(r=spec.envfit$vectors$r, p=spec.envfit$vectors$pvals)
species.long2 <- species.long(plot2, spec.data=spec.data.envfit)
species.long2 

Adding ordispider diagrams (THIS IS WHERE I AM GETTING AN ERROR)

library(ggforce)
ggplot() +
  geom_vline(xintercept = c(0), color = "grey70", linetype = 2) +
  geom_hline(yintercept = c(0), color = "grey70", linetype = 2) +
  xlab(axis.long2[1, "label"]) +
  ylab(axis.long2[2, "label"]) +
  scale_x_continuous(sec.axis = dup_axis(labels=NULL, name=NULL)) +
  scale_y_continuous(sec.axis = dup_axis(labels=NULL, name=NULL)) +
  ggforce::geom_mark_ellipse(data=sites.long2,aes(x=axis1, y=axis2, colour=Season,
                                         fill=after_scale(alpha(colour, 0.2))),
                            expand=0, size=0.2, show.legend=FALSE) +
  geom_segment(data=centroids.long(sites.long2, grouping=Season),
               aes(x=axis1c, y=axis2c, xend=axis1, yend=axis2, colour=Season),
               size=1, show.legend=FALSE) +
  geom_point(data=sites.long2,aes(x=axis1, y=axis2, colour=Season, shape=Season), 
             size=5) +
  BioR.theme +
  ggsci::scale_colour_npg() +
  coord_fixed(ratio=1) 

this gives the following error:

Error in eval(predvars, data, env) : object 'Season' not found

the error seems to start with the geom_segment argument

I am getting the same error for this one (next plot after the one above in the tutorial):

ggplot() +
  geom_vline(xintercept = c(0), color = "grey70", linetype = 2) +
  geom_hline(yintercept = c(0), color = "grey70", linetype = 2) +
  xlab(axis.long2[1, "label"]) +
  ylab(axis.long2[2, "label"]) +
  scale_x_continuous(sec.axis = dup_axis(labels=NULL, name=NULL)) +
  scale_y_continuous(sec.axis = dup_axis(labels=NULL, name=NULL)) +
  geom_mark_hull(data=sites.long2,aes(x=axis1, y=axis2, colour=Season,
                                      fill=after_scale(alpha(colour, 0.2))),
                 concavity=0.1, size=0.2, show.legend=FALSE) +
  geom_segment(data=centroids.long(sites.long2, grouping=Season),
               aes(x=axis1c, y=axis2c, xend=axis1, yend=axis2, colour=Season),
               size=1, show.legend=FALSE) +
  geom_point(data=sites.long2,aes(x=axis1, y=axis2, colour=Season, shape=Season), 
             size=5) +
  BioR.theme +
  ggsci::scale_colour_npg() +
  coord_fixed(ratio=1) 

As in the tutorial, the BioR.theme:

BioR.theme <- theme(
  panel.background = element_blank(),
  panel.border = element_blank(),
  panel.grid = element_blank(),
  axis.line = element_line("gray25"),
  text = element_text(size = 12),
  axis.text = element_text(size = 10, colour = "gray25"),
  axis.title = element_text(size = 14, colour = "gray25"),
  legend.title = element_text(size = 14),
  legend.text = element_text(size = 14),
  legend.key = element_blank())

I am not sure what is wrong. When I look at the sites.long2 df, it does have the Season column (factor with 3 levels). I ran the code in the tutorial with the dune df (included with vegan [i think]) and it plots with no problem. I looked online but can't figure out what I am doing wrong.

Thank you!

FishyFishies
  • 301
  • 3
  • 14

0 Answers0