sjmgarnier / viridis

Colorblind-Friendly Color Maps for R
http://sjmgarnier.github.io/viridis
Other
289 stars 38 forks source link

scale_fill_viridis() does not fill ggplot2 bar geoms colors #45

Closed jrosen48 closed 7 years ago

jrosen48 commented 7 years ago

Thanks for a great package!

I am trying to fill bars using geom_col() (which is equivalent to geom_bar(stat = "identity") in a ggplot2 bar plot with colors using scale_fill_viridis(discrete = T) :

library(tidyverse)
library(viridis)

mtcars %>% 
    select(cyl, mpg) %>% 
    group_by(cyl) %>% 
    summarise(mpg_mean = mean(mpg)) %>% 
    ggplot(aes(x = cyl, y = mpg_mean)) +
    geom_col() +
    scale_fill_viridis(discrete = T)

mpg

This does not seem to color the bars. Is there a way to use scale_fill_viridis() to do this?

sjmgarnier commented 7 years ago

@jrosen48 Looks like you didn't specify an aesthetic for "fill". This works for me:

mtcars %>% 
  select(cyl, mpg) %>% 
  group_by(cyl) %>% 
  summarise(mpg_mean = mean(mpg)) %>% 
  ggplot(aes(x = cyl, y = mpg_mean, fill = factor(cyl))) +
  geom_col() +
  scale_fill_viridis(discrete = T)
jrosen48 commented 7 years ago

That did the trick. Oops! Thanks