Search code examples
rggvis

ggvis density plot + xlim + xlab


I'm trying to learn how to use ggvis. Basically, I want to reproduce this ggplot2 graph:

library(ggplot2)
m <- ggplot(mtcars, aes(x = wt))
m + geom_density(aes(fill="orange"), size=2, alpha=.9) + xlim(0,5) + theme_bw() + 
  xlab("x label") + guides(fill=FALSE)

Right now I have this:

mtcars %>% ggvis(~wt, fill := "red") %>% 
  layer_densities() %>%  
  add_axis("x", title = "Weight") %>% 
  scale_numeric("x", domain = c(0, 5), nice = FALSE)

But I don't know how to do xlim(0,5)

Thanks for the help!


Solution

  • The credits for the answer should go to hadley, thanks!

    mtcars %>% ggvis(~wt, fill := "red") %>% 
      layer_densities() %>%  
      add_axis("x", title = "Weight") %>% 
      scale_numeric("x", domain = c(0, 5), nice = FALSE, clamp = TRUE)