Search code examples
rscaleshinyapps

scale axis on bar plot doesn't look good in R Shiny, how to fix it?


I have the output of my shiny app with scales of the bar plot a bit off and lacking aesthetics. This is the picture with the bar plot on the dialogue box. And as you can see the x axis frequency scale is a bit off and not able to spot the numerical values.

enter image description here

and this is my code I am using:

ui <- bootstrapPage(
navbarPage(theme = shinytheme("flatly"), header = "",
             "Symptom Tracker", id = "nav",

             tabPanel("Interactive map",
                      div(class = "outer",
                          #tags$head(includeCSS("style.css")),

                          tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "style.css")),

                          leafletOutput("mymap", width = "100%", height = 1100),

                          tags$style(type = "text/css", ".container-fluid {padding-left:0px;padding-right:0px;}"),
                          tags$style(type = "text/css", ".navbar {margin-bottom: .5px;}"),
                          tags$style(type = "text/css", ".container-fluid .navbar-header .navbar-brand {margin-left: 0px;}"),
                          #Floating panel
                          absolutePanel(id = "controls", style="z-index:400;", class = "panel panel-default", fixed = TRUE,
                                        draggable = TRUE, top = 75, left = 90,
                                        width = 400, height = "auto",

                                        h4("Symptoms"),

                                        selectInput("symptom", "Select symptom", c("Chills",
                                                                                   "Cough", "Diarrhoea",
                                                                                   "Fatigue",
                                                                                   "Headache",
                                                                                   "Loss of smell and taste",
                                                                                   "Muscle ache",
                                                                                   "Nasal congestion",
                                                                                   "Nausea and vomiting",
                                                                                   "Shortness of breath",
                                                                                   "Sore throat",
                                                                                   "Sputum",
                                                                                   "Temperature")
                                        ),

                                        plotOutput("barplot"),


                                        tags$div(id="cite",
                                                 'Data provided by '
                                        )




                          )))
  )

)

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

  filtered_data <- reactive({
    gather_divided %>%
      dplyr::filter(Symptom %in% input$symptom)
  })

  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles(urlTemplate = "//{s}.tiles.mapbox.com/v3/jcheng.map-5ebohr46/{z}/{x}/{y}.png",
               attribution = 'Maps by <a href="http://www.mapbox.com/">Mapbox</a>') %>%
      addMarkers(data = filtered_data(), clusterOptions = markerClusterOptions(), layerId = filtered_data()$rownum)
  })

  # When a marker is hovered over...
  observeEvent(input$mymap_marker_mouseover$id, {

    ## when a marker is hovered over...subset data to that country
    filtered_data2 <- reactive({
      pointer <- input$mymap_marker_mouseover$id
      t <- 0.5
      la <- input$mymap_marker_mouseover$lat
      lo <- input$mymap_marker_mouseover$lng

      df <- subset(gather_divided_2, ((lat1-t < la & la < lat1+t) & (lon1-t < lo & lo < lon1+t)))
      df
    })

    output$barplot <- renderPlot({

      mycountry <- unique(filtered_data2()$Country)
      plot <- ggplot2::ggplot(filtered_data2(), aes(x = Symptom, y = Frequency, fill = Frequency)) +
        ggplot2::geom_bar(stat = "identity", position = "dodge") +
        ggplot2::scale_fill_viridis_c(option = "magma", direction = -1, breaks = unique(filtered_data2()$Frequency)) +
        scale_x_discrete(breaks = unique(filtered_data2()$Symptom)) +
        scale_y_continuous(breaks = unique(filtered_data2()$Frequency), labels=unique(filtered_data2()$Frequency) ) +
        # theme(legend.position = "right") +
        guides(fill = "none") +
        theme_minimal() + labs(fill=NULL, title=mycountry) + coord_flip() 

      #plotly::ggplotly(plot)
      plot
    })

    observeEvent(input$mymap_marker_mouseout$id, {
      leafletProxy("mymap") %>% clearPopups()
    })

  })

}

# Run the application
shinyApp(ui = ui, server = server)

Is there a way to fix the frequency axis of the bar plot? Wish to solve this somehow.


Solution

  • I have solved the mystery with the scale y by taking out the scale_y_continuous in renderLeaflet, more exactly, in ggplot