Reputation: 391
I am working on an interactive shiny plot where the user can select what they want on each axis, and the attribute that colours the points.
For simplicity, I have provided a dummy data set here:
test_df <-tibble(test_x = sample(c(1:100), 10),
test_y = sample(c(1:100), 10),
variable = rep(c("A", "B"), 5),
colour = rep(c("orange", "green"), 5))
The following code works fine (and also works for my real reactive plot, which is a lot more complex and has additional arguments):
test_df %>%
ggvis(x = ~test_x, y = ~test_y, fill:= ~colour) %>%
layer_points()
However, as soon as I try to add in the legend (using add_legend()
), the plot produced is blank. It does not produce error messages (neither in this dummy code nor the real example). Code used is:
test_df %>%
ggvis(x = ~test_x, y = ~test_y, fill:= ~colour) %>%
layer_points() %>%
add_legend(scales = "fill", title = "blah")
I have attempted to use the information provided in the following issues plus the help information, but with no luck.
ggvis interactive figure does not work as expected using reactive values
ggvis output not appearing in shiny application
I feel like I am missing something very obvious, so any advice or guidance would be very appreciated. I also am aware that the shiny version will require more complexity in the code, but I feel if I can get it working locally first this will help me figure out the next step.
Note: I need to use ggvis to enable hover over tooltips. I know this can be done simply using other graphical packages, but I need the ggvis answer.
Thank you!
Upvotes: 0
Views: 93
Reputation: 2995
I can't figure out why your code doesn't work. Maybe it's just that it's not meant to be used this way. The only workaround I found is the one below, but I'm pretty sure something better can be done.
test_df %>%
ggvis(x = ~test_x, y = ~test_y, fill = ~colour) %>%
add_legend(scales = "fill", title = "blah") %>%
scale_ordinal("fill", range = unique(test_df$colour)) %>%
layer_points()
Upvotes: 1