How can I make a legend in ggplot2 with one point

2020-02-12 12:14发布

问题:

I am making a graph in ggplot2 consisting of a set of datapoints plotted as points, with the lines predicted by a fitted model overlaid. The general idea of the graph looks something like this:

names <- c(1,1,1,2,2,2,3,3,3)
xvals <- c(1:9)
yvals <- c(1,2,3,10,11,12,15,16,17)
pvals <- c(1.1,2.1,3.1,11,12,13,14,15,16)
ex_data <- data.frame(names,xvals,yvals,pvals)
ex_data$names <- factor(ex_data$names)

graph <- ggplot(data=ex_data, aes(x=xvals, y=yvals, color=names))
print(graph + geom_point() + geom_line(aes(x=xvals, y=pvals)))

As you can see, both the lines and the points are colored by a categorical variable ('names' in this case). I would like the legend to contain 2 entries: a dot labeled 'Data', and a line labeled 'Fitted' (to denote that the dots are real data and the lines are fits). However, I cannot seem to get this to work. The (awesome) guide here is great for formatting, but doesn't deal with the actual entries, while I have tried the technique here to no avail, i.e.

print(graph + scale_colour_manual("", values=c("green", "blue", "red")) 
+ scale_shape_manual("", values=c(19,NA,NA)) 
+ scale_linetype_manual("",values=c(0,1,1))) 

The main trouble is that, in my actual data, there are >200 different categories for 'names,' while I only want the 2 entries I mentioned above in the legend. Doing this with my actual data just produces a meaningless legend that runs off the page, because the legend is trying to be a key for the colors (of which I have way too many).

I'd appreciate any help!

回答1:

I think this is close to what you want:

ggplot(ex_data, aes(x=xvals, group=names)) + 
  geom_point(aes(y=yvals, shape='data', linetype='data')) + 
  geom_line(aes(y=pvals, shape='fitted', linetype='fitted')) + 
  scale_shape_manual('', values=c(19, NA)) + 
  scale_linetype_manual('', values=c(0, 1))

The idea is that you specify two aesthetics (linetype and shape) for both lines and points, even though it makes no sense, say, for a point to have a linetype aesthetic. Then you manually map these "nonsense" aesthetics to "null" values (NA and 0 in this case), using a manual scale.



回答2:

This has been answered already, but based on feedback I got to another question (How can I fix this strange behavior of legend in ggplot2?) this tweak may be helpful to others and may save you headaches (sorry couldn't put as a comment to the previous answer):

ggplot(ex_data, aes(x=xvals, group=names)) + 
  geom_point(aes(y=yvals, shape='data', linetype='data')) + 
  geom_line(aes(y=pvals, shape='fitted', linetype='fitted')) + 
  scale_shape_manual('', values=c('data'=19, 'fitted'=NA)) + 
  scale_linetype_manual('', values=c('data'=0, 'fitted'=1))


标签: r ggplot2