Find N unique values inside ggplot

2019-09-19 04:31发布

I have an example dataframe

df <- data.frame(a = c(sample(LETTERS[1:5], 10, TRUE), "Z", "Z"), 
                 b = c(rnorm(10), NA, NA))

and I'm trying to do some simple plotting and add some color. I can do this easily knowing how many a values there will be before I even plot:

library(randomcoloR)

df %>%
    filter(!is.na(b)) %>%
    ggplot() +
    geom_bar(aes(x = a, y = b),
             fill = randomColor(5),
             stat = "summary", 
             fun.y = "mean")

But what if I don't know how many a values there will be? How can get the number of unique a inline inside the geom_xxx statement?

Using n_distinct(a) does not work and provides the error

df %>%
    filter(!is.na(b)) %>%
    ggplot() +
    geom_bar(aes(x = a, y = b),
             fill = randomColor(n_distinct(a)),
             stat = "summary", 
             fun.y = "mean")

Error in n_distinct_multi(list(...), na.rm) : object 'a' not found

Using uniqueN() from data.table does not work and provides the error:

library(data.table)

df %>%
    filter(!is.na(b)) %>%
    ggplot() +
    geom_bar(aes(x = a, y = b),
             fill = randomColor(uniqueN(a)),
             stat = "summary", 
             fun.y = "mean")

Error in uniqueN(a) : object 'a' not found

Using unique(a) %>% length() does not work, either, and provides the same error immediately above.

Can I find inline the number of unique a values inside a geom_xxx statement? I feel like I'm missing something obvious, here.

标签: r ggplot2 dplyr
1条回答
Explosion°爆炸
2楼-- · 2019-09-19 04:57

You could pipe the data frame into an anonymous function:

df %>%
  filter(!is.na(b)) %>%
  (function(df) ({
    ggplot(df) +
      geom_bar(aes(x = a, y = b),
               fill = randomColor(length(unique(df$a))),
               stat = "summary",
               fun.y = "mean")
    }))
查看更多
登录 后发表回答