如何在R中的ggvis中订购地块

时间:2015-02-11 02:50:47

标签: r plot ggplot2 ggvis

我正在尝试学习如何使用ggvis制作情节。我真的希望看起来像这样:

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') ))

我的问题:我怎样才能像在顶部情节中那样订​​购情节?看起来他们已经从某种程度上把它从大到小排序了。谢谢!

1 个答案:

答案 0 :(得分: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') ))