【发布时间】:2014-08-12 19:36:28
【问题描述】:
请在下面找到使用ggplot2 的shiny 应用程序的代码,我不知道如何在server.R 代码中对其进行排序。
使用下面的代码,我可以显示条形图并对其进行更改,但排序数据似乎是个问题。
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")
)
))
服务器.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})
})
数据结构
> 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 ...
【问题讨论】:
-
据我所知,您不能直接在 ggplot2 中排序,您必须重新排序因子。这对你的情况不起作用吗?
-
您可以尝试在绘图前先对变量 hrs_0to1 进行排序
-
它没有,因为在闪亮的代码中我的变量是
input$var,它必须用于订购数据,我不知道如何使用input$var和订购数据。 -
sum2$TicketType<- reorder(sum2$TicketType, -sum2$hrs_0to1) p<-ggplot(data = sum2,aes_string("TicketType","hrs_0to1",fill="TicketType"))+ geom_bar(stat="identity") print(p)使用此代码它可以工作,但问题是我需要创建我不知道的 hrs_0to1 变量。
标签: shiny ggplot2 r charts ggplot2 shiny