我想要创建一个具有分类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我想要:
layer_lines和stroke = ~Specieslayer_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")发布于 2015-02-13 00:00:52
您只可以使用以下代码来完成这一任务,正如您将看到的,遗憾的是,存在一个无法解决的限制:
首先,为了使其工作,您只需要使用一个数据集,该数据集将包含所需的所有信息。然后使用以下代码:
数据的制备
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报告的原因,但几个小时前它将据报作为一个bug。
更新
经过一些研究和黑客攻击,我想出了一个解决办法如下:
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中控制条形图的宽度。
https://stackoverflow.com/questions/28485588
复制相似问题