Adding a layer to the current chart without creating a new one in ggplot2

In basic R, you can add layers to an 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 graph, 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 the plot, and the third creates a different schedule. Can I somehow add dots to an existing plot created by the second line? Is there something like add=TRUEggplot?

I want this behavior to be that using ggplot2 for brilliant purposes blinks in its animation.

+3
source share
2 answers

. reactiveValue . , . , , , . diamond ggplot2, , .

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 to this version
        ## output$plot <- renderPlot({
        ##     vals$pdata
        ## })
    })
)
+3

, css, , id . !

, , , chi-: tags$style(type="text/css", ".recalculating {opacity: 1.0;}")

0

Source: https://habr.com/ru/post/1655311/


All Articles