Ignacio
Ignacio

Reputation: 7938

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!

Upvotes: 3

Views: 1422

Answers (1)

Ignacio
Ignacio

Reputation: 7938

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)

Upvotes: 5

Related Questions