首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不同y轴上的layer_bars和layer_lines

不同y轴上的layer_bars和layer_lines
EN

Stack Overflow用户
提问于 2015-02-12 18:58:24
回答 1查看 1.5K关注 0票数 1

我想要创建一个具有分类x值和线和条的情节。

示例数据如下:

代码语言:javascript
复制
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轴上有刚毛、杂色和处女膜。表示sl、sw和pl的三行,即layer_linesstroke = ~Species
  2. 一个不同比例的layer_bars,右边有轴标签。

我很难把不同的功能组合在一起。以下是我失败的尝试:

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2015-02-13 00:00:52

您只可以使用以下代码来完成这一任务,正如您将看到的,遗憾的是,存在一个无法解决的限制:

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

数据的制备

代码语言:javascript
复制
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:

代码语言:javascript
复制
dataWide2 <- cbind(dataWide, longPw[2:3]) 

溶液

代码语言:javascript
复制
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报告的原因,但几个小时前它将据报作为一个bug。

更新

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

代码语言:javascript
复制
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并删除一些您不想要的滴答,但这是它所能得到的最好结果。希望能帮上忙!

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28485588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档