controlling color of factor group in ggvis - r

后端 未结 1 985
遇见更好的自我
遇见更好的自我 2020-12-20 18:40

I have a question about controlling the color of datapoints in ggvis.

I have a dataframe that I am filtering in multiple ways (within a shiny app in case it matters).

相关标签:
1条回答
  • 2020-12-20 19:29

    Solution 1

    It seems as if I overlooked a very easy solution:

    Just re-define the levels of the factor, and drop the factor from the fill=

    I'll leave this up as it might help someone else.

    dfvis$mygroup<-factor(dfvis$mygroup, levels=c("A", "B", "C", "D", "E"))
    
    dfvis %>% 
      ggvis(x= ~x, y= ~y)  %>% 
      layer_points(fill = ~mygroup)
    

    Solution 2

    This may actually have more generalizability for ggvis users. We can take advantage of : versus :=. Make a new variable of colors for each group

    dfvis$color <- c("blue","orange","green","red","purple")
    

    Then we can use the unscaled raw value of colors with fill:= inside the ggvis function...

    #:= denotes unscaled raw value
    dfvis %>% 
      ggvis(x= ~x, y= ~y, fill:= ~color)  %>% 
      layer_points()
    

    This will ensure color consistency even after filtering out other groups.

    0 讨论(0)
提交回复
热议问题