首页 > 解决方案 > 在 R 中制作交互式多条形图(使用 SelectizeInput 和 Reactive)

问题描述

我和我的团队开发了一个图表,该图表根据我们代码的一页上的选定国家/地区自动执行。这是针对个人资料页面的。在比较页面上,我们希望为多个选定的国家/地区填充相同的图表。下面是个人资料页面上单个 SelectInput 代码和单个图形代码的代码。下面是多图 SelectizeInput 的代码和比较页面上的多图代码。SelectizeInput 代码有效,但在我运行应用程序时图表未显示。我不断收到一条错误消息,提示“无法转换具有重复名称的数据框”。任何人都可以就如何使该图表适用于多个选定国家/地区提供任何意见吗?

#个人资料页面选择输入代码

 fluidRow(
             column(6,
                    wellPanel(
                        selectInput("country",
                                    "Select Country",
                                    choices = as.list(aypopdata.long$Country))
                    )
             ),

#个人资料页面图形代码

ay_res <- reactive({
    res <- aypopdata.long %>% filter(aypopdata.long$Country == 
    input$country)
    req(nrow(res) > 0)
    res
})

output$graph <- renderPlot({
    bar_one <- (ggplot(ay_res(), aes(Country, Count, fill = Age_Group)) + geom_bar(stat = "identity") + 
                    geom_text(aes(label=paste0(Count,"%", " ", "(", (round(Round_Total/1000000,1))," ", "Million", ")")), color="black", size=3.5, position = position_stack(vjust = 0.5))) +
        theme_classic() +
        scale_fill_manual(values = cbp2, labels = c("Young Adolescents (10-14)", "Older Adolescents (15-19)","Older Youth (20-24)"), name = "Age Group") + 
        theme(axis.line.y=element_blank(),
              axis.text.y=element_blank(),
              axis.title.y=element_blank(),
              axis.title.x = element_blank(),
              axis.ticks.y=element_blank(),
              axis.text.x =element_blank(),
              axis.ticks.x =element_blank(),
              axis.line.x =element_blank(),
              legend.position = "right")+
        coord_flip()+ scale_y_reverse() +
        labs(caption="Source: UN Population Division 2020", size=7) 
    
    vals$bar_one <- bar_one
    print(bar_one)
    
    
})
output$downloadGraph <- downloadHandler(
    filename = function() {
        paste("Adolescents and Youth", "png", sep = ".")
    }, 
    content = function(file) {
        png(file, width = 980, height = 400) 
        print(vals$bar_one)
        dev.off()
    })

#比较页面SelectizeInput代码

         fluidRow(
             column(6,
                    wellPanel(
                        selectizeInput("country",
                                       "Select Up to 4 Countries to Compare",
                                       choices = as.list(aypopdata.long$Country),
                                       multiple = TRUE,
                                       options = list(maxItems = 4))
                    )
             ),

#比较页面图代码

ay_res_compare <- reactive({
req(input$country)
res <- aypopdata.long %>% filter(aypopdata.long$Country %>% input$country) 
%>% group_by(Country) %>% summarise(Count = sum(Count))

})

output$graph12 <- renderPlot({
    bar_compare <- (ggplot(ay_res_compare(), aes(Country, Count, fill = Age_Group)) + geom_bar(stat = "identity") + 
                    geom_text(aes(label=paste0(Count,"%", " ", "(", (round(Round_Total/1000000,1))," ", "Million", ")")), color="black", size=3.5, position = position_stack(vjust = 0.5))) +
        theme_classic() +
        scale_fill_manual(values = cbp2, labels = c("Young Adolescents (10-14)", "Older Adolescents (15-19)","Older Youth (20-24)"), name = "Age Group") + 
        theme(axis.line.y=element_blank(),
              axis.text.y=element_blank(),
              axis.title.y=element_blank(),
              axis.title.x = element_blank(),
              axis.ticks.y=element_blank(),
              axis.text.x =element_blank(),
              axis.ticks.x =element_blank(),
              axis.line.x =element_blank(),
              legend.position = "right")+
        coord_flip()+ scale_y_reverse() +
        labs(caption="Source: UN Population Division 2020", size=7) 

vals$bar_compare <- bar_compare
print(bar_compare)

标签: rrenderreactive

解决方案


推荐阅读