I want to label only custom selected dots, "which is randomly taken without any specific criteria", in scatter plot. I tried:
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_text(label=rownames(mtcars))
But I want to label only following names:
Merc 450SE
Cadillac Fleetwood
Lincoln Continental
Chrysler Imperial
Fiat 128
Honda Civic
Toyota Corolla
Fiat X1-9
Porsche 914-2
Lotus Europa
The expected output will be:
This question is not a duplicate of the above mentioned question. In the given link they discuss about labelling using conditions. But here I am asking about labelling them about any randomly selected names.
You can subset the data when using geom_text
. And to circumvent the overlap issue, you can use geom_text_repel
from the ggrepel package:
library(ggrepel)
show.names <- c('Merc 450SE',
'Cadillac Fleetwood',
'Lincoln Continental',
'Chrysler Imperial',
'Fiat 128',
'Honda Civic',
'Toyota Corolla',
'Fiat X1-9',
'Porsche 914-2',
'Lotus Europa')
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text_repel(data=mtcars[rownames(mtcars) %in% show.names,],
label=show.names)