ggplot: plotting layers only if certain criteria are met

前端 未结 1 1940
闹比i
闹比i 2020-12-30 15:46

Is there a method of filtering within ggplot itself? That is, say I want to do this

p <- ggplot(iris, aes(x = Sepal.Width, y = Sepal         


        
相关标签:
1条回答
  • 2020-12-30 16:07

    apparently layers now accept a function as data argument, so you could use that

    pick <- function(condition){
      function(d) d %>% filter_(condition)
    }
    
    ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, species)) +
      geom_point(size = 4, shape = 4) +
      geom_point(data = pick(~Species == "setosa"), colour = "red") +
      geom_point(data = pick(~Species == "versicolor"), shape = 5)
    
    0 讨论(0)
提交回复
热议问题