Henk Straten
Henk Straten

Reputation: 1447

Only display label per category

I have the following dataset:

year <- as.factor(c(1999,2000,2001))
era <- c(0.4,0.6,0.7)
player_id <- as.factor(c(2,2,2))
df <- data.frame(year, era, player_id)

Using this data I created the following graph:

ggplot(data = df, aes(x = year, y=era, colour = player_id))+ 
 geom_line() +
 geom_text(aes(label = player_id), hjust=0.7)

Thing is however that I do now get a label at every datapoint. I only want to have a label at the end of each datapoint.

Any thoughts on what I should change to I get only one label?

Upvotes: 0

Views: 180

Answers (2)

Santosh M.
Santosh M.

Reputation: 2454

If I understand correctly, you want label at end of data point. You could do this using directlabels library, as below:

library(ggplot2)
library(directlabels)
ggplot(data = df, aes(x = year, y=era, group = player_id, colour = player_id))+ 
geom_line() +
scale_colour_discrete(guide = 'none') +
scale_x_discrete(expand=c(0, 1)) +
geom_dl(aes(label = player_id), method = list(dl.combine("last.points"), cex = 0.8))

Output:

enter image description here

Upvotes: 2

George Sotiropoulos
George Sotiropoulos

Reputation: 2133

If I am understanding correctly what you want, then you can replace the geom_text(...) with geom_point()

enter image description here

Upvotes: 0

Related Questions