# ggplot2图层语法之课后习题（一）
# 2022-11-12

# 清空变量
rm(list=ls())
# 设定路径
# setwd("path/to/your/data")  # 已注释：请按本地环境设置工作目录
# 读取数据
df <- read.csv("global_HIV.csv",header=T)
# 查看数据
colnames(df)
unique(df$measure)
unique(df$sex)
unique(df$age)
unique(df$cause)
unique(df$metric)
# 加载包
library(tidyverse)
# 选择数据
temp <- df |>
  filter(measure=="Incidence") |>
  filter(sex%in%c("Male","Female")) |>
  filter(age=="All ages") |>
  filter(cause=="HIV/AIDS") |>
  filter(metric=="Number")
# 我的第一个图
fig1 <- ggplot(data = temp,aes(x=year,y=val,color=sex))+
  geom_line()

fig1
ggsave("我的第一个图.jpeg",height=8,width = 10,dpi=300)

# 课堂作业一 -------------------------------------------------------------------
# 1. 给 fig1 增加一个点图层，点为各个年份的发病人数，将每个点的大小设定为 1.2(size=1.2)，并将生成的图片对象赋值给 fig2。

fig2 <- fig1+geom_point(size=1.2)
fig2

# 2. 给 fig2 增加一个图层, 展示发病人数的 95%UI, 提示：geom_ribbon(aes(ymin=?,ymax=?)),
# 将 95%UI 的填充颜色按照性别区分（提示：将性别这个变量映射到填充颜
# 色中去，设置 fill=?）；并将填充颜色的透明度设置为 0.1（alpha=?）。最终
# 将生成的图片对象赋值给 fig3.
fig3 <- fig2+
  geom_ribbon(aes(ymin=lower,ymax=upper,fill=sex),
              alpha=0.1)
fig3

# 3. 去除 fig3 图片中 95%UI 的颜色边框（提示：设置 color=NA），并大胆猜测 fill 和 color 参数的作用范围。然后赋值给 fig4。

##  注意，下面代码会报错，因为geom_ribbon要求一定要有aes(ymin=,ymax=)
## fig4 <- fig3+geom_ribbon(color=NA) 
## 正确做法
fig4 <- fig2+
  geom_ribbon(aes(ymin=lower,ymax=upper,fill=sex),
              alpha=0.1,
              color=NA)
##fill是填充，作用于内部，color是边框的颜色。


# 4. 使用线图展示全球因艾滋病死亡人数的发展变化趋势，并用 ggsave 函数保存到工作路径中。
temp2 <- df |>
  filter(measure=="Deaths") |>
  filter(sex%in%c("Male","Female")) |>
  filter(age=="All ages") |>
  filter(cause=="HIV/AIDS") |>
  filter(metric=="Number")

ggplot(data = temp2,aes(x=year,y=val,color=sex))+
  geom_line()+
  geom_ribbon(aes(ymin=lower,ymax=upper,fill=sex),
              alpha=0.1,
              color=NA)

ggsave("HIV死亡.jpeg",width = 10,height = 8,dpi=300)


# 课堂作业二 -------------------------------------------------------------------
fig5 <- fig4 +
  labs(title = "Temporal trend of global incidence number of HIV",
       subtitle = "from 1990 to 2019",
       caption = "base on data from GBD 2019",
       tag="A")
fig5
#将 fig5 的图片标题，副标题字体大小均设定为 12，居中显示。

fig5+
  theme(plot.title = element_text(size=12,hjust=0.5),
        plot.subtitle = element_text(size=12,hjust = 0.5))

# 课堂作业三 -------------------------------------------------------------------
# 设定 fig5 的 y 轴，将它的限度设置在 0 到 2.5 million 之间，间隔为 0.5 million，尺度缩小 1 百万倍，同时 y 轴的标题变为 Incidence number(million)。
max(temp$val)
max(temp$upper)

fig5+
  scale_y_continuous(limits = c(0,2500000),
                     breaks = seq(from=0,to=2500000,by=500000),
                     labels = seq(from=0,to=2.5,by=0.5))+
  ylab("Incidence number(million)")


# 课后作业 --------------------------------------------------------------------
# 提取数据:DALY
colnames(df)
unique(df$measure)
temp3 <- df |>
  filter(measure=="DALYs (Disability-Adjusted Life Years)") |>
  filter(sex%in%c("Male","Female")) |>
  filter(age=="All ages") |>
  filter(cause=="HIV/AIDS") |>
  filter(metric=="Number")
# 看看最大值有多大
max(temp3$val)
max(temp3$upper) #72,576,395
# 绘图
pic <- ggplot(data = temp3,aes(x=year,y=val,color=sex))+
  geom_line()+
  geom_ribbon(aes(ymin=lower,ymax=upper,fill=sex),
              alpha=0.1,
              color=NA)+
  scale_x_continuous(limits = c(1990,2020),
                     breaks = seq(1990,2020,by=5),
                     labels = seq(1990,2020,by=5))+
  scale_y_continuous(limits = c(0,80000000),
                     breaks = seq(0,80000000,by=20000000),
                     labels = seq(0,80,by=20))+
  labs(title="Temporal trend of golobal DALY number",
       subtitle = "of HIV from 1990 to 2019",
       x="",
       y="DALY number (million)")+
  theme_bw()+
  theme(legend.position = c(1,0),
        legend.justification=c(1,0),
        legend.background  = element_blank(),
        axis.text.x = element_text(angle = 45,hjust = 1,vjust=1))


pic

# ggsave()  略





