2015-02-11 48 views
1

我在試圖學習如何使用ggvis來製作圖。我真的想上,看起來像這樣:如何在ggvis中訂購一張圖R

Plot

我已經學會了如何做一個幾乎相同的情節:

library(ggvis) 
y <- c(
"a", "b", "c", "d", "e", "f", "g", "h", 
"a", "b", "c", "d", "e", "f", "g", "h") 

x <- c(28, 25, 38, 19, 13, 30, 60, 18, 11, 10, 17, 13, 9, 25, 56, 17) 
Status <- c(rep(c('Group 1'),8), rep(c('Group 2'),8)) 

df <- data.frame(y,x,Status) 

df %>% ggvis(x= ~x, y= ~y, fill= ~Status) %>% layer_points() %>% 
    add_axis('x', properties= axis_props(grid = list(stroke = 'blank'))) %>% 
    add_axis('y', properties= axis_props(grid = list(stroke = 'blank'))) 

我的問題:如何訂購劇情像他們這樣做在頂部情節?看起來他們已經以某種方式從大到小排序。謝謝!

+3

這可能不是最有效的方法,但您可以手動計算關卡的順序。在您的代碼中添加'lvl <- df %>%group_by(y)%>%summarize(total = sum(x))%>%arrange(desc(total))%>%select(y)'和'df $ y < - factor (df $ y,levels = lvl $ y)'應該正確地排列東西。 – jraab 2015-02-11 02:59:37

回答

2
tbl_df(df) %>% 
    mutate(y=as.character(y), x=as.numeric(x)) %>% 
    arrange(desc(x)) %>% 
ggvis(x= ~x, y= ~y, fill= ~Status) %>% layer_points() %>% 
    add_axis('x', properties= axis_props(grid = list(stroke = 'blank'))) %>% 
    add_axis('y', properties= axis_props(grid = list(stroke = 'blank')))