Say I have a data set that looks something like this:
| Number | Name | lat | long |
|---|---|---|---|
| 1 | A | 45 | 100 |
| 2 | B | 46 | 100 |
| 3 | C | 47 | 100 |
| ... | ... | ... | ... |
| 2000 | AAA | 48 | 100 |
I've built a shiny app that displays them all on a map based on a slider input:
#slider input:
sliderInput("data",
"Number",
min = 1,
max = max(data$number),
value = c(1,100))
#reactive to plot from
plot_data <- reactive({ data %>% filter(number <= max(input$data), number >= min(input$data))})
#map
output$distPlot <- renderPlotly({
mymap <- ggplot(states) + geom_sf() + geom_point(data = plot_data(), aes(x =
long, y = lat, color = number, text = name), alpha = a) +
scale_colour_viridis_c("Number", option = 'mako', direction = 1)
ggplotly(mymap, tooltip = c("number", "text"))
This works great when displaying the initial values of numbers 1-100. However, if I move the slider to plot all the points 1-2000, things get a bit cluttered. If I set alpha = 0.1 or 0.2, it works fine. I don't want to set this as a fixed value though, because alpha = 1 looks better when fewer points are plotted.
So, is there a way to vary alpha based on the number of points being plotted? This way, alpha can be close to 1 when there are fewer points selected with the slider, and closer to 0 when most of the points are selected. I think ideally, for X points plotted,
$$alpha = -\frac{0.9}{1999}(x-1)+1,$$
should work fine.
I've tried typing this kind of formula directly into the aes(), replacing x with count(input$data) or (max(input$data) - min(input$data)). This hasn't worked. I've also tried creating another reactive for x using the same count, max/min functions. This also hasn't worked. Why not and how can I do this instead? Thank you.