0
votes

I have plotted x and y variables in a scatter plot with a legend that provides a shape and color for each point based off a single sample id variable. I want to overlay points from a second data frame, however when I try to add in the points from the second data frame I get an error saying it can't find the variable that I used to specify the color and shape of the points from the original data frame. I am using this code:

p=ggplot(HebWater, aes(x = PCSrCa.24, y = SrIso, group = Location, 
color =     Location, shape = Location)) +
geom_point(size=6) +
scale_shape_manual(values = 1:17) +
theme(panel.grid.major=element_line(colour="white"),
panel.grid.minor=element_blank(),panel.background=element_rect(colour="black",fill="white"))
p
p1=p+geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))
p1
p2=p1+geom_point(data=HebOto, aes(SrCa,SrIso))
p2  

Everything works fine until I try to run the code for graph p2, I was already successful plotting both data frames on the same graph but was not able to get the legend to display properly no matter how I tried to change the shape, and color parameters using this code:

ggplot(HebWater, aes(PCSrCa.24, SrIso))+
geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
scale_shape_manual(values=1:17)+
geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))+
geom_point(data=HebOto, aes(SrCa,SrIso))+
geom_point(data=HebWater, show_guide=TRUE, shape=c(1:17), colour=c(1:17),  size=6)+
theme(panel.grid.major=element_line(colour="white"),
panel.grid.minor=element_blank(),
panel.background=element_rect(colour="black",fill="white"))

My data frames are both organized in this fashion:

> head(HebWater)
         Location   SrIso PCSrCa.24     SDSrIso    SDSrCa PCSrCa.28
1   Gib (baseflow) 0.70966 0.2911440 0.000643719 0.0308056 0.3396680
2 Fire (baseflow)  0.71006 0.1119312 0.000643719 0.0308056 0.1305864
3 Mad R (runoff)   0.71052 0.2043264 0.000643719 0.0308056 0.2383808
1
You should try 1. to give data that other people can use (you provide only 3 lines of only one of your data sets) and 2. to diminish your code until you have a minimal code that still produce the same error. Here all the theme has nothing to do with your error and should be removed. - Arthur
Is there a Location column in HebOto? If not, try changing the definition of p to: p=ggplot(HebWater, aes(x=PCSrCa.24, y=SrIso))+geom_point(aes(color=Location, shape=Location), size=6) + ... - jlhoward

1 Answers

1
votes
HebWater <- data.table(
  SrIso     = runif(20),
  SDSrIso   =  runif(20),
  PCSrCa.24 =  runif(20),
  SDSrCa    =  runif(20)
)
HebOto <- data.table(
  SrCa     = runif(20),
  SrIso   =  runif(20)
)

library(ggplot2)

ggplot(HebWater, aes(PCSrCa.24, SrIso))+
  geom_errorbar(aes(ymin=SrIso-SDSrIso*3, ymax=SrIso+SDSrIso*3))+
  scale_shape_manual(values=1:17)+
  geom_errorbarh(aes(xmin=PCSrCa.24-SDSrCa*3, xmax=PCSrCa.24+SDSrCa*3))+
  geom_point(data=HebOto, aes(SrCa,SrIso))+
  geom_point(data=HebWater, show_guide=TRUE, shape=c(1:17), colour=c(1:17),  size=6)

Produces this error:

Error: Incompatible lengths for set aesthetics: shape, colour, size

This seems logical to me since you fix the shape, color and size of your points in the second geom_point(). There is no legend to give since those do not depend at all on the data! If you want a legend, you have to use aes(shape=some_variable1, colour=some_variable2, size=some_variable3) and then if necessary force the randering with scale_xxxxxx_manual().