【问题标题】:ggvis layer_bars and layer_lines on different y-axisggvis layer_bars 和 layer_lines 在不同的 y 轴上
【发布时间】:2015-02-12 18:58:24
【问题描述】:

我想创建一个带有分类 x 值以及线条和条形的图。

示例数据如下:

dataWide <- iris %>%
  group_by(Species) %>%
  summarize(sl = mean(Sepal.Length),
            sw = mean(Sepal.Width),
            pl = mean(Petal.Length),
            pw = mean(Petal.Width))

# Source: local data frame [3 x 5]
# 
# Species    sl    sw    pl    pw
# 1     setosa 5.006 3.428 1.462 0.246
# 2 versicolor 5.936 2.770 4.260 1.326
# 3  virginica 6.588 2.974 5.552 2.026

我想拥有:

  1. x 轴上的 setosa、versicolor 和 virginica。三行 表示 sl、sw 和 pl,即 layer_linesstroke = ~Species
  2. 一个不同比例的layer_bars,右侧带有轴标签。

我无法将不同的功能组合在一起。这是我失败的尝试:

library(ggvis)
library(tidyr)
library(dplyr)

long <- dataWide %>%
  select(Species, sl, sw, pl) %>%
  gather(variable, value, sl : pl)

longPw <- dataWide %>% select(Species, pw) %>%
  gather(variable, value, pw)

long %>%
  ggvis(x = ~Species, y = ~value, stroke = ~variable) %>%
  layer_lines() %>%
  add_axis("y", "ypw", orient = "right", title = "PW", grid = F) %>%
  layer_bars(x = ~Species, y = ~value, data = longPw, scale = "ypw")

【问题讨论】:

  • 只是好奇,希望你在 ggvis 中找到一个好的解决方案。你需要在ggvis中这样做吗?即这会在某个时候是交互式的吗?否则就用ggplot2
  • @miles2know 我认为 ggplot2 不支持多轴?另外,我最终确实想过滤因素
  • 我在 ggplot2 上查看了它。我猜你的权利。它不受支持,从我和许多其他人的角度来看,这不是可视化数据的好方法。除此之外……您是否考虑过其他方式来可视化您的工作?你想表达什么?

标签: r ggvis


【解决方案1】:

您只能使用以下代码来完成此操作,并且您将看到有一个限制,不幸的是没有解决方案:

首先,为了使其工作,您只需要使用一个数据集,该数据集将包含您需要的所有信息。然后你使用下面的代码:

数据准备

dataWide <- iris %>%
  group_by(Species) %>%
  summarize(sl = mean(Sepal.Length),
            sw = mean(Sepal.Width),
            pl = mean(Petal.Length),
            pw = mean(Petal.Width))

library(ggvis)
library(tidyr)
library(dplyr)
long <- dataWide %>%
  select(Species, sl, sw, pl) %>%
  gather(variable, value, sl : pl)

longPw <- dataWide %>% select(Species, pw) %>%
  gather(variable, value, pw)

将绘图所需的所有信息仅包含在一个数据集中,即dataWide2:

dataWide2 <- cbind(dataWide, longPw[2:3]) 

解决方案

dataWide2 %>%
  #start with barchart
  ggvis(x = ~Species, y = ~value) %>%
  layer_bars(opacity := 0.4) %>%

  #details for right axis i.e. the bars
  add_axis("y", orient = "right", title = "My bars" ,title_offset = 50) %>% 

  #details for left axis i.e. the lines + plotting of lines 
  add_axis("y", 'ylines' , orient = "left", title= "My lines" , grid=F ) %>%
  layer_lines(stroke := 'red',   prop('y', ~sl, scale='ylines')) %>%
  layer_lines(stroke := 'blue',  prop('y', ~sw, scale='ylines')) %>%
  layer_lines(stroke := 'green', prop('y', ~pl, scale='ylines')) 

上面代码的几句话:

  • 首先您需要从条形图开始。我不知道为什么会发生这种情况,但相反会产生错误。
  • 其次,您需要单独创建线条,因为您使用的是一个数据集并单独设置颜色。

限制

正如您在条形图下方的图表中看到的那样,不幸的是,到目前为止无法修复的线条未对齐(至少据我目前的知识 - 检查下面的编辑)。这个限制实际上是我首先考虑堆栈溢出的原因。如果您查看我的个人资料,这是我唯一的问题。

希望对你有帮助:)

编辑

我不知道这篇文章是否是 github 页面上关于 ggvis 的错误报告的原因,但它在几个小时前 was reported 是一个错误。

更新

经过一番研究和一些黑客攻击,我想出了一个解决方法,如下所示:

dataWide2 %>%
  #start with barchart
  ggvis(x = ~as.numeric(Species), y = ~value) %>%
  layer_bars(opacity := 0.4) %>%

  #add the initial x axis in order to set x labes to blank
  add_axis('x', title='Species', properties = axis_props(labels=list(fill='blank'))) %>%


  #details for right axis i.e. the bars
  add_axis("y", orient = "right", title = "My bars" ,title_offset = 50) %>% 

  #details for left axis i.e. the lines + plotting of lines 
  add_axis("y", 'ylines' , orient = "left", title= "My lines" , grid=F ) %>%
  layer_lines(stroke := 'red',   prop('y', ~sl, scale='ylines')) %>%
  layer_lines(stroke := 'blue',  prop('y', ~sw, scale='ylines')) %>%
  layer_lines(stroke := 'green', prop('y', ~pl, scale='ylines')) %>%

  #add new axis which will be for our categorical x axis
  add_axis('x', 'myx2', orient='bottom', title='') %>%

  #add categorical data and make lines invisible (we only need the categories anyway)
  layer_lines(prop("x", ~ Species, scale = "myx2"), stroke := 'blank') 

数据集完全相同,基本上为了对齐条形和线,条形需要具有数字 x 轴。因此,我创建了第二个重叠的 x 轴来承载分类数据。

输出:

我想你可以设置grid=F 并删除一些你不想要的刻度,但这已经是最好的了。希望对您有所帮助!

附:您可以使用layer_bars 中的width 参数控制条形的宽度。

【讨论】:

  • 这太棒了,但是随着级别数的增加,标签似乎会错位......我想知道我们是否可以使用数字轴但将标签设置为levels(Species)
  • 感谢您的评论@organicagave。我尝试了很多东西,包括将levels(Species) 设置为ordinal_scale 并尝试layer_text,但不幸的是似乎没有任何效果......而且你是绝对正确的。如果你有很多标签,那么它们最终会错位。它只(有点)适用于像这里这样的几个标签。我会密切关注未来的发展,可能会玩更多的游戏。如果我找到任何东西,我会在此处包括在内。我认为现在这与ggvis 一样好。
  • 我还发布了关于我唯一的问题的答案(看起来像这个),但条形宽度不同,并设置了第二个 x 轴网格,看起来更好一些,但绝对不是最佳的。我会密切关注进一步的发展,如果我发现新的东西会发布。谢谢!
猜你喜欢
  • 2019-12-25
  • 1970-01-01
  • 2022-01-16
  • 2014-09-10
  • 2015-04-04
  • 2015-11-26
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
相关资源
最近更新 更多