---
title: "堆叠金字塔图的绘制示范"
author:
  - 李思齐
documentclass: ctexart
keywords:
  - 中文
  - R Markdown
output:
  rticles::ctex:
    fig_caption: yes
    number_sections: yes
    toc: yes
---

# 数据准备  

```{r warning=F,message=F}
library(tidyverse)
library(patchwork)
```


```{r}
# 读取并查看数据
mydata <- read.csv("data.csv",header = T)
mydata <- mydata %>% 
  arrange(year,sex,metric,cause)
unique(mydata$age)
unique(mydata$sex)
unique(mydata$cause)
unique(mydata$metric)
unique(mydata$location)
unique(mydata$measure)
unique(mydata$year)
```



```{r}  
# 将病因与年龄变量设置为factor
mydata$age <- factor(mydata$age,
                     levels = c("<5 years","5-9 years",
                                "10-14 years","15-19 years",
                                "20-24 years","25-29 years",
                                "30-34 years","35-39 years",
                                "40-44 years","45-49 years",
                                "50-54 years","55-59 years",
                                "60-64 years","65-69 years",
                                "70-74 years","75-79 years",
                                "80-84 years","85-89 years",
                                "90-94 years","95+ years"),
                     labels = c("<5 years","5-9 years",
                                "10-14 years","15-19 years",
                                "20-24 years","25-29 years",
                                "30-34 years","35-39 years",
                                "40-44 years","45-49 years",
                                "50-54 years","55-59 years",
                                "60-64 years","65-69 years",
                                "70-74 years","75-79 years",
                                "80-84 years","85-89 years",
                                "90-94 years","95+ years"))
mydata$cause <- factor(mydata$cause,
                       levels = unique(mydata$cause))
```



```{r}
# 分别准备number与rate数据
df_number <- mydata %>% 
  filter(metric=="Number")

df_rate <- mydata %>% 
  filter(metric=="Rate")
```

# 画图    

思路跟普通的金字塔图是一样的:    

第一步：先把女性数值设置为负值；  
第二步：颜色映射给病因;   
第三步：设定geom_bar的位置为"stack"（上下堆叠）
第四部：确定其他元素

```{r}
p1 <- df_number %>% 
  mutate(val=ifelse(sex=="Female",-val,val)) %>%  #女性设置为负值
  ggplot(aes(x=age,y=val,fill=cause))+
  geom_col(position = "stack")+  #位置为stack上下堆叠
  xlab('')+
  ylab('')+
  coord_flip()+ #倒转x,y轴
  #增加一个标签,标签的位置自己手动设置一下
  # geom_text(aes(x=1,y=-max(df_number$val),label='Female'))+
  # geom_text(aes(x=1,y=max(df_number$val),label='Male'))+
  #中间加条黄色线
  geom_line(y=0, colour="yellow")
  # 后面还可以设置下坐标呀什么的

p1


```

```{r}
p2 <- df_rate %>% 
  mutate(val=ifelse(sex=="Female",-val,val)) %>% 
  ggplot(aes(x=age,y=val,fill=cause))+
  geom_col()+  
  xlab('')+
  ylab('')+
  coord_flip()+ #倒转x,y轴
  #增加一个标签
  geom_text(aes(x=1,y=-max(df_rate$val),label='Female'))+
  geom_text(aes(x=1,y=max(df_rate$val),label='Male'))+
  #中间加条黄色线
  geom_line(y=0, colour="yellow")

p2
```


# 合并两张图 
```{r}
p1/p2+plot_annotation(tag_levels = "A")
```

# 也可以用填充  

```{r}
p1 <- df_number %>% 
  mutate(val=ifelse(sex=="Female",-val,val)) %>%  #女性设置为负值
  ggplot(aes(x=age,y=val,fill=cause))+
  geom_col(position = "fill")+  #位置为fill
  xlab('')+
  ylab('')+
  coord_flip()+ #倒转x,y轴
  #增加一个标签,标签的位置自己手动设置一下
  geom_text(aes(x=1,y=-0.5,label='Female'))+
  geom_text(aes(x=1,y=0.5,label='Male'))+
  #中间加条黄色线
  geom_line(y=0, colour="yellow")
  # 后面还可以设置下坐标呀什么的
p1
```




