问题
Unfortunately, I think this is a tough item to reproduce, but I think the question should be simple enough to answer with a visual...
I'd like to build a legend for three specific dimensions in geom_point.
Any Hockey Fans Out There?
I'd like to build a legend for the dimensions that have colors on this chart. They are three different players I'd like to highlight, the rest of the points on the plot being general noise, but necessary for a visual.
In my opinion, here a legend would be more appealing than labels.
I know this is kind of ridiculous without being able to reproduce, but I hope the question is general enough (though I couldn't find an answer that satisfied what I was looking for) that it can be easily solved.
Happy to field questions.
Thanks!
回答1:
Solved... the code looks like this:
library(ggplot2)
Offense <- read.csv("Offense1.csv")
plot <- ggplot(Offense[Offense$Gm>20,], aes(CF.Rel, SCF.Rel)) + geom_point() +
geom_point(data=Offense[Offense$Name == "Eric.Staal",], aes(colour="Eric Staal"), size=4) +
geom_point(data=Offense[Offense$Name == "Rick.Nash",], aes(colour="Rick Nash"), size=4) +
geom_point(data=Offense[Offense$Name == "Tanner.Glass",], aes(colour="Tanner Glass"), size=4)
plot <- plot + labs(title = "Driving Offense",
x = "Relative Corsi For %",
y= "Relative Scoring Chances For %")
plot <- plot + scale_colour_discrete(name="Player")
plot
They key here was to make the aesthetic color the dimension you want to include in the legend.
来源:https://stackoverflow.com/questions/35711592/r-legend-for-specific-points-in-ggplot