So I'm trying to visualize the following data using ggvis
because I want to be able to look at different customers and different combination of customers. I'd like to use a line plot and then be able to select two or three and view at them same time on the plot. The issue(s) is that I can't exactly tell which ones I'm viewing. Every time I try something a little different; I run into something else. See below, data is called m3
customer score model
a 0.437 1
a 0.471 2
a 0.036 3
b 0.455 1
b 0.371 2
b 0.462 3
c 0.280 1
c 0.042 2
c 0.279 3
d 0.282 1
d 0.470 2
d 0.246 3
e 0.469 1
e 0.500 2
e 0.303 3
f 0.290 1
f 0.387 2
f 0.161 3
g 0.075 1
g 0.111 2
g 0.116 3
Attempt 1: With this, I can see the lines, but I get a weird warning if I select two customers and I can't really tell which lines belong to who. Also it drops the second model
observation for both customers.
m3 %>% ggvis(x=~factor(model),y=~score)%>%
filter(customer == eval(input_select(choices = as.character(m3$customer),multiple=TRUE,label='Select which Domains to view'))) %>%
layer_lines()
Attempt 2: Now I can kinda see what's going on. Still not right though. The second shot is just with 'a' and 'c' selected.
m3 %>% ggvis(x=~factor(model),y=~score)%>%
filter(customer == eval(input_select(choices = as.character(m3$customer),multiple=TRUE,label='Select which Domains to view'))) %>%
layer_lines() %>% layer_text(text:= ~customer)
I still can't get it right. I've also tried using add_legend
with the layer_lines
and the stroke
argument to see if I can get the legend to show what customers have been selected by different colors and then have the legend to the side with the colors and corresponding names, but that didn't work at all. Is this too much for ggvis
? Or am I totally missing something?
Try this:
m3 %>%
#group by customer in order to separate them
group_by(customer) %>%
#the normal ggvis call
ggvis(x=~factor(model),y=~score) %>%
#filter in the same way that you did
#but add unique in order to pick one customer
#also make sure you use %in% instead of == in order to
#select multiple customers. == was the reason for the warning you received
#in your code
filter(customer %in% eval(input_select(choices = unique(as.character(m3$customer)),
multiple=TRUE,
label='Select which Domains to view'))) %>%
#add layer_paths with the stroke argument in order for
#different customers to have different colours (and a legend)
layer_paths(stroke = ~customer)
Output:
As you can see in the picture you have a legend with all the customers, and you can see the three customers selected (b, c and d) plotted. The plot will change each time according to your selections. I also use layer_paths
instead of layer_lines
as I find it works better.