Herb
Herb

Reputation: 21

ggplot2 legend when each layer has a single variable

Sorry for this basic question but I'm new to ggplot2 and am probably not using correct terminology in my search for an answer. I have a scatterplot with two layers. The base is individual observations at various times and the overlay is the mean of those observations at the same times. All data is in long format. Sample code below produces the correct plot but I can't figure out how to create the legend that links + to individual observations and circles to the means of those observations. Thanks for helping a newbie.

x <- c(1, 2, 3)  
x <- rep(x, each = 3)  
y <- c(3, 2, 5, 5, 6, 3, 8, 9, 6)  
df1 <- data.frame(x, y)
df2 <- aggregate(y ~ x, df1, mean)
p1 <- ggplot(data = df1, aes(x = x, y = y)) +
  geom_point(pch = 3, size =2) + # Individual observations
  geom_point(data = df2, aes(x = x, y = y)) + # Means
  geom_line(data = df2, aes(x = x, y =y)) # Connect means

Upvotes: 1

Views: 79

Answers (1)

erocoar
erocoar

Reputation: 5893

That is because to display a legend you have to specify a shape argument in the aes() function that maps data to visual parameters.

The easiest way would be to combine your two dataframes and add a third column specifying the type...

df3 <- as.data.frame(rbind(df1, df2))
df3$type <- rep(c("Ind. Obs.", "Means"), c(nrow(df1), nrow(df2)))

Then you need to specify the shape parameter to correspond to df3$type. Also to get the shapes that you want you will need to call scale_shape_manual().

p1 <- ggplot() +
  geom_point(data = df3, aes(x = x, y = y, shape = type)) +
  scale_shape_manual(values = c(3, 19)) +
  geom_line(data = df2, aes(x = x, y =y)) # Connect means
p1

Upvotes: 1

Related Questions