Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggvis line chart with interactive x axis range

I'd like to be able to

  1. Plot a line chart in ggvis
  2. Add two interactive controls, which allow me to set the min and max x values on the chart

This sounds pretty straightforward - my code is:

minx = minx = input_numeric(1, 'Min x-val')
maxx = input_numeric(1, 'Max x-val')

data.frame(train.dt) %>% 
ggvis(x = ~plot_idx, y = ~val) %>%
layer_lines() %>% add_axis('x') %>%
scale_numeric('x', domain = c(minx, maxx), clamp = T)

However, this doesn't work. I get this error message:

"Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : non-numeric argument to binary operator".

If I replace minx and maxx in the domain argument with e.g. 1 and 10, my graph plots just fine (but is static). Any ideas?

Thanks!

like image 879
user3279453 Avatar asked Dec 27 '14 23:12

user3279453


1 Answers

This may be helpful. Since there is no reproducible data, I decided to use an example from the CRAN manual. What you would have to do is to use input_slider and choose min and max for the x-axis. In this example, you have a double-ended slider. That means you can choose/change min and max both. The input_slider goes into domain in scale_numeric.

# Set up input_slider
foo <- input_slider(1, 10, c(1, 6))

mtcars %>%
ggvis(x = ~wt, y = ~mpg, stroke = ~factor(cyl)) %>%
layer_lines() %>%
scale_numeric("x", domain = foo, clamp = TRUE)
like image 177
jazzurro Avatar answered Oct 27 '22 10:10

jazzurro