2014-12-20 48 views
13

這是我迄今爲止與ggvis包在R.創建輔助y軸爲線圖與ggvisř

mtcars %>% ggvis(x = ~disp) %>% 
    layer_lines(y = ~wt, stroke := "red") %>% 
    layer_lines(y = ~mpg) %>% 
    add_axis("y", orient = "left", title = "Weight (lb/1000)") %>% 
    add_axis("y", orient = "right", title= "Miles/(US) gallon") %>% 
    add_axis("x", title = "Displacement (cu.in.)") 

我不能得到的左Y軸以表示重量比例的數據。

此輸出:

enter image description here

回答

12

我假設你要除以1000左側的Y軸(即重量):

library(dplyr) #you need this library 
mtcars %>% mutate(wt_scaled=wt/1000) %>% ggvis(x = ~disp) %>% #use mutate from dplyr to add scaled wt 
    layer_lines(y = ~wt_scaled, stroke := "red") %>% #use new column 
    add_axis("y", orient = "left", title = "Weight (lb/1000)" ,title_offset = 50) %>% #fix left axis label 
    scale_numeric("y", domain = c(0, 0.006), nice = FALSE) %>% #align the ticks as good as possible 
    add_axis("y", 'ympg' , orient = "right", title= "Miles/(US) gallon" , grid=F) %>% #remove right y axis grid and name axis 
    layer_lines(prop('y' , ~mpg, scale='ympg')) %>% #use scale to show layer_lines which axis it should use 
    add_axis("x", title = "Displacement (cu.in.)" ) 

,我認爲這是你想要什麼:

enter image description here

編輯

如果你只是想繪製重量左側Y軸(這是不是很清楚),然後執行mutate(wt_scaled=wt/1)(或刪除發生變異)和更改域domain = c(1.5, 5.5)