Remi.b Remi.b - 3 months ago 9
R Question

R: Handy user interface to subset data displayed on a plot

I am trying to build a simple user interface that allows the user to decide what subset of the data he/she wants to be displayed on a plot.

Here is a very silly trial of mine

require(ggplot2)

x = runif(1e5,1,10)
a = rep(0:9,each=1e4)
b = rep(1:10,1e4)
y = x^(a/b)

DATA = data.frame(a=a,b=b,x=x,y=y)

dummy=FALSE
while(TRUE)
{
response = readline("Do you want to continue? (n/y): ")
while (response!="y")
{
if (response=="n") {dummy=TRUE;break}
response = readline("Do you want to continue? (n/y): ")
}
if (dummy) break

a_sub = readline(
paste0("What subset for 'a' do you want?: (",paste(unique(DATA$a), collapse=","),") ")
)
b_sub = readline(
paste0("What subset for 'b' do you want?: (",paste(unique(DATA$b), collapse=","),") ")
)
print(ggplot(data=subset(DATA,a==a_sub & b==b_sub), aes(x=x,y=y)) + geom_point())
}


It works but isn't really convenient. There might exist much better solution though, where the user could select the desired value through mouse clicking. Can you please make an example of a nice interface that works my purpose?

Answer

You might try ggvis. Drawing from this post:

library(ggvis)
library(dplyr)
DATA %>% 
  ggvis(~x, ~y) %>%
  filter(a == eval(input_slider(0, 9))) %>%
  filter(b == eval(input_slider(1, 10))) %>%
    layer_points()

enter image description here