我为即将到来的澳大利亚大选创建了一个应用程序:https://regionalinnovationdatalab.shinyapps.io/Dashboard/
有效的应用程序代码和数据(其中不包含我寻求帮助的修改内容): https://gitlab.com/r.chappell/2019_ElectionApp_RIDL
我想创建一个多个选项卡(“人口统计”,“收入”,“房屋”等),使用户可以将选民数据与国家和州的平均值进行比较。选民级别数据是基于从下拉菜单中选择的选民的反应性的,但是我希望显示国家和州级别图,而与从下拉菜单中选择的选民无关。
例如,在“受众特征”标签上,我想要3个地块。 1.选定选民的年龄和性别人口 2.全澳大利亚(AUS)按年龄和性别划分的人口-无论选择哪个选民,都要显示) 3.整个昆士兰州(QLD)的按年龄和性别分布的人口-无论选择哪个选民都可以显示
我无法弄清楚如何将AUS和QLD数据绘制成图表,而不将它们合并到选民下拉菜单的无功输入中。我不想这样做的原因是实际的应用程序使用空间数据并将其映射出来,因此我将不得不为Aus和Qld创建一个可单击的多边形,我不确定该如何做才能不失去可单击的选区特定的多边形。
请给我一些帮助,谢谢:)。
样本数据:https://drive.google.com/drive/folders/1byijKXHZwnJSzVv3UiNgjmUwAG3p9yZ7?usp=sharing
#read in data
df.age<-read.csv("ced.age.csv") #electorate level data use for options from drop down
qld.age<-read.csv("qld.age.csv") #state level data
aus.age<-read.csv("aus.age.csv") #national data
#UI
ui <- shiny::navbarPage(title= "Federal Election Dashboard",
theme = "journal",
tabPanel("Commonwealth Electoral Divisions",
column(selectInput("division", "",
label="Select an electorate, graphs will be updated.",
choices = ced.age$Elect_div),
plotlyOutput("ageBar", height="300px", width = "500px"))),
tabPanel("Demographics",
radioButtons(inputId="View", label="Select an option", choices = c("Absolute_Numbers", "Percentages"), selected = "Absolute_Numbers"),
plotlyOutput("ageBar2"),#, #CED
plotlyOutput("age_qld"),#qld
plotlyOutput("age_aus")))
br()
#~~~~~~AESTHETIC STUFF~~~~~~~
#colour scheme for bar charts
mycol=c("#021893", "#0099CC", "#740699","#C12525","#990623")
MF=c("#2359AF","#970B76")
point <- format_format(big.mark = " ", decimal.mark = ",", scientific = FALSE)
#~~~~SERVER CODE~~~~~
server <- function(input, output, session) {
#Create the plots for all the different bar charts we want displayed using the dfs we created above (age, income etc.)
#using ggplot and plotly to create the bar charts/ distributions
#population per electorate by age and sex
output$ageBar <- renderPlotly({
ageplot<-ggplot(
subset(df.age,df.age$Elect_div==input$division),
aes(variable2, value)) + geom_bar(stat = "identity", position ="stack", aes(fill = variable3))+
scale_x_discrete(labels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34", "35-39","40-44",
"45-49","50-54","55-59","60-64","65-69",
"70-74","75-79", "80-84",">85")) +
theme_classic() + scale_fill_manual(values = MF, guide=FALSE) +
theme(axis.text.x = element_text(angle=45, hjust = 1, size = 8, face= 'bold'))+
labs(title = "Population", subtitle = "Data from ABS", x = "", y = "", fill= "Sex")
ggplotly(ageplot)%>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE)) })
output$ageBar2 <- renderPlotly({
ageplot<-ggplot(
subset(df.age,df.age$Elect_div==input$division),
aes(variable2, value)) + geom_bar(stat = "identity", position ="stack", aes(fill = variable3))+
scale_x_discrete(labels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34", "35-39","40-44",
"45-49","50-54","55-59","60-64","65-69",
"70-74","75-79", "80-84",">85")) +
theme_classic() + scale_fill_manual(values = MF, guide=FALSE) +
theme(axis.text.x = element_text(angle=45, hjust = 1, size = 8, face= 'bold'))+
labs(title = "Population", subtitle = "Data from ABS", x = "", y = "", fill= "Sex")
ggplotly(ageplot)%>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE)) })
#attempt to create some reactive data for the state and national level data to change the y variable to display absolute or percentages based on radio button input
qld_age_reactive<-reactive({
if (input$View=="Absolute_Numbers"){qld.age$View<-qld.age$Absolute_Numbers} else{
qld.age$View<-qld.age$Percentages
}
qld.age
})
#population for QLD by age and sex
output$age_qld <- renderPlotly({
ageQld<-ggplot(data=qld_age_reactive(), aes(x=variable2, y=View)) + geom_bar(stat = "identity", position ="stack", aes(fill = variable3))+
scale_x_discrete(labels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34", "35-39","40-44",
"45-49","50-54","55-59","60-64","65-69",
"70-74","75-79", "80-84",">85")) +
theme_classic() + scale_fill_manual(values = MF, guide=FALSE) +
theme(axis.text.x = element_text(angle=45, hjust = 1, size = 8, face= 'bold'))+
labs(title = "State Population", subtitle = "Data from ABS", x = "", y = "", fill= "Sex")
ggplotly(ageQld)%>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE)) })
#population for AUS by age and sex
output$age_aus <- renderPlotly({
ageAus<-ggplot(data=aus.age, aes_string(x=variable2, y=Absolute_Numbers)) + geom_bar(stat = "identity", position ="stack", aes(fill = variable3))+
scale_x_discrete(labels=c("0-4","5-9","10-14","15-19","20-24","25-29","30-34", "35-39","40-44",
"45-49","50-54","55-59","60-64","65-69",
"70-74","75-79", "80-84",">85")) +
theme_classic() + scale_fill_manual(values = MF, guide=FALSE) +
theme(axis.text.x = element_text(angle=45, hjust = 1, size = 8, face= 'bold'))+
labs(title = "Australian Population", subtitle = "Data from ABS", x = "", y = "", fill= "Sex")
ggplotly(ageAus)%>% config(displayModeBar = F) %>% layout(xaxis=list(fixedrange=TRUE)) %>% layout(yaxis=list(fixedrange=TRUE)) })
}
shinyApp(ui, server)