You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求为R-Shiny全球恐怖主义数据库应用添加可视化绘图代码

R-Shiny 响应式绘图补充方案(全球恐怖主义数据集)

使用马里兰大学的Global Terrorism Database数据集(覆盖1970-2017年全球恐怖袭击信息),针对现有带数据表格的Shiny应用,以下是对应6项业务问题的响应式绘图代码,可实现数据表格与图表的联动(假设应用中已定义filtered_data()作为响应式过滤数据集):


1. 恐怖袭击最多的前5个国家

output$attack_count_country <- renderPlot({
  filtered_data() %>%
    group_by(country_txt) %>%
    summarise(attack_count = n()) %>%
    arrange(desc(attack_count)) %>%
    slice_head(n = 5) %>%
    ggplot(aes(x = reorder(country_txt, attack_count), y = attack_count)) +
    geom_bar(stat = "identity", fill = "#2c3e50") +
    coord_flip() +
    labs(title = "恐怖袭击最多的前5个国家",
         x = "国家",
         y = "袭击次数") +
    theme_minimal()
})

2. 恐怖袭击致死人数最多的前5个国家

output$fatalities_count_country <- renderPlot({
  filtered_data() %>%
    group_by(country_txt) %>%
    summarise(total_fatalities = sum(nkill, na.rm = TRUE)) %>%
    arrange(desc(total_fatalities)) %>%
    slice_head(n = 5) %>%
    ggplot(aes(x = reorder(country_txt, total_fatalities), y = total_fatalities)) +
    geom_bar(stat = "identity", fill = "#e74c3c") +
    coord_flip() +
    labs(title = "恐怖袭击致死人数最多的前5个国家",
         x = "国家",
         y = "总致死人数") +
    theme_minimal()
})

3. 发动恐怖袭击最多的前5个恐怖组织

output$attack_count_group <- renderPlot({
  filtered_data() %>%
    filter(gname != "Unknown") %>%
    group_by(gname) %>%
    summarise(attack_count = n()) %>%
    arrange(desc(attack_count)) %>%
    slice_head(n = 5) %>%
    ggplot(aes(x = reorder(gname, attack_count), y = attack_count)) +
    geom_bar(stat = "identity", fill = "#3498db") +
    coord_flip() +
    labs(title = "发动恐怖袭击最多的前5个组织",
         x = "恐怖组织",
         y = "袭击次数") +
    theme_minimal() +
    theme(axis.text.y = element_text(size = 8))
})

4. 造成致死人数最多的前5个恐怖组织

output$fatalities_count_group <- renderPlot({
  filtered_data() %>%
    filter(gname != "Unknown") %>%
    group_by(gname) %>%
    summarise(total_fatalities = sum(nkill, na.rm = TRUE)) %>%
    arrange(desc(total_fatalities)) %>%
    slice_head(n = 5) %>%
    ggplot(aes(x = reorder(gname, total_fatalities), y = total_fatalities)) +
    geom_bar(stat = "identity", fill = "#e67e22") +
    coord_flip() +
    labs(title = "造成致死人数最多的前5个组织",
         x = "恐怖组织",
         y = "总致死人数") +
    theme_minimal() +
    theme(axis.text.y = element_text(size = 8))
})

5. 最常见的恐怖袭击类型

output$attack_type <- renderPlot({
  filtered_data() %>%
    group_by(attacktype1_txt) %>%
    summarise(attack_count = n()) %>%
    arrange(desc(attack_count)) %>%
    ggplot(aes(x = reorder(attacktype1_txt, attack_count), y = attack_count)) +
    geom_bar(stat = "identity", fill = "#9b59b6") +
    coord_flip() +
    labs(title = "最常见的恐怖袭击类型",
         x = "袭击类型",
         y = "次数") +
    theme_minimal()
})

6. 最常见的恐怖袭击目标

output$target_type <- renderPlot({
  filtered_data() %>%
    group_by(target1) %>%
    summarise(attack_count = n()) %>%
    arrange(desc(attack_count)) %>%
    slice_head(n = 10) %>% # 取前10避免标签拥挤
    ggplot(aes(x = reorder(target1, attack_count), y = attack_count)) +
    geom_bar(stat = "identity", fill = "#1abc9c") +
    coord_flip() +
    labs(title = "最常见的恐怖袭击目标",
         x = "目标类型",
         y = "次数") +
    theme_minimal() +
    theme(axis.text.y = element_text(size = 7))
})

联动实现说明

所有绘图均依赖应用中已定义的filtered_data()响应式数据集,当数据表格的过滤条件(如日期范围、地区筛选等)更新时,filtered_data()会自动刷新,所有图表将同步更新,实现表格与图表的联动。

在UI部分,需为每个绘图添加对应的输出控件,例如:

tabPanel("分析图表",
         fluidRow(
           column(6, plotOutput("attack_count_country")),
           column(6, plotOutput("fatalities_count_country"))
         ),
         fluidRow(
           column(6, plotOutput("attack_count_group")),
           column(6, plotOutput("fatalities_count_group"))
         ),
         fluidRow(
           column(6, plotOutput("attack_type")),
           column(6, plotOutput("target_type"))
         )
)

内容的提问来源于stack exchange,提问作者Kaushal Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 01:25:20