缘起:这次我们要使用的数据与之前的shiny文章([ R语言]数据可视化S01─shiny)相同,只是这次要制作的是Dashboard版本
方法:运用[ R语言]的[shiny]套件。
目标数据:交通部高速公路局交通数据库ETC(Electronic Toll Collection)数据─各类车种通行量统计(TDCS_M03A),2016-2019年。
1.加载套件。
library(ggplot2)
library(ggthemes)
library(shiny)
library(shinydashboard)
2.加载数据与前置处理。
setwd(“C:/Users/User/Desktop/Eric/data”)#设定档案路径
#加载数据,这边我们直接沿用上次([ R语言]数据可视化S01─shiny)的数据
a1<-read.csv(“a1.csv”)
a2<-read.csv(“a2.csv”)
b1<-read.csv(“b1.csv”)
b2<-read.csv(“b2.csv”)
c1<-read.csv(“c1.csv”)
c2<-read.csv(“c2.csv”)
d1<-read.csv(“d1.csv”)
d2<-read.csv(“d2.csv”)
3.shiny
ui是shiny中定义使用者看到的网页样子,dashboardPage是产生仪表板页面:
ui <- dashboardPage(
#skin设定颜色版颜色;dashboardHeader设定标题与标题宽度;#dashboardSidebar设定页面侧边拦
skin =“green”,
dashboardHeader(title =“ETC data of the first and second day of the Chinese New Year”,titleWidth = 600),
dashboardSidebar(),
#dashboardBody设置页面主文(gdgjyc);
fluidRow设定输入物件为浮动布局,较不会因为每个仁电脑屏幕的尺寸不同,导致物件错位;box(title,status,solidHeader,collapsible)产生方块物件的标题、颜色、格式及是否可折叠;
selectInput()设定输入的方式为选择勾选方式,ID是date、标签是Date:、可勾选的选项是ETCdata中date字段的所有数据(不重复计)
dashboardBody(
fluidRow(
box(title =“Select the date”,status =“primary”,solidHeader = TRUE,collapsible = TRUE,
selectInput(“date”,“Date:”,choices = unique(ETCdata$date)))
),
mainPanel(#设定输出
#产生tab页面;产生Plot;产生Summary;产生Table
#icon为(https://fontawesome.com/icons?from=io)网站中的名称,可加上小图标
tabsetPanel(type =“tabs”,
tabPanel(“Plot”,icon(“image”),plotOutput(“ETCPlot”)),
tabPanel(“Summary”,icon(“pen-fancy”),verbatimTextOutput(“summary”)),
tabPanel(“Table”,icon(“table”),tableOutput(“table”))
)
)
)
)
server是背后的代码,负责依照输入执行代码,并将输出回传:
注:这边代码我写的比较冗长,应该会有更好的写法,还是希望能够有所帮助,这边我也把代码的格式用的比较易读一点,所以看起来会更冗长
server <- function(input,output){
#ETCPlot为前面ui中的ETCPlot,这边定义他的功能,用if-eles if判断日期并使用ggplot2制作线图(ggplot2至作线图的解释请参考我的网站另一篇文章,连接在本文最下面)
output$ETCPlot <- renderPlot({
if(input$date==“2016-02-08”){
ggplot(a1,aes(x=time,y=flow,group = 1))+
geom_line(linetype =“solid”,size=1.5,color=“#00CED1”)+
labs(title = paste(“Traffic flow in”,input$date),x=“time”,y=“flow”)+
theme_stata()+
theme(axis.title.x = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5))+
theme(axis.title.y = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5,angle = 360))+
theme(axis.text.y = element_text(angle = 360))+
theme(plot.title = element_text(size=15,face =“bold”))
}else if(input$date==“2017-01-28”){
ggplot(b1,aes(x=time,y=flow,group = 1))+
geom_line(linetype =“solid”,size=1.5,color=“#2E8B57”)+
labs(title = paste(“Traffic flow in”,input$date),x=“time”,y=“flow”)+
theme_stata()+
theme(axis.title.x = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5))+
theme(axis.title.y = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5,angle = 360))+
theme(axis.text.y = element_text(angle = 360))+
theme(plot.title = element_text(size=15,face =“bold”))
}else if(input$date==“2018-02-16”){
ggplot(c1,aes(x=time,y=flow,group = 1))+
geom_line(linetype =“solid”,size=1.5,color=“#FFB90F”)+
labs(title = paste(“Traffic flow in”,input$date),x=“time”,y=“flow”)+
theme_stata()+
theme(axis.title.x = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5))+
theme(axis.title.y = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5,angle = 360))+
theme(axis.text.y = element_text(angle = 360))+ theme(plot.title = element_text(size=15,face =“bold”))
}else if(input$date==“2019-02-05”){
ggplot(d1,aes(x=time,y=flow,group = 1))+
geom_line(linetype =“solid”,size=1.5,color=“#EE6363”)+
labs(title = paste(“Traffic flow in”,input$date),x=“time”,y=“flow”)+
theme_stata()+
theme(axis.title.x = element_text(size = 15,face =“bold”,vjust = 0.5,hjust = 0.5))+
theme(axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5, angle = 360))+
theme(axis.text.y = element_text(angle = 360))+ theme(plot.title = element_text(size=15,face = "bold"))
}else if(input$date=="2016-02-09"){
ggplot(a2,aes(x=time,y=flow,group = 1))+
geom_line(linetype = "solid",size=1.5,color="#FF1493")+
labs(title = paste("Traffic flow in",input$date),x="time",y="flow")+
theme_stata()+theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5))+
theme(axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5, angle = 360))+
theme(axis.text.y = element_text(angle = 360))+
theme(plot.title = element_text(size=15,face = "bold"))
}else if(input$date=="2017-01-29"){
ggplot(b2,aes(x=time,y=flow,group = 1))+
geom_line(linetype = "solid",size=1.5,color="#008B8B")+
labs(title = paste("Traffic flow in",input$date),x="time",y="flow")+
theme_stata()+
theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5))+
theme(axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5, angle = 360))+
theme(axis.text.y = element_text(angle = 360))+
theme(plot.title = element_text(size=15,face = "bold"))
}else if(input$date=="2018-02-17"){
ggplot(c2,aes(x=time,y=flow,group = 1))+
geom_line(linetype = "solid",size=1.5,color="#90EE90")+
labs(title = paste("Traffic flow in",input$date),x="time",y="flow")+
theme_stata()+
theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5))+
theme(axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5, angle = 360))+
theme(axis.text.y = element_text(angle = 360))+ theme(plot.title = element_text(size=15,face = "bold"))
}else{
ggplot(d2,aes(x=time,y=flow,group = 1))+
geom_line(linetype = "solid",size=1.5,color="#008B45")+
labs(title = paste("Traffic flow in",input$date),x="time",y="flow")+
theme_stata()+
theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5))+
theme(axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5, hjust = 0.5, angle = 360))+
theme(axis.text.y = element_text(angle = 360))+ theme(plot.title = element_text(size=15,face = "bold"))
}
})
#summary为前面ui中的summary,这边定义他的功能,用if-eles if判断日期并使用summary()总结当日交通量
output$summary<-renderPrint({
if(input$date=="2016-02-08"){
summary(a1$flow)
}else if(input$date=="2017-01-28"){
summary(b1$flow)
}else if(input$date=="2018-02-16"){
summary(c1$flow)
}else if(input$date=="2019-02-05"){
summary(d1$flow)
}else if(input$date=="2016-02-09"){
summary(a2$flow)
}else if(input$date=="2017-01-29"){
summary(b2$flow)
}else if(input$date=="2018-02-17"){
summary(c2$flow)
}else if(input$date=="2019-02-06"){
summary(d2$flow)
}
})
#table为前面ui中的table,这边定义他的功能,用if-eles if判断日期并输出交通量数据
output$table <- renderTable({
if(input$date=="2016-02-08"){
a1[-1]
}else if(input$date=="2017-01-28"){
b1[-1]
}else if(input$date=="2018-02-16"){
c1[-1]
}else if(input$date=="2019-02-05"){
d1[-1]
}else if(input$date=="2016-02-09"){
a2[-1]
}else if(input$date=="2017-01-29"){
b2[-1]
}else if(input$date=="2018-02-17"){
c2[-1]
}else if(input$date=="2019-02-06"){
d2[-1]
}
})
}
启动shiny应用程序
shinyApp(ui,server xcsjbj)
4.大功告成
首先这是Plot作图的部分,可由左边选择想看的日期,右边就会自动更新为当日的线图。
再来是Summary的部分,可由右边图表上方3个选项(Plot、Summary及Table)选择数据呈现方式。
最后是原始数据的呈现,将呈现每日每隔30分钟的交通量。
由于这边只能用静态的方式呈现,大家可以在自己R语言中互动看看!