I am building a Shiny App. I am plotting a graph with ggvis, following the nice Movies example. My issue is that I am also allowing the user to select the variable to be used to `fill' in the graph, which means that there are multiple colors instead of all points black. I want to change the default colors of the points.
I want to use the colour palette viridis instead of the basic colors. So this is what I do, and it does work:
vis <- reactive({
dataset %>%
ggvis(x = ~Date, y = ~mentions, fill = ~get(input$z)) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := input$alpha, fillOpacity.hover := 1, key := ~Filename) %>%
scale_ordinal("fill", range = viridis(n = XXX)) %>%
add_tooltip(speech_tooltip, "hover") %>%
add_axis("x", title = "Date") %>%
add_axis("y", title = paste0("Mentions of `", req(input$keyword), "'"))
})
The only problem here is that the viridis() function requires me to set n, the number XXX of colors I need. However, I would like this to be set dynamically: if the user sets input$z on 'Region', there are six regions, so I want n = 6. If he choses to use instead 'Language' or 'Instituttion, there is 30 languages and 100 institutions, so I want n to evolve accordingly.
I'm sure there is an elegant way of doing this, basically counting the number of different possibilities there are for the input$z, but I don't find it.
Thanks!