【问题标题】:Align multiple graphs using ggvis (layer_points over layer_boxplots)使用 ggvis 对齐多个图形(layer_points over layer_boxplots)
【发布时间】:2015-11-10 13:46:40
【问题描述】:

我正在尝试在箱线图上叠加点。点和箱线图都来自同一个数据源:db_gems_spend。唯一的区别是它们的过滤方式(箱线图的日期范围和点的一天)。最终目标是为图表添加交互性,以便我能够选择一个日期并通过查看该点在特定箱形图上的位置来立即查看该日期与其他日期的比较。

问题是这些点当前没有与箱线图对齐。

你可以在这里看到它:

这是代码:

db_gems_spend %>%
  filter(dayofweek == "Fri") %>% # add interactivity (automate dayofweek selection)
  filter(date >= "2015-08-01") %>% # add interactivity
  ggvis(~action_type, ~count) %>%
  layer_boxplots() %>%
  add_axis("x", title = "action_type", title_offset = 50, 
           properties = axis_props(labels = list(angle = 20, align = "left", fontSize = 10))) %>%
  add_axis("y", title = "count", title_offset = 60) %>%
  add_data(db_gems_spend) %>%
  filter(date == "2015-11-04") %>% # add interactivity
  layer_points(x = ~action_type, y = ~count, fill :=  "red")

如何让这些点对齐?

【问题讨论】:

  • 您能否添加一些示例数据以使该问题可重现?请参阅 here 了解如何创建最小的可重现示例。
  • 你在 ggvis github 仓库上看到过this issue 吗?解决方法可能正是您所需要的。

标签: r ggplot2 visualization ggvis


【解决方案1】:
db_gems_spend %>%
  ggvis(~action_type, ~(count/total_spend)) %>%
  layer_boxplots() %>%
  add_data(db_gems_spend) %>%
  layer_points(x = ~action_type, y = ~count, fill := "red", 
    prop("x", ~action_type, scale = "xcenter"))

感谢 aosmith,github 上的解决方案正是我想要的。事实证明,如果值是分类的而不是数字的,ggvis 会将 layer_points 对齐到 layer_boxplots 的左侧,除非您从上面指定最后一行代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多