title: Sorting of Bars-ggplot2 in shiny
body: <p>Please find the code below for a <a href="/questions/tagged/shiny" class="post-tag" title="show questions tagged &#39;shiny&#39;" rel="tag">shiny</a> App using <a href="/questions/tagged/ggplot2" class="post-tag" title="show questions tagged &#39;ggplot2&#39;" rel="tag">ggplot2</a>, I do not know how to sort them inside the server.R code.</p>
  
  <p>WIth the below code I am able to display the bar chart and change it but ordering the data seems to be an issue. </p>
  
  <p>ui.R</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  
  
  shinyUI(fluidPage(
  
      column(12,offset=5,
      titlePanel("Template Type by Hours")),
      br(),
      h6(textOutput("text1")),
  
      fluidRow(
  
          column(4,offset=0,
                 wellPanel(
                     selectInput("var","Hours",
                                 choices = colnames(sum1[2:8]),selected ="hrs_0to1")))),
  
          column(12,offset=0,
                  plotOutput("stack", height=550,width=1300)
              ),
  
          column(12,
                 dataTableOutput("table1")
                 )
  
      ))
  </code></pre>
  
  <p>server.R</p>
  
  <pre><code>library(shiny)
  library(ggplot2)
  library(scales)
  library(dplyr)
  a&lt;-theme(panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank(),
           panel.background=element_rect(fill="white",colour=NA),
           axis.ticks=element_blank(),axis.text.x = element_text(angle = 80, vjust = 1, hjust=1),
           legend.position="none")
  
  #cbPalette &lt;- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
  #cbbPalette &lt;- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
  #sum1&lt;-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")
  #sum1&lt;-sum1[,-9]
  
  shinyServer(function(input,output){
  
          output$text1&lt;- renderText({
          paste("Template 1 is:",input$var)})
  
          output$stack&lt;-renderPlot({
  
          p&lt;-ggplot(data = sum1,aes_string("TicketType",input$var,fill="TicketType"))+
          geom_bar(stat="identity") +
          scale_colour_continuous(low="#56B4E9",high ="#009E73") + ggtitle("Distribution for Templates")+
          geom_text(aes_string(label=input$var),size=3.5,vjust=1,colour="black")
  
          print(p+a)})
  
      output$table1&lt;-renderDataTable({sum1})
  
      })
  </code></pre>
  
  <p>Data Structure</p>
  
  <pre><code>&gt; str(sum1)
  'data.frame':   37 obs. of  8 variables:
   $ TicketType : Factor w/ 37 levels "Address change",..: 1 2 3 4 5 6 7 8 9 10 ...
   $ hrs_0to1   : num  4.04 21.39 0.14 24.95 0 ...
   $ hrs_1to6   : num  3.08 32.03 0.18 9.3 0 ...
   $ hrs_6to12  : num  3.06 23.68 0.28 19.5 0.09 ...
   $ hrs_12to24 : num  2.5 18.07 0.09 6.02 0.05 ...
   $ hrs_24to48 : num  1.32 7.41 0.11 1.43 0.08 0.64 0.08 1.99 0 4.74 ...
   $ hrs_48to96 : num  0.76 3.31 0.24 0.27 0 2.25 0.27 1.46 0.09 5.38 ...
   $ hrs_above96: num  0 1.97 0 0 0 3.95 0.66 3.29 0 3.29 ...
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/x06d8.jpg" alt="enter image description here"></p>