Mischa
Mischa

Reputation: 641

How to assign a point label conditionally in ggplot2 scatterplot in R?

When I assign the points in my ggplot scatterplot a label like this, it works perfectly.

geom_text(aes(label=df[,c("name")])))    

The problem is, however, that I aim to label only the "outstanding" dots, which happen to be those that have a mycount higher that say 500. When I try to do this using the following snippet, the correct points are selected/labelled, but with a number (most probably the row index) instead of the name.

geom_text(aes(label=ifelse(df[,c("mycount")]>500,df[,c("name")],NA)))

Why does this not work and how should I modify the line so that points with a mycount > 500 get labelled with their name?

Upvotes: 1

Views: 2376

Answers (1)

Erwan LE PENNEC
Erwan LE PENNEC

Reputation: 526

geom_text(data = filter(df, mycount>500),aes(label=name))

Should work

Upvotes: 3

Related Questions