Search code examples
shinyshinydashboard

Shiny plot is outside the dashboardBody


Please see attached image. Do you have suggestions how to avoid that the plot is outside the white area, or to make the grey area below the plot white?

ui <- dashboardPage(
  # Application title
  dashboardHeader(title=h4(HTML("Virus Coverage plot"))),
  dashboardSidebar(
    useShinyjs(),
    
    selectInput("Taxa", "Taxa", choices = unique(files.Vir.DNA.df.test$V1))
  ),
  
  dashboardBody(
    tabsetPanel(
      tabPanel("Taxa", plotOutput("myplot1"))
      
    )
    )
)

server <- function(input, output, session) {
  
  data_selected <- reactive({
    filter(files.Vir.DNA.df.test, V1 %in% input$Taxa)
  })
  
  output$myplot1 <- renderPlot({
   #data_selected() %>%
    #  filter(Cancer=="Anus" | Cancer=="Cervix") %>%
        p <- ggplot(data_selected(),aes(position,rowSums, fill = V1)) + 
        #theme_bw(base_size = 6) +
        geom_bar(stat="identity") +
        facet_grid(Cancer~. , scales = "free_x", space = "free_x", switch = "x") +
        theme(strip.text.y = element_text(angle = 0),
            strip.text.x = element_text(angle = 90),
            strip.background = element_rect(colour = "transparent", fill = "transparent"),
            plot.background = element_rect(colour = "white", fill = "white"),
            panel.background = element_rect(colour = "white", fill = "white"),
            
            axis.text.x = element_blank(),
            axis.ticks.x = element_blank()) + 
      labs(y="Sum coverage within cancer type", x="", title="") +
      scale_fill_manual(values=mycolors) + 
      theme(legend.position = "none") 
      #scale_y_log10() 
      print(p)
  },res = 100,width = 600, height = 1200)
}

shinyApp(ui, server)

enter image description here


Solution

  • Your example isn't reproducible - so I made a new one.

    You just need to wrap the plotOutput in a fluidRow:

    library(shiny)
    library(ggplot2)
    library(datasets)
    library(shinydashboard)
    
    ui <- dashboardPage(
      dashboardHeader(),
      dashboardSidebar(),
      # dashboardBody(plotOutput("myplot")) # exceeds body
      dashboardBody(fluidRow(plotOutput("myplot"))) # works
    )
    
    server <- function(input, output, session) {
      output$myplot <- renderPlot({
        scatter <- ggplot(data=iris, aes(x = Sepal.Length, y = Sepal.Width)) 
        scatter + geom_point(aes(color=Species, shape=Species)) +
          xlab("Sepal Length") +  ylab("Sepal Width") +
          ggtitle("Sepal Length-Width")
      }, height = 1200)
    }
    
    shinyApp(ui, server)