简体   繁体   English

ggvis不同y轴上的layer_bars和layer_lines

[英]ggvis layer_bars and layer_lines on different y-axis

I would like to create a plot with categorical x values and lines and bars. 我想创建一个带有分类x值以及线条和条形的图。

The example data is as follows: 示例数据如下:

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

I want to have: 我希望有:

  1. setosa, versicolor, and virginica on the x-axis. 在x轴上显示setosa,杂色和维吉尼亚。 three lines representing sl, sw, and pl, ie layer_lines with stroke = ~Species 三行代表sl,sw和pl,即具有stroke = ~Species layer_lines
  2. a layer_bars on a different scale, with axis label on the right. 不同比例的layer_bars ,右侧带有轴标签。

I'm having trouble putting together the different functions together. 我在将不同功能组合在一起时遇到麻烦。 Here's my failed attempt: 这是我失败的尝试:

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

You can do it with the following code only, and as you will see there is a limitation which there is no solution for unfortunately: 您只能使用以下代码来完成此操作,并且您将看到一个局限性,不幸的是没有解决方案:

First of all in order for this to work you need to use ONLY one dataset which will have all of the information you need. 首先,为了使它起作用,您只需要使用一个数据集即可,它具有您需要的所有信息。 Then you use the following code: 然后,使用以下代码:

Preparation of Data 数据准备

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)

Have all the information you need for the plot in only one data set ie dataWide2: 仅在一个数据集中(即dataWide2)拥有绘图所需的所有信息:

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

Solution

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

Few words for the above code: 上面的代码几句话:

  • First of all you NEED to start with the barchart. 首先,您需要从条形图开始。 I don't know why this happens but the opposite will produce an error. 我不知道为什么会这样,但是相反的情况会产生错误。
  • Secondly you need to create the lines separately since you are using one data set and set the colours individually. 其次,由于要使用一个数据集并分别设置颜色,因此需要分别创建线条。

Limitation 局限性

As you can see in the graph below the bar chart and the lines are not aligned which cannot be fixed so far unfortunately (at least to my current knowledge - check the Edit below). 正如您在条形图下方的图形中所看到的那样,不幸的是,这些线尚未对齐(目前为止还无法固定)(至少就我目前所知-请检查下面的“编辑”)。 This limitation is actually the reason I made an account on stack overflow in the first place. 这个限制实际上是我首先在堆栈溢出上进行帐户注册的原因。 If you check my profile this is my one and only question. 如果您查看我的个人资料,这是我唯一的问题。

Hope it helps :) 希望能帮助到你 :)

在此处输入图片说明 EDIT 编辑

I don't know if this post is the reason for the bug report on ggvis on the github page but it was reported as a bug a few hours ago. 我不知道这篇文章是否是github页面上ggvis上的错误报告的原因,但几小时前它被报告为错误。

UPDATE UPDATE

After a bit of researching and a bit of hacking I figured out a workaround which is as follows: 经过一些研究和一些黑客攻击,我找到了一种解决方法,如下所示:

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

The data set is exactly the same and essentially in order to have aligned bars and lines the bars need to have a numeric x axis. 数据集完全相同,并且实质上是为了具有对齐的条和线,条需要具有数字x轴。 I therefore, created a second overlapping x axis which hosts the categorical data. 因此,我创建了第二个重叠的x轴,用于存放分类数据。

The output: 输出:

在此处输入图片说明

I suppose you could set the grid=F and remove some of the ticks you don't want but this is as good as it can get. 我想您可以设置grid=F并删除一些不需要的刻度,但这是可以做到的。 Hope it helps! 希望能帮助到你!

PS you can control the width of the bars with the width argument in layer_bars . PS,您可以使用layer_barswidth参数控制条的width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM