ggvis并排条形图按第二个变量分组 [英] ggvis side-by-side barchart grouped by second variable

查看:41
本文介绍了ggvis并排条形图按第二个变量分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Excel迁移到ggvis进行数据分析.对于具有两个变量的典型分组条形图,但是我很难绘制并排绘制条形图而不是堆叠.

I am in process of migrating from Excel to ggvis for data analysis. For a typical grouped bar chart with two variables, however I have difficulty to plot bar chart side-by-side instead of stacked.

以下数据具有四个步骤A,B,C,D,其中两个特征cc,ca具有比率"数据.我的尝试是并排绘制cc和ca特征的比率.但是,默认图将两个数据堆叠在一起.检查ggvis vignetts是否具有设置stack = FALSE的选项.但这将与其他功能重叠.

The following data has four steps A, B, C, D with "ratio" data from two features cc, ca. My attempt is to plot the ratio from cc and ca features side-by-side. However, the default plot with stack the two data together. Check the ggvis vignetts has an option to set stack =FALSE. But it would overlap the other feature.

ggvis中是否可以选择在ggplot中执行"geom_bar(position ="dodge")之类的事情?

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()

推荐答案

我还没有一种简单的方法可以做到这一点.但是一种解决方法是将x轴显式定义为x和fill变量的组合:

I don't see an easy way to do this yet. But a work around is to explicitly define your x axis as a combination of your x and fill variables:

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) 

并非完全令人满意-您需要控制条之间的间隙,并可能更改标签-但方向正确.无论如何,我都不怎么喜欢这些情节,即使它们是周围最常见的情节之一,我也会在视觉上感到困惑.

Not entirely satisfactory - you'd want to control the gaps between the bars, and maybe change the labels - but in the right direction. I don't much like these plots anyway, I find them visually confusing, even though they are one of the most common plots around.

我知道这不是您要的,并且有些用户需要一些时间来适应他们,但是我更喜欢具有此类数据的散点图:

I know it's not what you asked, and it takes some users a time to get used to them, but I much prefer a scatter plot with this sort of data:

library(tidyr)
shrink %>%
   spread(feature, ratio) %>%
   ggvis(x = ~ca, y = ~cc, text := ~steps) %>%
   layer_text(fontSize := 35)

这篇关于ggvis并排条形图按第二个变量分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆