The data I'm playing with comes from
the internet source listed
below
nba <-
read.csv("http://datasets.flowingdata.com/ppg2008.csv",
sep=",")
What I want
to do, is create a 2D points graph comparing two metrics from this table, with each
player representing a dot on the graph. I have the following code:
nbaplot <-
ggplot(nba, aes(x= MIN, y= PTS, colour="green", label=Name)) +
geom_point()
This gives me the
following:
src="https://i.stack.imgur.com/RVZz8.png" alt="NBA
Plot">
What I want is a label of player's
name right next to the dots. I thought the label function in ggplot's aesthetics would
do this for me, but it didn't.
I
also tried text()
function and the
textxy()
function from
library(calibrate)
, neither of which appears to work with
ggplot.
How can I add name labels to these
points?
Answer
Use geom_text
, with
aes
label. You can play with hjust,
to adjust text
vjust
position.
ggplot(nba, aes(x= MIN,
y= PTS, colour="green", label=Name))+
geom_point()
+geom_text(aes(label=Name),hjust=0,
vjust=0)
src="https://i.stack.imgur.com/EGOIn.png" alt="enter image description
here">
EDIT: Label only values above a
certain threshold:
ggplot(nba,
aes(x= MIN, y= PTS, colour="green", label=Name))+
geom_point() +
geom_text(aes(label=ifelse(PTS>24,as.character(Name),'')),hjust=0,vjust=0)
src="https://i.stack.imgur.com/qxK00.png" alt="chart with conditional
labels">
No comments:
Post a Comment