#流行曲线
library(ggplot2)
library(gtable)
library(grid)
library(ggthemes)
#生成作图时间序列
date_seq <- seq(as.Date('2020/01/22'), by = "3 days",length.out = 16)
date_seq<-format(date_seq,format="%m/%d")
date <- c()
for(i in 1:(length(date_seq)-1)){
kongge <- rep(' ',2)
date_new <- c(date_seq[i],kongge)
date <- c(date,date_new)
}
date <- c(date,date_seq[16])
date<-c(date,"")
mytheme <- theme_bw() +
theme(plot.margin = unit(c(1, 1, 2, 1), "lines"),
panel.grid.major=element_blank(), #去掉网格线中的竖线
panel.grid.minor=element_blank(), #去掉网格线中的横线
panel.border=element_rect(color="white"),
axis.title.x = element_blank(),
axis.line=element_line(color="black",size=0.65),
legend.justification=c(1,1), legend.position=c(0.9,0.95))
type<-read.xlsx("安徽省汇总数据.xlsx",sheet=5,detectDates = T)
#调整数据结构
mytype <- melt(type,id.vars="日期",variable.name="类型",value.name="新增病例数")
mytype$日期<-format(mytype$日期,format="%m/%d")
p2<-ggplot(mytype,aes(日期,新增病例数,fill=类型))+geom_bar(stat="identity",position="stack",aes(fill=类型),width = 0.65)+
theme(axis.ticks.length=unit(0.1,'cm'))+ #调整刻度线长度
guides(fill=guide_legend(title=NULL))+ #去掉图例中的标题
scale_fill_manual(values = c("#8C2D04","#4292C6","#D9D9D9"))+
scale_x_discrete(labels=date)+
coord_cartesian(ylim = c(0, 80), expand = FALSE, clip = "off")+
mytheme+
annotate("segment",
x = 35.25, xend = 36.65,
y = 54, yend = 54,
colour = "#66CC66", linetype="solid",size=1)+
annotate("text", x = 41.1, y = 54.5,
label = "有湖北省暴露史的输入病例",
size=3)+
annotate("segment",
x = 1, xend = 1,
y = 70, yend = 3.7,
colour = "#8C2D04", arrow=arrow(length=unit(0.2,"cm")),linetype=1,size=0.5)+
annotate("segment",
x = 1, xend = 2.5,
y = 70, yend = 70,
colour = "#8C2D04", linetype=1,size=0.5)+
annotate("text", x = 5, y = 70,
label = "安徽省通报首例",
size=3)+
annotate("text", x = 5, y = 65,
label = "确诊病例",
size=3)+
annotate("segment",
x = 3, xend = 3,
y = 59, yend = 24.5,
colour = "#8C2D04", arrow=arrow(length=unit(0.2,"cm")),linetype=1,size=0.5)+
annotate("segment",
x = 3, xend = 3.55,
y = 59, yend = 59,
colour = "#8C2D04", linetype=1,size=0.5)+
annotate("text", x = 5.5, y = 59,
label = "安徽省启动",
size=3)+
annotate("text", x = 5.5, y = 54,
label = "重大公共卫生",
size=3)+
annotate("text", x = 5.5, y = 49,
label = "事件一级响应",
size=3)+
annotate("segment",
x = 8, xend = 8,
y = 74, yend = 48.5,
colour = "#8C2D04", arrow=arrow(length=unit(0.2,"cm")),linetype=1,size=0.5)+
annotate("segment",
x = 8, xend = 8.8,
y = 74, yend = 74,
colour = "#8C2D04", linetype=1,size=0.5)+
annotate("text", x = 10.7, y = 74,
label = "延迟复工开学",
size=3)+
annotate("segment",
x = 17, xend = 17,
y = 74, yend = 68.5,
colour = "#8C2D04", arrow=arrow(length=unit(0.2,"cm")),linetype=1,size=0.5)+
annotate("segment",
x = 17, xend = 17.55,
y = 74, yend = 74,
colour = "#8C2D04", linetype=1,size=0.5)+
annotate("text", x = 23.25, y = 74,
label = "全省16个市对所有村组小区实行封闭管理",
size=3)+
annotate("text", x = 4, y = -6,
label = "第一阶段",
size=3)+
annotate("text", x = 13, y = -6,
label = "第二阶段",
size=3)+
annotate("text", x = 33, y = -6,
label = "第三阶段",
size=3)+
annotate("text", x = 4, y = -10,
label = "(1月22日-1月29日)",
size=3)+
annotate("text", x = 13, y = -10,
label = "(1月30日-2月7日)",
size=3)+
annotate("text", x = 33, y = -10,
label = "(2月8日-3月8日)",
size=3)+
annotate("segment",
x = 1, xend = 1,
y = -4.3, yend = -12,
linetype="solid",size=0.5)+
annotate("segment",
x = 8, xend = 8,
y = -4.3, yend = -12,
linetype="solid",size=0.5)+
annotate("segment",
x = 17, xend = 17,
y = -4.3, yend = -12,
linetype="solid",size=0.5)+
annotate("segment",
x = 47, xend = 47,
y = -4.3, yend = -12,
linetype="solid",size=0.5)+
annotate("segment",
x = 47.3, xend = 47.3,
y = 0, yend = 80.2,
linetype=1,size=0.75,col="black")+
theme(axis.ticks.length=unit(0.1,'cm'))
type<-read.xlsx("安徽省汇总数据.xlsx",sheet=6,detectDates = T)
type$日期<-format(type$日期,format="%m/%d")
![c677a400115c70484355f03b51333e61.png](https://i-blog.csdnimg.cn/blog_migrate/a16d5728aad02c901b189e5c12d8f881.jpeg)