Elin
Elin

Reputation: 6770

Strange behavior of y axis in histograms

I want to put a set of histograms of different variables all on the same scaled y axis using ggivs. However, once my axes get meaningfully larger than the highest count for a variable they start to get very strange and even start plotting the bars in a negative direction. This is with my data http://rpubs.com/elinw/116698

Here is a reproducable example

# no values specified
iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
   add_axis("y", title = "Count", title_offset="50") 
   add_axis("x", title = "Width", title_offset="50") 

#0 to 150
 iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
   add_axis("y", title = "Count", title_offset="50",  values = seq(0,150, by = 10))  %>%
   add_axis("x", title = "Width", title_offset="50") 

  #0 to 175
 iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
   add_axis("y", title = "Count", title_offset="50",  values = seq(0,200, by = 10))  %>%
   add_axis("x", title = "Width", title_offset="50") 

 #0 to 250
 iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
   add_axis("y", title = "Count", title_offset="50",  values = seq(0,250, by = 10))  %>%
   add_axis("x", title = "Width", title_offset="50") 

#0 to 500
 iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
   add_axis("y", title = "Count", title_offset="50",  values = seq(0,500, by = 10)) 
   add_axis("x", title = "Width", title_offset="50") 

I've read the documentation but I don't see anything about this. Is there something in the properties that I can change to make this work? Or is there a known rule about this? Or is it a bug?

Upvotes: 0

Views: 82

Answers (1)

scoa
scoa

Reputation: 19857

The argument values in add_axis only sets where the ticks are on the axis, but it does not change the minimal and maximal limits of the axis (ylim/xlim). According to ggvis doc, you need to set those with argument domain in scale_numeric(). Try this:

iris %>% ggvis(~Sepal.Width) %>% layer_histograms(width = 1)  %>%
  add_axis("y", title = "Count", title_offset="50",  values = seq(0,150, by = 10))  %>%
  ## Set axis limits:
  scale_numeric("y", domain = c(0, 150), nice = FALSE) %>% 
  add_axis("x", title = "Width", title_offset="50")

You can see all the plots here: http://rpubs.com/scoa/116718

Upvotes: 2

Related Questions