基本:将ggplot插入rshiny仪表板的位置

时间:2020-05-21 03:43:36

标签: r ggplot2 plot shiny

我有两个图,我想在我的Shiny仪表板中的另一个图下面显示一个图。我很新,不知道如何将两个图表片段插入主仪表板代码中。

library(shiny)
library(semantic.dashboard)
library(ggplot2)
library(plotly)
library(DT)
devtools::install_github("ropensci/plotly")

library(plotly)
fig <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
fig

ui <- dashboardPage(
  dashboardHeader(color = "blue",title = "HMH Convalescent Plasma Transfusion Therapy", inverted = TRUE),
  dashboardSidebar(
    size = "thin", color = "teal",
    sidebarMenu(
      menuItem(tabName = "main", "Recipient Data", icon = icon("home")),
      menuItem(tabName = "extra", "Donor Data", icon = icon("table"))
    )
  ),
  dashboardBody(
    tabItems(
      selected = 1,
      tabItem(
        tabName = "main",
        fluidRow(
          box(width = 8,
              title = "Graph 1",
              color = "green", ribbon = TRUE, title_side = "top right",
              column(width = 8,
                     plotOutput("boxplot1")
              )
          ),
          box(width = 8,
              title = "Graph 2",
              color = "red", ribbon = TRUE, title_side = "top right",
              column(width = 8,
                     plotlyOutput("dotplot1")
              )
          )
        )
      ),
      tabItem(
        tabName = "extra",
        fluidRow(
          dataTableOutput("carstable")
        )
      )
    )
  ), theme = "cerulean"
)

server <- shinyServer(function(input, output, session) {
  data("mtcars")
  colscale <- c(semantic_palette[["red"]], semantic_palette[["green"]], semantic_palette[["blue"]])
  mtcars$am <- factor(mtcars$am,levels=c(0,1),
                      labels=c("Automatic","Manual"))
  output$boxplot1 <- renderPlot({
    ggplot(mtcars, aes(x = am, y = mpg)) +
      geom_boxplot(fill = semantic_palette[["green"]]) + 
      xlab("gearbox") + ylab("Miles per gallon")
  })
  
  output$dotplot1 <- renderPlotly({
    ggplotly(ggplot(mtcars, aes(wt, mpg))
             + geom_point(aes(colour=factor(cyl), size = qsec))
             + scale_colour_manual(values = colscale)
    )
  })
  output$carstable <- renderDataTable(mtcars)
})

shinyApp(ui, server)

需要插入的图表:

server <- shinyServer(function(input, output) {  
  output$Plot <- renderPlot({  
    Figure %>% drop_na(Requirement) %>% ggplot() + geom_line(aes(x=Day,y=Patient,group=Patient,col=Requirement),lwd=2.5) + scale_color_manual(values=cols) + scale_y_continuous(trans="reverse",breaks=seq(1,50,1))+scale_x_continuous(breaks=seq(-14,24,1))+theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank()) + geom_vline(xintercept=0) + geom_point(data=Figure_Event, aes(x=Day,y=Patient,shape=Event),size=4) + scale_shape_manual(values=c(15,22)) + scale_color_discrete(breaks=c("AmbientAir","LowFlow","HighFlow", "NIPPV","MechanicalVentilation","ECMO"))
  })
})

第二张图表:

ggplot(data=o2outcomes, aes(x=transfusion_date, y=ID, group=1)) +
  geom_line()+
  ylim(0, 100)+labs(y= "Number of Patients Transfused", x = "Transfusion Date")+ggtitle("Number of Patients Transfused Over Time") 
##create line graph 

我知道这很基础。只是似乎无法继续前进。谢谢。另外,如果有人知道如何显示我的标题,那将会很有帮助。谢谢

1 个答案:

答案 0 :(得分:1)

您的代码中都没有出现Figureo2outcomes这两个新图的源数据,因此我们无法为您提供完整的答案。

话虽如此,包括地块也应该很简单。只需遵循两个绘图输出所演示的模式即可。首先,将plotOutput("<yourId>")添加到ui函数中。您可以将它们添加到现有框之一中,也可以添加到自己的新框中。然后将output$<yourId> <- renderPlot({...})添加到您的server函数中。您用于第一个绘图的示例代码已经包装在renderPlot(和server函数中)中:不要复制包装器。第二个情节的代码没有包装程序,因此需要添加它。

缺少什么标题?

您的代码并不简单且自成体系。如果可以的话,将大大增加获得有用答案的机会。