如何通过从checkboxGroup中选择状态来从UI并排交互式地绘制图形?

时间:2019-04-04 10:34:55

标签: r shiny

数据包括美国各州,其构造类型(字符),两个模型(即4个因子)的变量损失和损失成本及其各自的值(浮动)。施工类型将在X轴上,损失成本将绘制条形图,损失将绘制线形图。复选框组应从用户那里获取输入,并按照选择的顺序并排绘制四个图形。

我试图绘制图形,但是无法以正确的方式显示。

请协助

该代码绘制了从用户那里获取输入的图形列表。例如:PLot 1,Plot2,PLot 3,Plot4和Plot5。如果选择选项1,2,4,5,它将为Plot 3留一个空白,并在列中绘制图形

ui <- fluidPage(

  headerPanel("Construction"),
  sidebarPanel(

    dropdownButton(label= "City", status = "default",width=400,tooltip = TRUE,circle = FALSE,

                   checkboxGroupInput("check","Check" ,choices = unique(raw$City), selected = NULL)),
    actionButton("plot","Display")

  ),

  mainPanel(

    lapply(1:length(unique(raw$City)), function(i)  {  
       plotOutput(paste0('plot',i))

    }
    )
  )
)



server <- function(input, output) {

  takinput <- reactive({

    a<- subset(raw, City %in% input$check)
    return(a)
  })

  plotlist <- lapply(1:length(unique(raw$City)), function(i)  { 

    output[[paste0('plot',i)]] <- renderPlot({

      input$plot 
      isolate(

        if(any(choice[i] %in% input$check)){

          b<- takinput()
          #View(b)
          ggplot(data = b,aes(x= Construction.Type, y= value,fill = variable)) +
            geom_bar(data = subset(b, variable %in%  c("RMS.AAL.per..Million","AIR.Loss.Cost.per..Million")
                                   & City == choice[i]) ,stat = 'sum' , position = "dodge", na.rm=TRUE)+

            guides(fill =  FALSE)+
            geom_line(data = subset(b, variable %in% c("RMS.Relative.AAL","AIR.Relative.Loss.Cost")
                                    & City == choice[i]), linetype = "dashed", 
                      aes(y = value*(1400/1.4),group =variable, colour= variable))+

            scale_color_manual(values =c("RMS.Relative.AAL" = "#7030A0","AIR.Relative.Loss.Cost" = "#FFC000")) +
            scale_fill_manual(values = c("#FFC000","#FFC000","#7030A0",'#7030A0'))+
            scale_y_continuous(name = expression("AAL Per Million"), sec.axis =sec_axis( ~.*1.4/1400, name = "Relative AAL"))+
            theme(axis.text.x = element_text(angle= 25, vjust = 1.0,hjust = 1.0), legend.position = "none",
                  panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
                  panel.background = element_blank(), axis.line=element_line(colour = "black"))+ 
            labs(title= as.vector(choice[i]))+
            annotate(geom="text",x = c(4,4),y= c(1200,1140),label = c("RMS","AIR"))+
            annotate(geom="rect",xmin = c(3.3,3.3),xmax = c(3.7,3.7), ymin = c(1120,1180),ymax = c(1165,1225),
                     colour = c("#FFC000","#7030A0"), fill = c("#FFC000","#7030A0"))
        }

      )
    })
  })

}

shinyApp(ui,server)

预期结果:按照用户输入的顺序,在2 x 2网格中绘制4个图。

实际结果:该代码绘制了从用户那里获取输入的图形列表。例如:PLot 1,Plot2,PLot 3,Plot4和Plot5。如果选择选项1,2,4,5,它将为Plot 3留一个空白,并在列中绘制图形。

0 个答案:

没有答案