Gplot- plotting multiple relationship predictions

I've been modelling relationships of a continuous variable against a response variable. In the case below, I modelled the column ground (which number of ground species at a site ) against the aridity (of the site), grabbing from my data set (alldata).
This is the code I have been using to:

  • model the relationship.
    m5 <- gam(ground ~ s(aridity), data=alldata, family=poisson())

  • then predict to a new data set
    newdatm10=data.frame(aridity=seq(min(alldata$aridity), max(alldata$aridity), length=100))
    m10preds<- predict(m10, newdata=newdatm10, type="link" ,

  • calculate mean and 95% CIs. exp back transforms the predictions to probabilities#

  • join it all together
    predictionm10=cbind(newdatm10, meanm10, lowm10, uppm10)

  • then graph
    geom_point(data=alldata, aes(x=aridity, y=ground), size=2, shape=1)+
    geom_ribbon(data=predictionm10, aes(x=aridity, ymin=lowm10, ymax=uppm10), color="NA", alpha=0.4, fill="grey")+
    geom_line(data=predictionm10, aes(x=aridity, y=meanm10), size=1)+
    labs(x="Aridity", y="Ground Species Richness")+
    guides(linetype = guide_legend(override.aes = list(size=0.8)))+#adjusts line size in legend
    guides(linetype = guide_legend(override.aes=list(fill=NA)))


My issue is, that I am now wanting to graph multiple response variables against aridity and show it on one graph. For example, I have another column that is for Aerial species or Canopy species. But I just can't work out a way to get them on there together with a legend so it's easy to tell which one is which.
This is the best I have gotten so far:

graphm18 = ggplot()+
geom_point(data=alldata, aes(x=aridity, y=ground), color="black", size = 2, shape = 1) +
geom_point (data=alldata, aes(x=aridity, y=canopy), color="tomato2", size = 2, shape = 2) +
geom_line (data=predictionm18, aes(x=aridity, y=meanm18), color="gray10", linetype="solid", size = 0.5) +
geom_line (data=predictionm19, aes(x=aridity, y=meanm19),color="gray10", linetype="dashed", size = 0.5) +
geom_ribbon(data=predictionm18, aes(x=aridity, ymin=lowm18, ymax=uppm18), color="NA", alpha=0.4, fill="black")+
geom_ribbon(data=predictionm19, aes(x=aridity, ymin=lowm19, ymax=uppm19), color="NA", alpha=0.4, fill="tomato2")+
labs(x="Aridity", y="Species Richness") +
scale_x_continuous (limits = c(0,82), breaks=seq(0, 82, 10), expand = c (0, 0)) +
scale_y_continuous (limits = c(0,30), breaks=seq(0, 30, 5), expand = c (0, 0))


  • I apologise for not having the reprex. I was having issues trying to pull out the data to be able to replicate for the reprex.

The data doesn't have to be all your data, or even your data, or even real data—it just needs to illustrate what the question is to be addressed. R is analysis—breaking data into the constituent parts that can be used as arguments to one-or-more off-the-shelf or bespoke functions. The threshold task is also analysis—framing a question so that it is readily understandable by someone who can see the data.

Here, two things will be helpful.

  1. If alldata is large, reduce it to just the ground and aridity fields and no more than 100 rows.
  2. Generate predictionm10 from the reduced alldata.
  3. run dput(alldata) and dput(predictionm10) and cut-and-paste into a script (with the library(ggplot2) and any other required libraries)
  4. Trim the ggplot by omitting labs(x="Aridity", y="Ground Species Richness") and everything following, as those lines are not germane to the principal problem.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.