-->

ggvis上不同的y轴和layer_bars layer_lines(ggvis layer_bar

2019-10-21 20:33发布

我想创建一个分类的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. setosa,云芝和锦葵在x轴上。 三行表示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")

Answer 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 bug报告的原因,但据报道在几个小时前,是一个错误。

UPDATE

有点研发的,有点黑客攻击后,我想出了一个解决方法是如下:

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和删除一些你不想要的蜱但这是好,因为它可以得到的。 希望能帮助到你!

PS可以有效控制横杠的宽度width的说法layer_bars



文章来源: ggvis layer_bars and layer_lines on different y-axis
标签: r ggvis