本节继续根据2020-2021年美国疾病死亡率数据来绘制热图,后台回复关键词2021-4-6获取数据及代码,喜欢的小伙伴欢迎关注我的公众号R语言数据分析指南,持续分享更多优质资源
加载R包
pacman::p_load(tidyverse,extrafont,janitor,lubridate)
movi <- read.delim("movi.txt",header = T,sep="\t",
check.names = F) %>%
as_tibble() %>% mutate(fecha=as.Date.character(fecha))
数据可视化
ggplot(movi,
aes(x = fecha, y = linea_servicio, fill = afluencia)) +
geom_tile()+
geom_vline(xintercept = ymd("2020-03-23"), linetype="dashed",
color = "black", size=1.5)+
geom_vline(xintercept = ymd("2020-05-30"), linetype="dashed",
color = "black", size=1.5)+
annotate(geom = "text", x = ymd("2020-03-01"), y = 13.5,
label = "A",
hjust = 0, vjust=1, fontface="bold", color="#31a354",
size=6)+
annotate(geom = "text", x = ymd("2020-03-27"), y = 13.5,
label = "B",
hjust = 0, vjust=1, fontface="bold", color="#636363",
size=6)+
geom_vline(xintercept = ymd("2020-06-29"), linetype="dashed",
color = "black", size=1.5)+
annotate(geom = "text", x = ymd("2020-06-03"), y = 13.5,
label = "C",
hjust = 0, vjust=1, fontface="bold", color="red",
size=6)+
geom_vline(xintercept = ymd("2020-12-21"), linetype="dashed",
color = "black", size=1.5)+
annotate(geom = "text", x = ymd("2020-09-01"), y = 13.5,
label = "D",
hjust = 0, vjust=1, fontface="bold", color="#d95f0e",
size=5)+
annotate(geom = "text", x = ymd("2021-01-05"), y = 13.5,
label = "F",
hjust = 0, vjust=1, fontface="bold", color="red",
size=6)+
geom_vline(xintercept = ymd("2021-02-15"), linetype="dashed",
color = "black", size=1.5)+
annotate(geom = "text", x = ymd("2021-02-24"), y = 13.5,
label = "E",
hjust = 0, vjust=1, fontface="bold", color="#d95f0e",
size=6)+
theme_minimal()+
scale_x_date("",expand = c(0, 0),
date_labels = "%b %d %Y",date_breaks = "30 days")+
scale_fill_viridis_c()+
scale_y_discrete(position="right")+
labs(x=NULL,y=NULL)+
theme(axis.text.x = element_text(angle = 90,hjust=1,vjust=0.6),
plot.margin = unit(rep(0.5, 4), "cm"))+
theme(axis.text.x=element_text(family = "Times",colour = "black",size=10))+
theme(axis.text.y=element_text(family = "Times",
face = "italic",colour = "black",size=10))+
theme(legend.text=element_text(face="plain",family = "Times",
colour = "black",size = 10),
legend.title = element_blank())+
guides(fill = guide_colorbar(direction = "vertical",reverse = F,
barwidth = unit(.5, "cm"),
barheight = unit(10, "cm")))
可以看到我们通过geom_vline()函数添加线条与annotate()函数添加注释将热图分成了不同的部分,由于本人一直使用Linux系统进行ggplot2绘图,昨天系统崩溃了目前还在重新配置,在windows下为何图中会出现中文实在琢磨不清,请各位观众老爷慢慢品味,喜欢我的文档请多多支持,在此先行拜谢了!