我想创建一个带有分类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 = ~Species
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")
答案 0 :(得分: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
并删除一些你不想要的滴答声,但这是最好的。希望它有所帮助!
P.S。您可以使用width
中的layer_bars
参数控制条形的宽度。