How to do selective labeling with GGPLOT geom_point()

前端 未结 5 1399
执念已碎
执念已碎 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 can pass a subset argument to a layer. In your case this would require having the rownames as a column, so they are evaluated properly. You will need to explicitly load plyr to get the function . which makes the syntax easy.

    # shamelessly using @marius initial code
    library(ggplot2)
    library(plyr)
    mtcars$name <- row.names(mtcars)
    p <- ggplot(mtcars, aes(wt, mpg))
    
    p + geom_point() + geom_text(aes(wt,mpg,label=name), subset = .(wt > 4 | mpg > 25))
    

提交回复
热议问题