head(data.scores.pa) NMDS1 " /> head(data.scores.pa) NMDS1 " /> head(data.scores.pa) NMDS1 "/>

Error in FUN(X[[i]], ...) : object 'Year' not found when plotting ordination in ggplot

259 Views Asked by At

I am having an issue with the ggplot code line where R doesn't like the "group = Year".

Here is what my data looks like:

> head(data.scores.pa)
    NMDS1      NMDS2       NMDS3 Site Year Elevation Fire history 
1 -0.737547 0.73473457  0.7575643   BF 2004      1710        Burnt
......


> head(spp.scrs2)
            species        MDS1        MDS2  pval
1 Acrothamnus.montanus  0.8383 -0.02382347 1e-04
........


> head(vec.sp.df.pa)
            MDS1      MDS2   species pvals
Elevation 0.834847 0.747474 Elevation 0.005



Here is the code I am using:

>xy <- ggplot(data.scores.pa, aes(x = NMDS1, y = NMDS2, group = Year)) +  
geom_point(size = 3, aes(shape = Fire history, colour = Year))+ 
stat_ellipse(mapping = NULL, data = NULL, geom = "path", position = "identity", type = "t", level = 0.95, segments = 51, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE) + 
geom_segment(data=vec.sp.df.pa, aes(x=0,xend=MDS1,y=0,yend=MDS2),
            arrow = arrow(length = unit(0.5,"cm")),colour="grey")+
geom_text_repel(data=vec.sp.df.pa,aes(x=MDS1,y=MDS2,label=species),size=2)+
geom_segment(data=spp.scrs2,aes(x=0,xend=MDS1,y=0,yend=MDS2),
            arrow = arrow(length = unit(0.5, "cm")),colour="black")+ 
geom_text_repel(data=spp.scrs2, aes(x=MDS1,y=MDS2,label=species),size=2)+ 
annotate("text", x = -1.6, y = 1, label = paste0("3D stress: ", format(ord.pa$stress, digits = 4)), hjust = 0) + 
theme_cowplot() + scale_color_brewer(palette = "BrBG", direction = 1) + 
theme(panel.border = element_rect(colour = "black"))+ 
ggtitle("All Sites - distance data using Bray-Curtis")+ 
labs(x = "NMDS1", y = "NMDS2")
> Error in FUN(X[[i]], ...) : object 'Year' not found

However, when I remove the geom_segment and geom_text_repel code lines it fixes the problem and I am able to plot the graph...

Is anyone able to provide some insight into this issue?

Thank you!

0

There are 0 best solutions below