0

Using ggplot2, I'm trying to make a plot that has two groupings, one of organism, the other by treatment. Both organisms have the same Treatments.

For example, I'd like the lines of the invaded fungi to connect to the invaded fungi (and all the other paired points) but I can't seem to make it work. Here is the code I'm using.

ggplot(bnti.avg, aes(x=Season, y=Mean.BNTI, group=Treatment, color=Treatment, 
shape=Organism)) + 
  geom_errorbar(aes(ymin=Mean.BNTI-SD, ymax=Mean.BNTI+SD), width=.1) +
  geom_line() +
  geom_point(size = 8)

I have tried moving around the aes to have some part just in geom_line or geom_point but I seem to be getting no where. Suggestions?

  • 2
    Welcome to SO! To help us to help you could you please make your issue reproducible by sharing a sample of your **data**? See [how to make a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Simply type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Jun 07 '21 at 22:07

1 Answers1

0

The group variable must be combination of Treatment & Organism. Without your actual data here is the psuedo code. This should work in theory. Please check and if possible please share a minimal reproducible example as @stefan comment

bnti.avg$grouping_var <- paste0(bnti.avg$Treatment, bnti.avg$Organism)

ggplot(bnti.avg, aes(x=Season, y=Mean.BNTI, group=grouping_var, color=Treatment, 
shape=Organism)) + 
  geom_errorbar(aes(ymin=Mean.BNTI-SD, ymax=Mean.BNTI+SD), width=.1) +
  geom_line() +
  geom_point(size = 8)
Sinh Nguyen
  • 4,277
  • 3
  • 18
  • 26