0

I am new here and need some help! I am working on a shiny app for a class project and I am almost done with one of my tabs. I need help with sorting my bars. My bars are not in order by month, what I mean is February should come after January but its the inverse in my graph. I have been looking all over and haven't found a solution that didn't throw and error...so I created an account hoping someone could point me in the right direction.

Here is the link to the dataset: https://www.kaggle.com/jessemostipak/hotel-booking-demand

Here is my code and image of my shiny app working

#install packages
library(shiny)
library(shinythemes)
library(ggplot2)
library(dplyr)

#loading data
hotel_df <- read.csv("/Users/chrismcmanus/Documents/DSBA 5122 - Visual      Analytics/ShinyApp/Data/Spreadsheet for ShinyApp.csv")

#list of hotels
hotel_list <- list("Resort Hotel","City Hotel")

#converting to long data
dfm <- melt(hotel_df, id.vars=c("stays_in_weekend_nights", "stays_in_week_nights",  "hotel", "arrival_date_month"))

#creating UI layout
ui <- fluidPage(theme=shinytheme("cyborg"),
titlePanel("Hotel popularity based on count of stays"),
  sidebarLayout(
    sidebarPanel(
    selectInput(inputId="hotel_2", label="Select the Hotel", choices=hotel_list)),

  mainPanel(
    tabsetPanel(
                tabPanel("Weekend Nights", plotOutput("plot")),
                tabPanel("Week Nights", plotOutput("plot_2"))

))))

#creating server
server <- function(input, output, session) {
   htl_type <- reactive({filter(dfm, hotel==input$hotel_2)})

    output$plot <- renderPlot({
 ggplot(htl_type(), aes(x=stays_in_weekend_nights, y=arrival_date_month)) +
        geom_bar(stat="identity", position="dodge") + labs(x="Count of stays during   Weekend", y="Check-in Month") +
        theme(panel.grid.major = element_blank(), panel.grid.minor =  element_blank(),
               panel.background = element_blank(), axis.line = element_line(colour    = "black")) + coord_flip()
     })

     output$plot_2 <- renderPlot({
      ggplot(htl_type(), aes(x=stays_in_week_nights, y=arrival_date_month)) +
        geom_bar(stat="identity", position="dodge") + labs(x="Count of stays during Week", y="Check-in Month") +
        theme(panel.grid.major = element_blank(), panel.grid.minor =  element_blank(),
               panel.background = element_blank(), axis.line = element_line(colour = "black")) + coord_flip()
     })

 }

 #displaying shiny app
 shinyApp(ui, server)

Any help would be greatly appreciated!

  • 2
    [where did you look?](https://stackoverflow.com/search?q=%5Br%5D+ggplot2+order+month), [what was the error?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) [1](https://stackoverflow.com/questions/20524408/how-do-you-order-months-in-ggplot) [2](https://stackoverflow.com/questions/57257683/how-to-plot-into-month-year-order-instead-of-alphabetic-order-in-ggplot) [3](https://stackoverflow.com/questions/36020146/how-can-i-order-the-months-chronologically-in-ggplot2-short-of-writing-the-month) – rawr Dec 05 '21 at 00:05
  • Is this what you want? https://stackoverflow.com/questions/25258409/sorting-of-bars-ggplot2-in-shiny – OTA Dec 05 '21 at 00:11
  • I have just been using google and the results led me to this site which helped me figure my other errors out. I am getting a feeling that the months column is a factor based on other threads. I tried the forcats method and that did not do anything. – Chris McManus Dec 05 '21 at 00:24
  • To you, "February" logically comes after "January", but according to computers, the *strings* sort with `F*` *before* `J*`. Regardless of the dataset, when you use `read.csv`, none of your columns will be `Date`- or `POSIXt`-class, and none of your code attempts to change that. Once you change all date-like columns, then look into `scale_x_date` and its methods of formatting (likely mentioned in the links provided). – r2evans Dec 05 '21 at 00:24
  • I suggest you stop trying to attack this within shiny, instead solving it on the console first. – r2evans Dec 05 '21 at 00:27
  • I figured it out! the solution was in this thread https://stackoverflow.com/questions/18413756/re-ordering-factor-levels-in-data-frame – Chris McManus Dec 05 '21 at 00:39

0 Answers0