I am trying to create a legend when using ggplot2 and geom_point. I have a dataframe that looks like this
d <- data.frame(variable = c("A", "B", "C", "D", "E", "F", "G", "H", "I", "J"),
value = rnorm(10, 3),
Schoolave = rnorm(10, 3),
districtave = rnorm(10, 3),
max = rnorm(10, 3),
min = rnorm(10, 3))
I want to make a plot that looks like this.
plot <- ggplot(data = d, aes(x = variable, y = value)) + geom_errorbar(ymax = d$max, ymin = d$min)
plot <- plot + coord_flip()
plot <- plot + geom_point(data = d, aes(x = variable, y = value),
shape = 1, size = 5)
plot <- plot + geom_point(data = d, aes(x = variable, y = districtave), shape = 0, size = 4)
plot <- plot + geom_point(data = d, aes(x = variable, y = Schoolave), shape = 2, size = 3)
plot <- plot + theme_bw() + theme(legend.position= "bottom")
plot
I would like a legend that tells them the circle for your average score. The triangle is the average score for your school. The square is the average for the district. I have looked for a way to do this can't find a way. Any help would be appreciated.
ggplotlikes tidy data, which means you have to melt your data into long format.Now your data has a single column that we'll map to
shape, so the auto legend will work. And there's no need to add to the same plot and save it every single line, just add it all at once. And please don't specify thedata$columninside ofaes()! It will cause problems if you ever want to facet or do more advanced plots. You specify the data upfront, so you don't have to say it again insideaes(). And do useaes()for all your aesthetic mappings, like the min and max of the errorbar.If you want nicer labels, you can specify them in the shape scale. On a horizontal legend, I like to add in a little white space, something like this: