问题描述:

Please find the code below for a shiny App using ggplot2, I do not know how to sort them inside the server.R code.

WIth the below code I am able to display the bar chart and change it but ordering the data seems to be an issue.

ui.R

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")

)

))

server.R

library(shiny)

library(ggplot2)

library(scales)

library(dplyr)

a<-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 <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

#cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")

#sum1<-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")

#sum1<-sum1[,-9]

shinyServer(function(input,output){

output$text1<- renderText({

paste("Template 1 is:",input$var)})

output$stack<-renderPlot({

p<-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<-renderDataTable({sum1})

})

Data Structure

> 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 ...

网友答案:

You just have to use the sorting as in:

sum2$TicketType<- reorder(sum2$TicketType, -sum2[,input$var])

This works just fine for me.

# server.R

library(shiny)
library(ggplot2)
library(scales)
library(dplyr)
a<-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 <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
#sum1<-read.csv("E:/R/shiny/FirstTryShiny-Version-3/data/sum1.csv", header= TRUE,sep=",")
#sum1<-sum1[,-9]

sum1 <- data.frame(TicketType=c("Test1", "Test2", "Test3", "Test4"),hrs_0to1=c(4.04,21.39,0.14,24.95), hrs_1to6=c(3.08, 32.03, 0.18, 9.3))

shinyServer(function(input,output){

output$text1<- renderText({
paste("Template 1 is:",input$var)})

output$stack<-renderPlot({

sum1$TicketType<- reorder(sum1$TicketType, -sum1[,input$var])

p<-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<-renderDataTable({sum1})

})
相关阅读:
Top