创建一个闪亮的条形图,每次选择一个区域时都会显示一个新图

肖恩

我正在尝试创建一个交互式条形图,但是我在闪亮方面的技能并不是最好的。我试图让一些东西工作,但我很挣扎 - 服务器部分开始让我有点困惑。

以下是我试图包含在交互式图中的用户输入集:

  1. 一个选择输入,允许您选择一个区域,当进行此选择时,会出现一个全新的图(仅显示该特定区域的人口统计数据)。

  2. 一个滑块输入,允许您在一系列年龄组中滑动。例如 - 您可能只想选择从“0 到 10”和“40 到 44”的年龄范围。

下面我创建了一个可复制的示例,您可以复制和粘贴。请注意我的主数据集中的年龄范围不是等间隔的,也没有足够的数据来使每个位置都有完整的年龄范围。我所做的就是尝试创建我拥有的较大数据集的小版本。


library(dplyr)
library(ggplot2)
library(shiny)
library(shinyWidgets)

# creating example data

exampledata <- data.frame(City=c("London","Liverpool","Manchester",
                                        "Porstmouth","Liverpool","Leeds",
                                        "London","Manchester","Nottingham"),
                         Ageband = c("35 to 39","80+","40 to 44",
                                        "0 to 10","80+","35 to 39",
                                        "40 to 44","0 to 10", "80+"),
                         count = c(1200,800,520,
                                      300,105,630,
                                      410,150,700))


# Static Plot to show what I intend to make interactive

ggplot(exampledata, aes(fill=Ageband, y=count, x=Ageband)) + 
  geom_bar(position="dodge", stat="identity", show.legend = FALSE) +
  facet_wrap(~City)+
  labs(y="Participant count", x=" ",caption=(paste0("Participants sampled = ", sum(exampledata$count))))+
  scale_fill_manual(name="Age",values=c("#CEE0F1", "#C3DAEE" ,"#B3D3E8" ,"#A2CBE2", "#8FC1DD" ,"#79B6D9" ,"#66AAD4" ,"#559FCD" ,"#4493C6", "#3686C0", "#2878B9","#1C69AF" ,"#1257A1" ,"#084594", "#05337d"))+
  theme(plot.title = element_text(hjust = 0.5))+
  theme(axis.text.x = element_text(angle = 90))

# shiny attempt

ui <- fluidPage(
  titlePanel("Age Distribution of Research"),
  selectInput("location", "Select Location", choices = exampledata$City),
  sliderTextInput("age", "Select Age", choices = exampledata$Ageband)
  plotOutput("bar")
)

server <- function(input, output, session) {

# At this point would you need to create a reactive expression or not?
  
  data <- reactive({
    exampledata %>%
      filter(City == input$City)
  })

# At which point of the plots do the inputs need specifying? 
  
  output$plot <- renderPlot({
    
    ggplot(data, aes(aes(fill=Ageband, y=count, x=input$age)) + 
             geom_bar(position="dodge", stat="identity", show.legend = FALSE)
   })
}

}

shinyApp(ui = ui, server = server)



石灰

这是否接近你想要的?

library(dplyr)
library(ggplot2)
library(shiny)
library(shinyWidgets)

exampledata <- data.frame(City=c("London","Liverpool","Manchester",
                                 "Porstmouth","Liverpool","Leeds",
                                 "London","Manchester","Nottingham"),
                          Ageband = c("35 to 39","80+","40 to 44",
                                      "0 to 10","80+","35 to 39",
                                      "40 to 44","0 to 10", "80+"),
                          count = c(1200,800,520,
                                    300,105,630,
                                    410,150,700))

ui <- fluidPage(
  titlePanel("Age Distribution of Research"),
  selectInput("location", "Select Location", choices = exampledata$City, selected="London", multiple=TRUE),
  # Slider inputs work with numeric data, not categorical data
  selectInput("age", "Select Age", choices = exampledata$Ageband, selected="35 to 39", multiple=TRUE),
  plotOutput("plot")
)

server <- function(input, output, session) {
  data <- reactive({
    exampledata %>%
      filter(
        City %in% input$location,
        Ageband %in% input$age
      )
  })
  
  output$plot <- renderPlot({
    req(input$age, input$location)
    
    data() %>% 
      ggplot(aes(fill=City, y=count, x=Ageband)) + 
             geom_bar(position="dodge", stat="identity")
  })
}

shinyApp(ui = ui, server = server)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 R 中创建一个带有 2 个条形图的条形图

matplotlib 条形图显示一个额外的值

R ggplot 只显示一个条形图

如何创建一个双轴条形图?

D3.js条形图不会追加,创建一个新图

条形图不只显示一个x值的条形图

创建一个类别的条形图,每个类别仅填充一个

根据一个布尔列创建一个“堆叠”条形图

这个函数是否每次都会创建一个新的TensorFlow图?

创建一个堆叠条形图,其中包含 2 个条形的 3 个轨迹

闪亮的条形图:单击操作按钮时,第二个条形图总是出现延迟,而出现上一个图形

创建条形图时缺少第一个值

在一个条形图中创建多个条形图

css来模仿一个简单的条形图

每年一个闪避条形图

Seaborn 显示一个条形图而不是多个图

需要显示一个堆叠的样式条形图,分为左图和右图

如何创建一个直方图,其中每个条形图都是相应容器的热图?

创建一个特殊的径向条形图(赛道图)

如何从三个独立的数据源创建一个条形图?

从两个列表创建一个json以在Javascript中制作堆叠的条形图

修改第一个条形时,如何为条形图获取正确的图例?

条形图:将一个堆叠的条与一个躲避的条组合

在 ggplot 上将一个条形图叠加到另一个上

创建时间序列堆积条形图时出现问题,该条形图描绘了一个月内发生的事件次数

Pine-Script:从已知范围中选择一个[仅]条形图

Python:更新图而不是创建一个新图

JFreeChart / JasperReports-设置刻度标签仅在条形图的一个轴上显示

matplotlib没有在条形图的x轴上显示第一个标签