0

I have done species accumulation curves and would like to plot the SAC results of different substrate sizeclasses in the same ggplot, with expected species richness on y-axis and number of sites samples on x-axis. The data features a cumulative number of samples in each sizeclass (column "sites"), the expected species richness (column "richness"), and substrate size classes 10, 20 and 30 (column "sc").

   sites  richness sc
1     1 0.6696915 10
2     2 1.2008513 10
3     3 1.6387310 10
4     4 2.0128472 10
5     5 2.3424933 10
6     6 2.6403239 10

     sites richness sc
2836     1 1.000000 20
2837     2 1.703442 20
2838     3 2.249188 20
2839     4 2.706618 20
2840     5 3.110651 20
2841     6 3.479173 20

I want each sizeclass to have unique linetype. I used the following code for ggplot:

sac_kaikki<-ggplot(sac_data, aes(x=sites, y=richness,group=sc)) +
  geom_line(aes(linetype=sc))+
  coord_cartesian(xlim=c(0,100))+
  theme(axis.title.y = element_blank())+
  theme(axis.title.x = element_blank())

However, instead of getting three neat lines in different linetypes, I got [this jumbly muddly messy thing with more stripes than a herd of zebras][1]: https://i.stack.imgur.com/iD75K.jpg. I am sure the solution is rather simple, but for my life I am not able to figure it out.

// as Brookes kindly pointed out I should add some reproducible data, here is a subset of my data with dput, featuring 10 first observations of size classes 10 and 20:

dput(head(subset(sac_data,sac_data$sc=="10"),10))
structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.669691470054462, 
1.20085134466255, 1.63873100707468, 2.01284716414471, 2.34249332096243, 
2.64032389106845, 2.91468283244696, 3.17111526890278, 3.41334794519086, 
3.64392468817362), sc = c("10", "10", "10", "10", "10", "10", 
"10", "10", "10", "10")), .Names = c("sites", "richness", "sc"
), row.names = c(NA, 10L), class = "data.frame")

dput(head(subset(sac_data,sac_data$sc=="20"),10))
structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.999999999999987, 
1.70344155844158, 2.24918831168832, 2.70661814764865, 3.11065087175364, 
3.47917264517669, 3.82165739030286, 4.14341144680334, 4.44765475554031, 
4.73653870494466), sc = c("20", "20", "20", "20", "20", "20", 
"20", "20", "20", "20")), .Names = c("sites", "richness", "sc"
), row.names = 2836:2845, class = "data.frame")

// okay so for whatever reason, the plot works just fine if I plot only two sizeclasses, but including the third one produces the absurd plot I posted a picture of.

structure(list(sites = 1:10, richness = c(0.42857142857143, 0.838095238095238, 1.22932330827066, 1.60300751879699, 1.95989974937343, 2.30075187969924, 2.62631578947368, 2.93734335839598, 3.23458646616541, 3.5187969924812 ), sc = c("30", "30", "30", "30", "30", "30", "30", "30", "30", "30")), .Names = c("sites", "richness", "sc"), row.names = c(NA, 10L), class = "data.frame")

Vilukko
  • 5
  • 3
  • Can you try `geom_path` instead of `geom_line` – Jack Brookes Mar 24 '18 at 20:43
  • If you want specific answers you need to produce a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jack Brookes Mar 24 '18 at 20:46
  • Thank you Brookes! I included a subset of the data in the correct form now (or so I hope). I just found out that when I have only 2 groups ( = two sizeclasses), the ggplot works just fine. However, for some reason adding the third one produces a plot like the one I posted a picture of. – Vilukko Mar 24 '18 at 21:13

1 Answers1

0

Works fine for me with your sample data:

a <- structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.669691470054462, 
                                                                      1.20085134466255, 1.63873100707468, 2.01284716414471, 2.34249332096243, 
                                                                      2.64032389106845, 2.91468283244696, 3.17111526890278, 3.41334794519086, 
                                                                      3.64392468817362), sc = c("10", "10", "10", "10", "10", "10", 
                                                                                                "10", "10", "10", "10")), .Names = c("sites", "richness", "sc"
                                                                                                ), row.names = c(NA, 10L), class = "data.frame")
b <- structure(list(sites = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), richness = c(0.999999999999987, 
                                                                           1.70344155844158, 2.24918831168832, 2.70661814764865, 3.11065087175364, 
                                                                           3.47917264517669, 3.82165739030286, 4.14341144680334, 4.44765475554031, 
                                                                           4.73653870494466), sc = c("20", "20", "20", "20", "20", "20", 
                                                                                                     "20", "20", "20", "20")), .Names = c("sites", "richness", "sc"
                                                                                                     ), row.names = 2836:2845, class = "data.frame")

c <- structure(list(sites = 1:10, richness = c(0.42857142857143, 0.838095238095238, 
                                               1.22932330827066, 1.60300751879699, 1.95989974937343, 2.30075187969924, 
                                               2.62631578947368, 2.93734335839598, 3.23458646616541, 3.5187969924812
), sc = c("30", "30", "30", "30", "30", "30", "30", "30", "30", 
          "30")), .Names = c("sites", "richness", "sc"), row.names = c(NA, 
                                                                       10L), class = "data.frame")

sac_data <- bind_rows(a, b, c)

Plotting:

ggplot(sac_data, aes(sites, richness, group = sc)) + 
  geom_line(aes(linetype = sc))

enter image description here

Jack Brookes
  • 3,720
  • 2
  • 11
  • 22
  • Well that is weird. For whatever reason, now it works for me too. I have no idea what happened before. Thank you for your answer and using your time for this! – Vilukko Mar 25 '18 at 13:01
  • Based on your original graph, it was probably your data that was zig-zagging. If you are using rstudio, be sure to clear your environment often such that you know you are always working with the correct dataset. – Jack Brookes Mar 25 '18 at 13:05