Is position_fill equivalent to ggvis?

Trying to replicate ggplot function position="fill"

in ggvis. I use this handy feature all the time in the results view. Reproducible example successfully executed in ggplot2 + ggvis code. Can this be done with a function scale_numeric

?

library(ggplot2)
p <- ggplot(mtcars, aes(x=factor(cyl), fill=factor(vs)))
p+geom_bar()
p+geom_bar(position="fill")

      

library(ggvis)
q <- mtcars %>%
  ggvis(~factor(cyl), fill = ~factor(vs))%>%
  layer_bars()

# Something like this?
q %>% scale_numeric("y", domain = c(0,1))

      

+3


source to share


1 answer


I think for this kind of thing with ggvis, you need to do heavy data drag and drop before sending it to ggvis. ggplot2 geom_bar conveniently does a lot of calculations (counting things, weighing them, etc.) for you, which you need to do explicitly yourself in ggvis. So try something like below (maybe more elegant ways):

mtcars %>%
   mutate(cyl=factor(cyl), vs=as.factor(vs)) %>%
   group_by(cyl, vs) %>%
   summarise(count=length(mpg)) %>%
   group_by(cyl) %>%
   mutate(proportion = count / sum(count)) %>%
   ggvis(x= ~cyl, y = ~proportion, fill = ~vs) %>%
   layer_bars()

      



enter image description here

0


source







All Articles