【发布时间】:2015-02-12 18:58:24
【问题描述】:
我想创建一个带有分类 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
我想拥有:
- x 轴上的 setosa、versicolor 和 virginica。三行
表示 sl、sw 和 pl,即
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")
【问题讨论】:
-
只是好奇,希望你在 ggvis 中找到一个好的解决方案。你需要在ggvis中这样做吗?即这会在某个时候是交互式的吗?否则就用ggplot2
-
@miles2know 我认为 ggplot2 不支持多轴?另外,我最终确实想过滤因素
-
我在 ggplot2 上查看了它。我猜你的权利。它不受支持,从我和许多其他人的角度来看,这不是可视化数据的好方法。除此之外……您是否考虑过其他方式来可视化您的工作?你想表达什么?