0

I was trying to create a scatter graph of two fish I've tracked showing them at different depths over time. I've tried to subset them out of the main data frame but the graph only shows one.

p1 <- ggplot(subset(Data.df,ID %in% c(Data.df$ID == "9" , "11")))
p1 <- p1 + geom_point(aes(dt, d, colour=as.factor(ID), shape=as.factor(ID)))
p1 <- p1 + scale_shape_manual(values=c(3, 6))
p1 <- p1 + scale_color_manual(breaks = c("ID == 9", "ID == 11"), values=c("red", "blue"))
p1

The graph below is what the above produced but as you can see its only showing the results for fish 11. How do I get it to show both.

enter image description here

Student121
  • 15
  • 4
  • Can you please provide a sample of your data, and reproducible code using `dput`and `structure`? This will make it easier to help. See also here https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. You might be able to use a facet. – johnjohn Aug 14 '20 at 11:12

1 Answers1

2

Your mistake is is your subset. Data.df$ID == "9" creates a logical vector of length Data.df$ID and then c(Data.df$ID == "9" , "11") coerces it into character because of "ll". So your subset is trying to match "TRUE", "FALSE", or "11" and only "11" is found. Also, you don't need the breaks argument to scale_color_manual.

p1 <- ggplot(subset(Data.df,ID %in% c("9" , "11")))
p1 <- p1 + geom_point(aes(dt, d, colour=as.factor(ID), shape=as.factor(ID)))
p1 <- p1 + scale_shape_manual(values=c(3, 6))
p1 <- p1 + scale_color_manual(values=c("red", "blue"))
p1
Ben Norris
  • 5,639
  • 2
  • 6
  • 15