-2
ggplot() +
      # blue plot
      geom_point(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(native[MainIsl == "Main"&organism == "Plant"]))) + 
      geom_smooth(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(native[MainIsl == "Main"&organism == "Plant"])), fill="blue",colour="darkblue") +
      # red plot
      geom_point(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(exotic[MainIsl == "Main"&organism == "Plant"]))) + 
      geom_smooth(data=Cen, aes(x=log(area[MainIsl == "Main"&organism == "Plant"]), y=log(exotic[MainIsl == "Main"&organism == "Plant"])), fill="red",colour="red")

I used the above code but R gave me an error. The data set I have contains columns: MainIsl(Mainland or island), organisms(Plant or bird), # of natives, # of exotics, total #, and area.

I want to plot area against #of natives, exotics, and total, based on its organism and MainIsl. I want to put points belongs to the same MainIsl and the same organism all together in one plot, different color indicating whether the point is showing #natives or exotics or total.

Is this a correct way to implement this idea using ggplot?

markus
  • 25,843
  • 5
  • 39
  • 58
  • 2
    It's hard to say without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). As it stands, we can't see either your data or your output, but if you're adding multiple of the same geom just to change an aesthetic, you'd probably do well to read back through a `ggplot2` tutorial and reshape your data – camille Feb 23 '19 at 00:00

1 Answers1

1

I second everything Camille said in the comment above. Here is an example of what you could do:

library(tidyverse)

# example data
Cen <- data.frame(MainIsl = sample(c("Mainland", "Island"), 100, replace = T), organism = sample(c("Plant", "Bird"), 100, replace = T),
                  native = sample(1:100, 100, replace = T), exotic = sample(1:100, 100, replace = T), area = sample(1:100, 100, replace = T)
            )

Cen$Total <- Cen$native + Cen$exotic

#  convert to long format
Cen2 <- gather(Cen, key = "Sp", value = "Richness", -MainIsl, -organism, -area)

# plot
ggplot(data = Cen2, aes(x = log(area), y = log(Richness), colour = Sp)) +
  geom_point() +
  geom_smooth()+
  facet_grid(MainIsl~organism)

enter image description here

flee
  • 1,253
  • 3
  • 17
  • 34