0
votes

I am trying to make a bar plot displaying the male/female ratio of respondents of my survey. Assuming that qual$Q1.4 is a column of the dataframe containing factors (either male of female), I have the following code:

qual %>% 
  compute_tabulate(~Q1.4) %>% 
  ggvis(x = ~x_, y = ~count_/sum(count_), fill = ~x_) %>% 
  layer_bars() %>%
  add_axis("x", title = "Gender") %>%
  add_axis("y", title = "Percentage", format = "%")

This works well but both bars have 100%. If I remove the fill attribute, then my bars have the correct height but are not colored.

Any idea why the y axis stops working correctly when I put a variable fill property?

The data looks like that: data sample

The output of the code above looks like that: output

1
first of all, you are plotting a barplot not a histogram, secondly it's hard to help when you're not sharing an example of your data. - mtoto
Sorry, my bad. I have corrected the title and added a picture of how the data looks like and of the output to clarify. - Jonathan Zimmermann
please dput() your data instead of posting pictures, and preferably more than one column since compute_tabulate only works with data.frame's - mtoto
I really don't think it is necessary for this specific problem. I am only using this column of the dataframe and you can assume that all the other rows take any form with just those two values. Adding more data would just make the question more difficult to understand. - Jonathan Zimmermann

1 Answers

0
votes

The problem actually turned out to be the sum() function inside ggvis(). I added a mutate() step (using dplyr) and the problem was solved:

qual %>% 
  compute_tabulate(~Q1.4) %>% 
  mutate(percent = count_/sum(count_)) %>%
  ggvis(x = ~x_, y = ~percent,fill=~x_) %>% 
  layer_bars() %>%
  add_axis("x", title = "Gender") %>%
  add_axis("y", title = "Percentage", format = "%")

Output: graph

Still no idea why it happened in the first place though. Bug of ggvis?