【发布时间】:2021-07-23 08:01:49
【问题描述】:
我没有收到任何错误消息,所以我有点卡在这里。一切似乎都工作正常,但是当我在应用程序中移动滑块小部件时,绘图上的日期不会更新。有些东西告诉我我的 ggplot 中遗漏了一些东西(与 xlim 相关?),但我尝试的一切都会破坏应用程序。我附上了下面的代码,以及我的数据集示例。
rm(list=ls())
#Libraries
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)
library(readxl)
library(scales)
library(ggthemes)
library (plotly)
library(shinythemes)
#Load Data
data<-read_excel("C:/Users/Kanew/OneDrive/Desktop/RApp/Unemployment2.xlsx")
data$Date<-as.Date(data$Date)
ui<-fluidPage(
theme = shinytheme("cerulean"),
tabsetPanel(
tabPanel("Welcome",
HTML(paste(
h1("Welcome!"),
p("Welcome to the Unemployment Exploration Application. In this application you will find tabs,
located at the top of your screen, for data visualization and data table exploration."), '<br/>',
p("The data visualization tab includes functionality to visualize the unemployment rate by various
racial/ethnic categories of your choice. The graph includes functions for magnification, panning,
and observing individual data points at your choice (by hovering over data points)."), '<br/>',
p(" The data table tab is a searchable data table that allows you to search the dataset and download the
dataset if you'd like."),'<br/>',
p("Enjoy!")))),
tabPanel("Data Visualization",
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "Date",
label = "Dates:",
min = as.Date("2010-01-01", "%Y-%m-%d"),
max = as.Date("2021-02-01","%Y-%m-%d"),
value = as.Date("2010-01-01"), timeFormat = "%Y-%m-%d",
step = 1, animate = animationOptions(interval = 1800))),
mainPanel(h1("National Unemployment Rate by Racial/Ethnic Category"),
plotlyOutput("plot", height = 'auto', width = 'auto'),
selectInput(
inputId = "Category",
label = "Select Race or National Rate",
choices = c("All",unique(data$Category)),
selected = "All")))),
tabPanel("Data Table",
h1("Data Table"),
DT::dataTableOutput("data"))
))
server<-function(input, output){
output$plot <- renderPlotly({
data %>%
filter(Category == input$Category | input$Category == "All" | Date == input$Date) %>%
ggplot(data, mapping = aes(x=Date, y = Rate, colour = Category)) +
geom_line() +
xlab("Year") + ylab("Unemployment Rate (%)") +
scale_y_continuous(breaks=c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18))+
scale_x_date(date_breaks = "years" , date_labels = "%Y")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
theme_economist() +
scale_fill_economist()})
output$data <- DT::renderDataTable({DT::datatable(data)})
}
shinyApp(ui = ui, server = server)
> head(data)
# A tibble: 6 x 3
Date Category Rate
<date> <chr> <dbl>
1 2010-01-01 National 9.8
2 2010-02-01 National 9.8
3 2010-03-01 National 9.9
4 2010-04-01 National 9.9
5 2010-05-01 National 9.6
6 2010-06-01 National 9.4
> head(data, 20)
# A tibble: 20 x 3
Date Category Rate
<date> <chr> <dbl>
1 2010-01-01 National 9.8
2 2010-02-01 National 9.8
3 2010-03-01 National 9.9
4 2010-04-01 National 9.9
5 2010-05-01 National 9.6
6 2010-06-01 National 9.4
7 2010-07-01 National 9.4
8 2010-08-01 National 9.5
9 2010-09-01 National 9.5
10 2010-10-01 National 9.4
11 2010-11-01 National 9.8
12 2010-12-01 National 9.3
13 2011-01-01 National 9.1
14 2011-02-01 National 9
15 2011-03-01 National 9
16 2011-04-01 National 9.1
17 2011-05-01 National 9
18 2011-06-01 National 9.1
19 2011-07-01 National 9
20 2011-08-01 National 9
根据 ismirsehregal 的解决方案更新代码:
rm(list=ls())
#Libraries
library(shiny)
library(shinyWidgets)
library(ggplot2)
library(dplyr)
library(readxl)
library(scales)
library(ggthemes)
library (plotly)
library(shinythemes)
#Load Data
data<-read_excel("C:/Users/Kanew/OneDrive/Desktop/RApp/Unemployment2.xlsx")
uniqueCats <- unique(data$Category)
data$Date <- as.Date(data$Date)
ui<-fluidPage(
theme = shinytheme("cerulean"),
tabsetPanel(
tabPanel("Welcome",
HTML(paste(
h1("Welcome!"),
p("Welcome to the Unemployment Exploration Application. In this application you will find tabs,
located at the top of your screen, for data visualization and data table exploration."), '<br/>',
p("The data visualization tab includes functionality to visualize the unemployment rate by various
racial/ethnic categories of your choice. The graph includes functions for magnification, panning,
and observing individual data points at your choice (by hovering over data points)."), '<br/>',
p(" The data table tab is a searchable data table that allows you to search the dataset and download the
dataset if you'd like."),'<br/>',
p("Enjoy!")))),
tabPanel("Data Visualization",
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "Date",
label = "Dates:",
min = min(data$Date),
max = max(data$Date),
value = min(data$Date),
timeFormat = "%Y-%m-%d",
step = 1,
animate = animationOptions(interval = 300))),
mainPanel(h1("National Unemployment Rate by Racial/Ethnic Category"),
plotlyOutput("plot", height = 'auto', width = 'auto'),
selectInput(
inputId = "Category",
label = "Select Race or National Rate",
choices = c("All", unique(data$Category)),
selected = "All")))),
tabPanel("Data Table",
h1("Data Table"),
DT::dataTableOutput("myDataTable"))
))
server<-function(input, output){
output$plot <- renderPlotly({
if(input$Category == "All"){
catFilter<-uniqueCats
} else{
catFilter<-input$Category
}
filteredData <- filter(data, Category %in% catFilter & Date <= input$Date)
req(nrow(filteredData) > 0)
myGGPlot <- ggplot(filteredData, mapping = aes(x = Date, y = Rate, colour = Category),
geom_line() +
geom_point() +
xlab("Year") + ylab("Unemployment Rate (%)") +
scale_y_continuous(breaks=c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18))+
scale_x_date(date_breaks = "years" , date_labels = "%Y")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
theme_economist()+
scale_fill_economist(),
ggplotly(myGGPlot))
})
output$myDataTable <- DT::renderDataTable({
DT::datatable(data)
})
}
shinyApp(ui = ui, server = server)
【问题讨论】:
-
好问题 - 我想要选择要绘制哪些系列的选项,还想要一次绘制所有系列的选项。 IE。所有不同类型的失业与其他一些单一类型的失业(现在复选框有问题,但我稍后会处理)。关于 x 轴上的日期,它是一个时间序列图。所以这个想法是用户可以选择查看 2010-2011 或 2010-2015 等。
-
请使用
dput(head(data, 20))的输出分享您的数据。 -
请检查我的回答。
-
@ismirsehregal 抱歉耽搁了!应用程序运行,但数据可视化选项卡的绘图输出上没有任何图形?我用修改后的代码更新了问题
-
我在答案中添加了一张图片。输出很好。您需要使用滑块向图表添加更多点。
标签: r ggplot2 shiny plotly r-plotly