1
votes

I am trying to add a legend to a geom_point ggplot2 of two unequal lengths of data. I tried to mapping the colours into the aesthetics for in the ggplot() but because of the different lengths of data I get an error. Is there a way to add an legend to get around the issue of different vector lengths?

conc <- c(0.004, 0.003, 0.003, 0.003, 0.004, 0.003, 0.004, 0.008, 0.020)
time <- c(seq(from=1,to=length(conc)))
conc <- c(0.007, 0.012, 0.002, 0.003, 0.003, 0.004, 0.007, 0.003, 0.004, 0.005, 0.004, 0.016)
time <- c(seq(from=1,to=length(conc)))
data1 <- data.frame(time,conc)
data2 <- data.frame(time,conc)

ggplot()+ coord_cartesian(ylim = c(0,0.075))+ 
geom_point(data=data1,aes(time,conc),shape=10,size=.02,color='black')+
geom_step(data=data2,aes(time,conc),size=.1,color='black')+
xlab("Sampling Time (sec)")+
ylab("Concentration (#/cm^3)")
1
Please edit your post and add your data, so that your example becomes reproducible. dput(spAm) etc. might be useful.lukeA
stackoverflow.com/questions/16311335/… (Also, unequal lengths shouldn't be the problem. You can combine the two data sets through rbind).oshun
When I try to input the variables for each dataset into the aethestics I get the error "of Aesthetics must be either length 1 or the same as the data (12): x, y" If I were to use combine the datasets, what arrangement would I have to use for ggplot? I am new to using ggplot2 so this may seem like a naive question. @oshun you suggested that I use rbind for them, then do I read in selected subsets of the datasetsEJJ
ggplot is great when you have all your data in one data.frame. You add another grouping column to differentiate your subdata (ex. data2$category <- "myStep"). You can assign colors, shapes, transparency according to the different groupings. I cannot replicate your error but I'm guessing you are setting aes` for one dataset but trying to plot both?oshun

1 Answers

0
votes

I cannot replicate the OP's error, so I'm guessing the question is: "How to add a legend with separate geom elements?"

2 approaches here:

  1. Basic: one dataset, legend from aes()
  2. Advanced: customized legend keys (copied almost directly from the linked answer).

First, it's usually good practice to send a single dataset into ggplot and add grouping variables as their own columns. Set your desired properties based on these groupings (i.e. colour, fill, size, shape, alpha). ggplot will construct a legend based on these grouping variables. (Note: A single data.frame isn't necessary for this specific case because you are already subsetting your data to make different geoms.)

#Example data
conc <- c(0.004, 0.003, 0.003, 0.003, 0.004, 0.003, 0.004, 0.008, 0.020)
time <- c(seq(from=1,to=length(conc)))
data1 <- data.frame(time,conc)    
conc <- c(0.007, 0.012, 0.002, 0.003, 0.003, 0.004, 0.007, 0.003, 0.004,
          0.005, 0.004, 0.016)
time <- c(seq(from=1,to=length(conc)))
data2 <- data.frame(time,conc)

#Create grouping variables
data1$category <- "myPoint"
data2$category <- "myStep"
data3 <- rbind(data1, data2)

Basic approach:

#Graph the combined data, setting the overall aes()
#but subset the data for the different visual elements (points and steps)
ggplot(data3, aes(time, conc, colour=category))+ coord_cartesian(ylim = c(0,0.075))+ 
  geom_point(data=data3[data3$category == "myPoint",], shape=10,size=2)+
  geom_step( data=data3[data3$category == "myStep", ], size=1)+
  xlab("Sampling Time (sec)")+
  ylab("Concentration (#/cm^3)") + 
  ggtitle("Basic")

Advanced approach:

#Specify a custom colour in each variable's aesthetics
#Hack the legend: color = guide_legend(override.aes = list(...))
ggplot()+ coord_cartesian(ylim = c(0,0.075))+ 
  geom_point(data=data1,aes(time,conc, color='point'),shape=10,size=2)+
  geom_step(data=data2,aes(time,conc, color='step'),size=1)+
  xlab("Sampling Time (sec)")+
  ylab("Concentration (#/cm^3)") +
  ggtitle("Advanced") +
  scale_colour_manual(name = "legend", 
                      values = c("point" = "black", "step" = "black")) +
  guides(color=guide_legend(override.aes=list(
               shape=c(10, NA),
               linetype=c(0,1) 
                )))

same legend, different geoms override.aes point and line different geoms