Ggvis side by side grouped by second variable

I am migrating from Excel to ggvis to analyze data. For a typical grouped histogram with two variables, however, I have difficulty drawing the chart diagram side by side, rather than folded .

The following data has four steps A, B, C, D with the “relationship” data from the two characteristics cc, ca. My attempt is to build a ratio from cc and ca, side by side. However, the default chart with the stack combines the two data. Verify that ggvis vignetts has the ability to set stack = FALSE. But that would block another function.

Is there any way in ggvis to do things like "geom_bar (position =" dodge ")" in 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() 
+5
source share
1 answer

I do not see a simple way to do this yet. But the job 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 want to control the gaps between the bars and possibly change the labels, but in the right direction. I still don’t like these stories, I find them visually confusing, although they are one of the most common stories.
enter image description here

I know that this is not what you requested, and for some users, it takes time to get used to them, but I prefer a spread of data with such data:

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

enter image description here

+2
source

Source: https://habr.com/ru/post/1213502/


All Articles