Search code examples
rggvis

How to change maximum and minimum label in ggvis plot


I am trying to create a ggvis plot. I want the maximum value on the y-axis to be 220 and the minimum value to be 0, so I added values = seq(0,220,20) in add_axis(). But the problem is after I do this the the plot looks like the picture below. Does anyone know how do I fix it?

df = data.frame(date = c("12/24/2015", "12/25/2015","12/27/2015", "12/29/2015", 
                         "12/26/2015", "12/30/2015"),
                rate = runif(6,50,150),stringsAsFactors=F)

library(ggvis)
df%>%
  ggvis(~date, ~rate)%>%
  layer_lines()%>%
  layer_points()%>%
  add_axis("y", subdivide = 1, values = seq(0,220,20))

enter image description here


Solution

  • To override the default data limits you can add the appropriate scale. In this case, you will need scale_numeric and the domain argument. Once you change the data limits, you can change what the y axis looks like with add_axis as in the OP.

    df %>%
        ggvis(~date, ~rate) %>%
        layer_lines() %>%
        layer_points() %>%
        scale_numeric("y", domain = c(0, 220)) %>%
        add_axis("y", subdivide = 1)
    

    enter image description here

    The ggvis properties and scales page has some examples of this in the "Properties -> scales" section.