简体   繁体   中英

Density Plots in Shiny R not working as intended

I've been building my first applications in shiny in order to publish them and share interactive graphs with my coworkers. At present I've been producing density plots to map behaviour. The Goal is to display between 1 and 4 plots as facets, depending on the "origin" selection and the "variable" selection in shiny. If I manually plot all 4 graphs, I get an output resembling this:

sampleplot <- data.frame("origin" = c("US","GB","GB","US","CA","US","GB","GB","US","CA","US","GB","GB","US","CA","US","GB","GB","US","CA"),
                         "variable" = c("p1","p2","p3","p1","p4","p1","p2","p3","p1","p1","p1","p2","p3","p1","p4","p1","p2","p3","p1","p1"),
                         "value" = runif(20, min = 0.2, max = 0.8))

sampleplot %>%
  ggplot(aes(value, color = origin)) +
  facet_wrap(~ variable, ncol = 2) +
  geom_density()

However, in shiny, when you select "p1", and then subsequently "p2", "p3" or "p4", the plot changes for "p1" (not just because of the scale, the shape is altered). It happens with any combination of variables. I get a warning along those lines when that happens:

Warning in ==.default (variable, input$var): longer object length is not a multiple of shorter object length

I don't get what the issue is. The variable checkbox input is every type of variable listed, and each facet is essentially an individual subplot. So why does plotting the density of "p2" have an impact on the density of "p1". Would be great if anyone could shine some line on the problem for me.

I made a reproducible shiny app with the data and ggplot call provided, and taking the change in scale apart, nothing rare happens. Let me know if it helps. It would be helpful if you can provide some code of your app, so we know exactly what's going with your plot.

Example app:

library(shiny)
library(tidyverse)

sampleplot <- data.frame("origin" = c("US","GB","GB","US","CA","US","GB","GB","US","CA","US","GB","GB","US","CA","US","GB","GB","US","CA"),
                         "variable" = c("p1","p2","p3","p1","p4","p1","p2","p3","p1","p1","p1","p2","p3","p1","p4","p1","p2","p3","p1","p1"),
                         "value" = runif(20, min = 0.2, max = 0.8))


ui <- fluidPage(sidebarLayout(
    sidebarPanel(selectInput('selection', "Select sampleplot's variable",choices = unique(sampleplot$variable), multiple = TRUE, selected = 'p1')),
    mainPanel(plotOutput('sameplots'))
)
  
)

server <- function(input, output, session) {


    
    reactive_sameplot <- reactive({
        sampleplot %>%
            filter(variable %in% input$selection) %>% 
            ggplot(aes(value, color = origin)) +
            facet_wrap(~ variable, ncol = 2) +
            geom_density()
        })
    
    output$sameplots <- renderPlot({reactive_sameplot()})
    
}

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM