我正在从Excel迁移到ggvis进行数据分析。对于带有两个变量的典型分组条形图,但是我很难并排绘制 条形图而不是堆叠。
以下数据有四个步骤A,B,C,D,其中"比率为"来自两个特征的数据cc,ca。我的尝试是并排绘制cc和ca特征的比率。但是,默认情节是将两个数据叠加在一起。检查ggvis vignetts有一个选项来设置stack = FALSE。但它会与其他功能重叠。
ggvis中是否有选项可以执行" geom_bar(position =" dodge")"在ggplot?
library(ggvis)
steps <-c("A","B","C","D","A","B","C","D")
ratio <-c(1.1,1.5,1.7,1.4,1.5,1.7,1.4,1.9)
feature <-c("cc","cc","cc","cc","ca","ca","ca","ca")
shrink <- data.frame(steps,ratio,feature)
shrink %>% ggvis(x= ~steps, y= ~ratio, fill = ~feature) %>% layer_bars()
答案 0 :(得分:2)
我还没有看到一个简单的方法来做到这一点。但解决方法是明确地将x轴定义为x和fill变量的组合:
library(ggivs)
library(dplyr)
steps <-c("A","B","C","D","A","B","C","D")
ratio <-c(1.1,1.5,1.7,1.4,1.5,1.7,1.4,1.9)
feature <-c("cc","cc","cc","cc","ca","ca","ca","ca")
shrink <- data.frame(steps,ratio,feature)
shrink %>%
mutate(steps_feature = factor(paste(steps, feature))) %>%
ggvis(x= ~steps_feature, y= ~ratio, fill = ~feature) %>%
layer_bars(stack = FALSE)
并不完全令人满意 - 您希望控制条形之间的间隙,也许可以更改标签 - 但方向正确。我不喜欢这些情节,我发现它们在视觉上令人困惑,即使它们是最常见的情节之一。
我知道这不是你提出的要求,并且需要一些用户时间来适应它们,但我更喜欢这种数据的分散图:
library(tidyr)
shrink %>%
spread(feature, ratio) %>%
ggvis(x = ~ca, y = ~cc, text := ~steps) %>%
layer_text(fontSize := 35)