
# setwd("C:\\Users\\35111\\Desktop\\群资料：GBD第三期\\专题3：ggplot2图层语法\\ggplot2与地图相关资料")  # 已注释：请按本地环境设置工作目录
library(tidyverse)
library(sf)

# 第一题 :展示19年男性胃癌ASIR的空间分布---------------------------------------------------------------------
rm(list=ls())
# 读取GBD数据
GBD <- read.csv("stomach cancer.csv",header = T)
location <- read.csv("location.csv",header = T)
GBD <- left_join(GBD,location,by="location")
colnames(GBD)
# 读取地图数据
map <- read_sf("世界国家/世界国家.shp")
colnames(map)
# 合并，筛选
df <- map |> 
  left_join(GBD,by=c("NAME"= "location3")) |>
  filter(year==1990) |> 
  filter(sex=="Male") |> 
  filter(age=="Age-standardized") |> 
  filter(measure=="Incidence")
# 画图
ggplot(df) + 
  geom_sf(aes(fill=val))+
  scale_fill_distiller(palette="Spectral")  

# 第二题:使用分面系统，同时展示1990年与2019年男性胃癌不同测量指标的年龄标准化率的全球分布---------------------------------------------------------------------
rm(list=ls())
# 读取GBD数据
GBD <- read.csv("stomach cancer.csv",header = T)
location <- read.csv("location.csv",header = T)
GBD <- left_join(GBD,location,by="location")
colnames(GBD)
unique(GBD$cause)
unique(GBD$measure)
# 读取地图数据
map <- read_sf("世界国家/世界国家.shp")
colnames(map)
# 合并，筛选
df <- map |> 
  left_join(GBD,by=c("NAME"= "location3")) |>
  filter(year%in%c(1990,2019)) |> 
  filter(sex=="Male") |> 
  filter(age=="Age-standardized") |> 
  mutate(year=factor(year)) |> 
  mutate(measure=factor(measure,
                        levels = c("Incidence","Prevalence","Deaths",
                                   "YLDs (Years Lived with Disability)",
                                   "YLLs (Years of Life Lost)","Deaths"),
                        labels = c("Incidence","Prevalence","Deaths",
                                   "YLDs (Years Lived with Disability)",
                                   "YLLs (Years of Life Lost)","Deaths"))) |> 
  na.omit()

# 画图
ggplot(df) + 
  geom_sf(aes(fill=val))+
  facet_grid(measure~year)+
  scale_fill_distiller(palette="Spectral")  



  
  
