创建方法时自动添加注释_ggplot画图时添加箭头和注释的例子

该博客展示了如何使用ggplot2库在图表中添加箭头和文字注释,以解释流行病数据的时间序列变化。通过加载必要的库,创建日期序列,并对数据进行处理,博主构建了一个展示安徽省新增病例数的柱状图。图表包含多个注释,说明了关键事件,如首例病例通报、不同阶段的公共卫生响应和封闭管理措施等。
摘要由CSDN通过智能技术生成

#流行曲线

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
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值