How to do selective labeling with GGPLOT geom_point()

前端 未结 5 1384
执念已碎
执念已碎 2020-12-13 19:24

With this code:

library(ggplot2)
p <- ggplot(mtcars, aes(wt, mpg))
p + geom_point()
p + geom_point() + geom_text(aes(wt, mpg, label=row.names(mtcars)))
         


        
5条回答
  •  温柔的废话
    2020-12-13 19:52

    You could just get an extra variable:

    carnames <- row.names(mtcars)
    carnames[with(mtcars, !(wt > 4 | mpg > 25))] <- ""
    
    p + geom_point() + geom_text(aes(wt,mpg,label=carnames))
    

提交回复
热议问题