Regression lines vanishing if toggled by shiny checkboxInput

77 views
Skip to first unread message

Jemus42

unread,
Jul 15, 2014, 11:03:48 AM7/15/14
to gg...@googlegroups.com
Live shiny app here: https://jemus42.shinyapps.io/tRakt/

The plotting code has grown fairly chaotic, but it looks like this: https://github.com/jemus42/tRakt-shiny/blob/master/server.R#L69-L106

Everything worked fine for the ggvis plot until I tried to add the regression lines, and trying the regression lines outside of shiny worked, too:

plot <- epdata %>% ggvis(x = ~epnum, y = ~rating)
plot <- plot %>% layer_points(fill = ~season)
plot <- plot %>% layer_model_predictions(formula = rating ~ epnum, model = "lm", se = F)
plot <- plot %>% group_by(season) %>% layer_model_predictions(model = "lm", se = F, stroke = ~season)
plot <- plot %>% add_axis("x", title = "Episode Number")
plot <- plot %>% add_axis("y", title = "Rating")
plot <- plot %>% add_legend("fill", title = "Season", orient = "left")

But in the app, the lines appear shortly and vanish, except for the rightmost season trendline, which seems to behave as intended.

The data looks roughly like this:

> str(epdata[c("epnum", "rating", "season")])
'data.frame': 97 obs. of  3 variables:
 $ epnum : int  1 2 3 4 5 6 7 8 9 10 ...
 $ rating: num  82 83 85 86 85 84 88 83 87 86 ...
 $ season: Ord.factor w/ 6 levels "1"<"2"<"3"<"4"<..: 1 1 1 1 1 1 1 1 1 1 ...

With a full set of sample data available here: https://paste.xinu.at/vdm/

I have no idea whether this is a shiny, ggvis, vega or browser issue, so I'd appreciate any hint I can get on this.

Regards,
Jemus42

Hadley Wickham

unread,
Jul 16, 2014, 7:02:06 PM7/16/14
to Jemus42, gg...@googlegroups.com
Hi Jemus42,

Could you please try creating a minimal example? We're most likely to
be able to help if you can spend some time isolating the problem.

Hadley
> --
> You received this message because you are subscribed to the Google Groups
> "ggvis" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggvis+un...@googlegroups.com.
> To post to this group, send email to gg...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/

Jemus42

unread,
Jul 17, 2014, 8:58:48 AM7/17/14
to gg...@googlegroups.com, jem...@gmail.com
Of course, I should have thought of that, sorry.

I tried my best to  recreate the basic concept, and it seems it's primarily an issue with the tooltip functionality, or so it seems at least.
Although in this example, some points vanish on the spot upon rendering, which is not the case with my actual plot.

However, both throw an error referencing the variable used for the tooltip
I tried circumventing this by using a function in `add_tooltip()` which only returns the tooltip variably if it 1) !is.null(tooltipvar) or 2) "tooltipvar" %in% data,
but that didn't help. I also tried to explicitly give the tooltip variable as a prop to layer_points, but then it complained about the key variable not being allowed to be constant, but my supsicion remains that either the grouping done by group_by() or the layer_model_prediction() function seem to cause problems with the key/tooltip.

I hope this somewhat helps, I've been fiddleing around with this on and off for a few days now and feel like I've lost track of everything I've tried so far.

Regards,
Jemus42

Jemus42

unread,
Jul 17, 2014, 4:34:41 PM7/17/14
to gg...@googlegroups.com
I solved it! And of course, the solution was way too simple as that I could have thought of that a week ago.

When you're creating a ggvis plot and intend to set a key property to later use as a tooltip, it might be a good idea to set that only in the layer you actually want to apply that tooltip to, because otherwise you'll run into problems if you later use group_by() to group your data and your original tooltip variable will… well, I don't know exactly what happened, but the TL;DR is:

     plot <- data %>% ggvis(x    = ~epnum,
                           y    = ~rating,
                           fill = ~season)
    plot <- plot %>% layer_points(key := ~tooltip, …)
    if ("Show" %in% input$btn_trendlines && var_y == "rating"){
      plot <- plot %>% layer_model_predictions(model = "lm", se = F, stroke := "black")
    }
    if ("Season" %in% input$btn_trendlines && var_y == "rating"){
      plot <- plot %>% group_by(season) %>% layer_model_predictions(model = "lm", se = F, stroke = ~season)
    }

works.

Now I only have to find out how to hide the legend for the stroke property I used for the regression lines.

Anyways, thanks to anyone reading, and a friendly hello to future people who may run into a similar issue and stumble upon this post.
Reply all
Reply to author
Forward
0 new messages