rm(list=ls())
# 画图代码 
# setwd("D:\\李远存文件（千万别打开！！！）\\课题-GBD相关\\APC模型\\全球2型糖尿病与糖网趋势分析\\APC数据")  # 已注释：请按本地环境设置工作目录
library(tidyverse)
library(patchwork)

# 1.DM --------------------------------------------------------------------
mydata <- read.csv("糖尿病APC数据.csv",header = T)
str(mydata)
mydata$age2 <- factor(mydata$age2)
mydata$period2 <- factor(mydata$period2)
mydata$cohort2 <- factor(mydata$cohort2)
levels(mydata$age2)
levels(mydata$period2)
levels(mydata$cohort2)

#挑选数据
mydata <- mydata %>% 
  arrange(age,period,cohort) %>% 
  mutate(rate2=rate*100000)

#period on age
p1 <- mydata %>% 
  ggplot(aes(x=age2,y=rate2,group=period2,color=period2))+
  geom_line(size=1)+
  geom_point(shape=2)+
  guides(color=guide_legend(title = "period"))+
  scale_color_manual(values = heat.colors(3))+
  scale_shape_discrete(guide ="none")+
  xlab("")+
  ylab("Prevalence rate (per 100,000)")+
  labs(title = "Diabetes mellitus type 2")+
  theme_gray()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
        legend.position = c(1,0),
        legend.justification = c(1,0),
        legend.background = element_blank(),
        legend.key.size = unit(0.1,"inches"))
p1

#cohort on age
p2 <- mydata %>% 
  ggplot(aes(x=age2,y=rate2,group=cohort2,color=cohort2))+
  geom_line(size=1)+
  geom_point(shape=2)+
  guides(color=guide_legend(title = "cohort",ncol = 2))+
  scale_color_manual(values = heat.colors(10))+
  xlab("")+
  ylab("Prevalence rate (per 100,000)")+
  theme_gray()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
        legend.position = c(0,1),
        legend.justification = c(0,1),
        legend.background = element_blank(),
        legend.key.size = unit(0.1,"inches"))
p2

#period on cohort
p3 <- mydata %>% 
  ggplot(aes(x=cohort2,y=rate2,group=period2,color=period2))+
  geom_line(size=1)+
  geom_point(shape=2)+
  guides(color=guide_legend(title = "period",ncol = 1))+
  scale_color_manual(values = heat.colors(6))+
  xlab("")+
  ylab("Prevalence rate (per 100,000)")+
  theme_gray()+
  theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
        legend.position = c(1,1),
        legend.justification = c(1,1),
        legend.background = element_blank(),
        legend.key.size = unit(0.1,"inches"))
p3

  
  p1/p2/p3
  

# 2.DR--------------------------------------------------------------------
  mydata <- read.csv("糖网APC数据.csv",header = T)
  str(mydata)
  mydata$age2 <- factor(mydata$age2)
  mydata$period2 <- factor(mydata$period2)
  mydata$cohort2 <- factor(mydata$cohort2)
  levels(mydata$age2)
  levels(mydata$period2)
  levels(mydata$cohort2)
  #挑选数据
  mydata <- mydata %>% 
    arrange(age,period,cohort) %>% 
    mutate(rate3=rate2*100000)
  #period on age
  pa <- mydata %>% 
    ggplot(aes(x=age2,y=rate3,group=period2,color=period2))+
    geom_line(size=1)+
    geom_point(shape=2)+
    guides(color=guide_legend(title = "period"))+
    scale_color_manual(values = heat.colors(3))+
    scale_shape_discrete(guide ="none")+
    xlab("")+
    ylab("")+
    labs(title = "Diabetic retinopathy type 2")+
    theme_gray()+
    theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
          legend.position = c(1,0),
          legend.justification = c(1,0),
          legend.background = element_blank(),
          legend.key.size = unit(0.1,"inches"))
  pa
  
  #cohort on age
  pb <- mydata %>% 
    ggplot(aes(x=age2,y=rate3,group=cohort2,color=cohort2))+
    geom_line(size=1)+
    geom_point(shape=2)+
    guides(color=guide_legend(title = "cohort",ncol = 2))+
    scale_color_manual(values = heat.colors(10))+
    xlab("")+
    ylab("")+
    theme_gray()+
    theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
          legend.position = c(0,1),
          legend.justification = c(0,1),
          legend.background = element_blank(),
          legend.key.size = unit(0.1,"inches"))
  pb
  
  #period on cohort
  pc <- mydata %>% 
    ggplot(aes(x=cohort2,y=rate3,group=period2,color=period2))+
    geom_line(size=1)+
    geom_point(shape=2)+
    guides(color=guide_legend(title = "period",ncol = 1))+
    scale_color_manual(values = heat.colors(3))+
    xlab("")+
    ylab("")+
    theme_gray()+
    theme(axis.text.x = element_text(angle = 45,vjust = 0.5),
          legend.position = c(1,1),
          legend.justification = c(1,1),
          legend.background = element_blank(),
          legend.key.size = unit(0.1,"inches"))
  # pc
  
# (p1+pa)/(p2+pb)/(p3+pc)+ plot_annotation(tag_levels = "A")

  
  
