【问题标题】:How to order a plot in ggvis in R如何在 R 中的 ggvis 中订购绘图
【发布时间】:2015-02-11 02:50:46
【问题描述】:

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

我已经学会了如何制作几乎相同的情节:

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

我的问题:我怎样才能像他们在顶层情节中那样订​​购情节?看起来他们以某种方式从大到小订购了它。谢谢!

【问题讨论】:

  • 这可能不是最有效的方法,但您可以手动计算级别的顺序。在您的代码中添加 lvl &lt;- df %&gt;% group_by(y) %&gt;% summarise(total = sum(x)) %&gt;% arrange(desc(total)) %&gt;% select(y)df$y &lt;- factor(df$y, levels = lvl$y) 应该正确排序。

标签: r plot ggplot2 ggvis


【解决方案1】:
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') ))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多