In basic R you can add layers to the existing plot without creating a new one.
df <- data.frame(x = 1:10, y = runif(10))
plot(df, type = "l")
points(df, add = T)
The second line creates a plot and the third line adds points to the existing plot. In ggplot2:
my_plot <- ggplot(df, aes(x, y)) + geom_path()
my_plot
my_plot + geom_point()
The second line creates a plot and the third one creates another plot. Can I somehow add the points to the existing plot created by the second line? Is there something like add=TRUE
in ggplot?
The reason I want this behaviour is that using ggplot2 in shiny causes blinks in its animations.
Here is an idea. Keep the plot as a
reactiveValue
and have an observer to update the plot with user inputs. Then, have another observer to watch the plot data that will render the plot when the plot data changes. This way the long calculations happen before the plot data is changed, so the rendering of the plot should happen so quickly that there should be very little visible interrupt. Here is an example using thediamond
dataset fromggplot2
which is large enough to be obviously slow when rendering paths.I have exactly the same problem, and I saw here using special css tag, it solved the problem, but actually it id not solve it for me. It might help you though!
I have just found out, however, that all what you need to do is to add this link of code in the ui chunck of code:
tags$style(type="text/css", ".recalculating {opacity: 1.0;}")