2014-06-30 28 views
2

我在學習如何使用ggvis。基本上,我想重現此GGPLOT2圖:ggvis密度圖+ xlim + xlab

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) 

現在我有這樣的:

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

但我不知道該怎麼辦XLIM(0,5)

謝謝尋求幫助!

+4

我覺得你想在調用'scale_numeric'時使用'clamp = TRUE' – hadley

回答

4

答案的信用應該去哈德利,謝謝!

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