nik
nik

Reputation: 2584

labeling each data point

I have a data like this

df<-structure(list(X = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 10L, 
9L, 11L, 12L, 8L), .Label = c("A", "B", "C", "D", "E", "F", "GG", 
"IR", "MM", "TT", "TTA", "UD"), class = "factor"), X_t = c(3.7066, 
3.6373, 3.2693, 2.5626, 2.4144, 2.2868, 2.1238, 1.8671, 1.7627, 
1.4636, 1.4195, 1.0159), NEW = structure(c(8L, 7L, 9L, 1L, 2L, 
3L, 4L, 5L, 6L, 10L, 11L, 12L), .Label = c("12-Jan", "14-Jan", 
"16-Jan", "19-Jan", "25-Jan", "28-Jan", "4-Jan", "Feb-38", "Feb-48", 
"Jan-39", "Jan-41", "Jan-66"), class = "factor")), class = "data.frame", row.names = c(NA, 
-12L))

I am trying to put the label for each dot but I get a warning

here is how I plot it

ggplot(data=df)+
  geom_point(aes(X_t, X,size =X_t,colour =X_t,label = NEW))

also I want to merge the two legend into one because it is redundant, if you have any tips let me know please

Upvotes: 0

Views: 46

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 146249

Use geom_text for text (e.g., labels):

ggplot(data=df, aes(X_t, X)) +
  geom_point(aes(size = X_t, colour = X_t)) +
  geom_text(aes(label = NEW), nudge_y = 0.5) +
  guides(color = guide_legend(), size = guide_legend())

enter image description here

Aesthetics you specify in the ggplot() call will be inherited by subsequent layeres (geoms). So by putting the x and y aesthetics in ggplot(), we don't have to re-specify them again.

As for the legend question, see this answer for details. To combine color and size legends we use guide_legend.

Upvotes: 1

Related Questions