Skip to content
This repository has been archived by the owner on Feb 13, 2020. It is now read-only.

Using CheckboxGroupinput to populate four graphs sided by side instead of a list #538

Open
Dhrutic opened this issue Apr 2, 2019 · 1 comment

Comments

@Dhrutic
Copy link

Dhrutic commented Apr 2, 2019

No description provided.

@Dhrutic
Copy link
Author

Dhrutic commented Apr 2, 2019

The data comprises of City, Construction Type, variables, and Values. I am building an app to show 3 dimensional graphs , with X, Y and Z axes, of 4 cities in 2 rows depending on input provided by the user. There 2 factors 'AAL per Million' and 'Relative AAL' for 2 models AIR and RMS. The end result is to build a bar graph for 'AAL per Million' and a line graph for 'Relative AAL' in the same graph. The below code list the graphs. How should I plot 4 graphs for the selected cities in 2 rows.

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)  {   #lapply(1:4, function(i)  {
  plotOutput(paste0('plot',i))
  
}
))

)

server <- function(input, output) {

takinput <- reactive({
#input$plot
a<- subset(raw, City %in% input$check)
return(a)
})

lapply(1:length(unique(raw$City)), function(i) {
output[[paste0('plot',i)]] <- renderPlot({
input$plot
isolate(

    if(any(input$check %in% choice[i])){
      
      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)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant