I'm trying to use a slider to control year in a longitudinal spatial data set, essentially a set of scatter plots. I can't figure out how to assign the slider to this variable - can you do this in ggvis?
A simplified data set:
data <- data.frame(year=rep(2000:2002, each=23),
x=rnorm(23*3,10), y=rnorm(23*3,10),
count=c(rnorm(23,2), rnorm(23,4), rnorm(23,6)))
What I've tried:
### This is what is looks like in ggplot2, I'm aiming to be able to toggle
### between these panels
ggplot(data, aes(x, y, size=count)) + geom_point() + facet_grid(~year)
### Here is where I'm at with ggvis
data %>%
ggvis(~x, ~y, size=~count) %>%
layer_points()
# I'm not sure how to assign a variable (year) to a slider, I've been trying
# within the layer_points() function
### I also tried using the props() function, but I don't fully understand
### how to use it.
data %>%
ggvis(~x, ~y, size=~count) %>%
layer_points() %>%
props(prop("fill", input_slider(min(data$year), max(data$year)))) #error message
Any help is appreciated!
I'm not sure if you want to use the slider to
filter
the data points (i.e. only show those points from the year selected on the slider), or to show the years in different colors according to the slider's value.Case 1 (only display the points from a specific year)
Case 2 (highlight the selected years)
EDIT2: How to simply wrap a
left_right()
function.In the first edit I presented a solution that is not properly considered as wrapping. I was interested in creating a wrapper of the reactive object returned by
left_right()
, avoiding modifyingcreate_keyboard_event
all together.After reading the source code of
ggvis
more thoroughly and more on S4 objects in R, I realized that yes, you can simply wrap a reactive object, as long as you preserve thebroker
class and itsbroker
attribute appropriately.This allows us to write more elegant code, like:
EDIT: How to create your own (modified)
left_right()
functionuser3389288 asked me a good question, that since you don't have a
map
argument forleft_right()
function, how can you actually bind keyboard event to generate custom parameters. For example, in the context of this question, how can we tailorleft_right()
as a year filter?If you dig into the source code of
ggvis
, you can see thatleft_right()
is simply a thin wrapper function callingcreate_keyboard_event
.Hence we can create our own version of
left_right()
, or evenh_j_k_l()
say if you are fanatic about Vi. But, here is a big but, if you dig one layer further to look at the implementation ofcreate_keyboard_event
, you will find that it is not quite suitable for our task.This is because in order to show some of the dots, while hide others, we have to let
left_right
return avector
(that equals to the number of rows indata
). However, bothleft_right
andcreate_keyboard_event
are created with the assumption that the returned value (which is also the current state of thevalue
modified by Left/Right key presses) is a scalar.In order to separate the return value (vector) from the cached current state (scalar, i.e. the year), we have to create a slightly modified version of
left_right()
andcreate_keyboard_event
.Below is the source code that would work.
You can compare
left_right_year
andcreate_keyboard_event2
with their vanilla version counterparts.For example, the original
create_keyboard_event
is:You can see that our modified version will not only cache the current state
vals$x
, but also the return vectorvals$res
.The variable
vals
is a reactive value. The concept is borrowed from Shiny. You can check out this document about a high-level overview of reactive values and reactivity in general.A question yet to be answeredSince
vals$x
is itself a reactive value. Intuitively, ifthen
should allow us to implement a quick
map
function.However it doesn't work as expected. I am yet to figure out why exactly. If you know the answer, please kindly let me know!
UPDATED: c.f. EDIT2
The answers above are great. Definitively worth study. This is what I came up with for the original question for a quick fix.
Global.R:
ui.R:
Server.R: