Search code examples
rggplot2ggvis

ggvis fill attribute is not working for certain layers or variables


Pretty straightforward:

  1. This does not work

    iris %>%  
    ggvis(x= ~Sepal.Length, y = ~Sepal.Width, fill=~Sepal.Length) %>%
    layer_bars()
    
  2. This it does

    iris %>%  
    ggvis(x= ~Sepal.Length, y = ~Sepal.Width, fill=~Sepal.Length) %>% 
    layer_points()
    

Why?

I actually managed to use the fill aesthetic with another dataset that I am not sharing, but that's just to point out that the fill should definitely work in my replicable example, right?


Solution

  • From ?layer_bars

    If grouping var is continuous, you need to manually specify grouping

    iris %>%  
      group_by(Sepal.Length) %>%
      ggvis(x= ~Sepal.Length, y = ~Sepal.Width, fill = ~Sepal.Length) %>%
      layer_bars()
    

    Which gives:

    enter image description here