r - adding a layer to the current plot without creating a new one in ggplot2 -
in basic r can add layers existing plot without creating new one.
df <- data.frame(x = 1:10, y = runif(10)) plot(df, type = "l") points(df, add = t)
the second line creates plot , third line adds points existing plot. in ggplot2:
my_plot <- ggplot(df, aes(x, y)) + geom_path() my_plot my_plot + geom_point()
the second line creates plot , third 1 creates plot. can somehow add points existing plot created second line? there add=true
in ggplot?
the reason want behaviour using ggplot2 in shiny causes blinks in animations.
here idea. keep plot reactivevalue
, have observer update plot user inputs. then, have observer watch plot data render plot when plot data changes. way long calculations happen before plot data changed, rendering of plot should happen there should little visible interrupt. here example using diamond
dataset ggplot2
large enough slow when rendering paths.
shinyapp( shinyui( fluidpage( sidebarlayout( sidebarpanel( selectinput("x", "x", choices=names(diamonds)), selectinput("y", "y", choices=names(diamonds)), checkboxinput("line", "add line") ), mainpanel( plotoutput("plot") ) ) ) ), shinyserver(function(input, output, session) { data(diamonds) vals <- reactivevalues(pdata=ggplot()) observe({ input$x; input$y; input$line p <- ggplot(diamonds, aes_string(input$x, input$y)) + geom_point() if (input$line) p <- p + geom_line(aes(group=cut)) vals$pdata <- p }) observeevent(vals$pdata,{ output$plot <- renderplot({ isolate(vals$pdata) }) }) ## compare version ## output$plot <- renderplot({ ## vals$pdata ## }) }) )
Comments
Post a Comment