Open PeSteff opened 6 months ago
After playing with the data even more I think I was able to narrow down the problem and reproduce it on dummy data. Whenever a group includes two or less data points the error (Error in anchors[[i]] : subscript out of bounds) occurs. And I think it has something to do with label placement, as without labelling, a hull was drawn around the group with two points. Yet the graph still remains incomplete as is demonstrated with the code below.
Is there a way to fix this or work around? I do not need the labels in the plot as a nice legend is produced either way. But I have not found a way to simply switch off the labelling and have all the hulls created.
# dummy data
gr <- c("A","B","C","D","E")
mock_dat <- data.frame(NMDS1 = sort(rep(1:5,3)) - rep(c(0,0.2,0.3),5),
NMDS2 = c(2,2,2,1,1,1,1,1,1,2,2,2,2,2,2) - c(rep(c(-0.1,0,0.2),2),rep(c(0,-0.25,0.1),3)),
group3 = sort(rep(gr[1:3],5)),
group_with2 = c(rep("A",2),rep("B",9),rep("C",4)),
group_with2_rev = c(rep("A",4),rep("B",9),rep("C",2)))
# trying to create a plot with hulls when one group contains only 2 data points
p1<-ggplot(data=mock_dat,aes(x=NMDS1,y=NMDS2))+
geom_point()+
geom_mark_hull(aes(fill=group_with2, label=group_with2))
p1
# setting label = NULL at least draws the first hull, with the group of two
p2<-ggplot(data=mock_dat,aes(x=NMDS1,y=NMDS2))+
geom_point()+
geom_mark_hull(aes(fill=group_with2, label=NULL))
p2
# when I reverse the order of groups, so that the group with 2 point is last (C in A, B, C) all other hulls are drawn
p3 <-ggplot(data=mock_dat,aes(x=NMDS1,y=NMDS2))+
geom_point()+
geom_mark_hull(aes(fill=group_with2_rev, label=NULL))
p3
# simply removing the "label" argument doesn't fix the problem as still not all hulls are created
p4 <-ggplot(data=mock_dat,aes(x=NMDS1,y=NMDS2))+
geom_point()+
geom_mark_hull(aes(fill=group_with2))
p4
# the plot is produced when all groups contain at least 3 data points
p5 <-ggplot(data=mock_dat,aes(x=NMDS1,y=NMDS2))+
geom_point()+
geom_mark_hull(aes(fill=group3, label=group3))
p5
I think this is fixed in the development version. Can I get you to test that out?
Thanks for the recommendation. I will test it. At the moment I get an error when trying to install the development version using
devtools::install_github("thomasp85/ggforce")
Something including a lot of "warning: 'if constexpr' only available with '-std=c++17' or '-std=gnu++17'" and several errors from concaveman (see attached file)
I am trying to update R and then I will try again.
I tried the very same code with the developmental version and the newest R version (4.3.3) and it worked without any problems!
Thank you so much!
Hello,
EDIT: in the first comment there is a better reproducable example.
I am using ggplot2 with the packages ggforce (geom_mark_hull) and ggrepel (geom_text_repel) to plot the results of my NMDS analysis (vegan). I have managed a 1) nice plot with one set of variables, yet 2) choosing another variable to base the hulls on, it was not possible but returned a (for me) cryptic error. I have checked the class of the variables, it's the same (character) and 3) played around with many options of geom_mark_hull, nothing helped. I created a small subset of my data (provided below), still the same error. Also I created 4) mock_data and tried to reproduce the error but I couldn't. Which is good, so the code works really well. But I also couldn't find out what is wrong with my data or what to do to resolve the error.
1) nice plot
2) different variable used for the hulls
Running the above piece of code the error appears when I want to see the plot with the last line. The error is this: "Error in anchors[[i]] : subscript out of bounds"
3) playing around The one thing I found to have a plot created at all (and without any error message) was to set
label = NULL
in the aes of geom_mark_hull. But this resulted in a plot where only the first of three hulls was drawn. If I reversed the order of my groups, all but the first hulls were drawn.4) creating mock data and test
Recreating such a plot with made up data worked fine as well. I am very puzzled what this error message means, I tried to make sense of it by looking at source code "mark_hull.R" here on github. But this is beyond my level of understanding and I was not able to find where this error message was pointing to.
I would appreciate any help to figure this out.