前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[GBD数据库挖掘2] ggplot2优雅的展示发病率

[GBD数据库挖掘2] ggplot2优雅的展示发病率

作者头像
R语言数据分析指南
发布2022-09-23 14:17:36
4350
发布2022-09-23 14:17:36
举报
文章被收录于专栏:R语言数据分析指南

❝本节继续来进行GBD数据库的挖掘,小编在去年写过代码的基础上进行了更加精细的加工,各位观众老爷们细细品味,「数据代码已经上传VIP群,请自行下载」

加载R包

代码语言:javascript
复制
library(tidyverse)
library(ggh4x)
library(ggsci)
library(magrittr)
library(grid)

导入数据

代码语言:javascript
复制
df <- read_csv("liver_cancer.csv")

数据清洗

代码语言:javascript
复制
df1 <- df %>% select(measure,year,val,location,age,metric,sex) %>% 
  filter(location %in% c("Global","High SDI",
                         "High-middle SDI","Middle SDI",
                         "Low-middle SDI","Low SDI"),
         age == 'Age-standardized',metric=="Rate",measure !="Prevalence") %>%
  select(measure,val,location,year,sex) %>% 
  mutate(measure=case_when(measure=="DALYs (Disability-Adjusted Life Years)" ~ 
                             "DALYs per 100,000",
                           TRUE ~ as.character(measure))) %>% 
  mutate(across("measure",str_replace,"Deaths","ASDR per 100,000")) %>% 
  mutate(across("measure",str_replace,"Incidence","ASIR per 100,000")) %>% 
  mutate(val=as.numeric(val),year=as.numeric(year),sex=as.factor(sex)) %>% 
  unite(.,col="measure",sex,measure,sep="-",remove = T,na.rm = F)

定义因子

代码语言:javascript
复制
df1$measure <- factor(df1$measure,levels=c("Both-ASIR per 100,000","Both-ASDR per 100,000",
                                          "Both-DALYs per 100,000",
                                          "Female-ASIR per 100,000","Female-ASDR per 100,000",
                                          "Female-DALYs per 100,000",
                                          "Male-ASIR per 100,000","Male-ASDR per 100,000",
                                          "Male-DALYs per 100,000"))

定义分面背景

代码语言:javascript
复制
ridiculous_strips <- strip_themed(
  text_x = elem_list_text(colour=c("#9C8D58","#EDB749","#3CB2EC"),
                          face=c("bold","bold","bold"),size=c(10,10,10)),
  background_x = elem_list_rect(fill = c("#EEECE1","#FFF2E7","#E8F2FC")))

定义注释函数

代码语言:javascript
复制
annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data) 
{
  layer(data = data, stat = StatIdentity, position = PositionIdentity, 
        geom = ggplot2:::GeomCustomAnn,
        inherit.aes = TRUE, params = list(grob = grob, 
                                          xmin = xmin, xmax = xmax, 
                                          ymin = ymin, ymax = ymax))
}

数据可视化

代码语言:javascript
复制
df1 %>% ggplot(aes(year,val,color=location,fill=location))+
  geom_point(size=2,pch=21,color="white")+geom_line()+
  facet_wrap2(vars(measure),scales = "free_y",axes = "y",remove_labels = "x",strip = ridiculous_strips)+
  guides(color=guide_legend(title="location",nrow=1))+
  scale_color_jco()+
  scale_fill_jco()+
  coord_cartesian(clip="off")+
  theme_bw()+
  theme(panel.spacing.x = unit(0.1,"cm"),
        panel.spacing.y = unit(0.15,"cm"),
        axis.title = element_blank(),
        strip.background = element_rect(fill="grey80"),
        strip.text.x = element_text(size=9,color="black"),
        axis.text = element_text(color="black"),
        plot.margin=unit(c(0.2,0.5,0.2,0.2),units=,"cm"),
        legend.text=element_text(color="black",size=9,face="bold"),
        legend.key=element_blank(),  
        legend.title = element_blank(),
        legend.position = "top",
        legend.spacing.x=unit(0.1,'cm'),
        legend.key.width=unit(0.5,'cm'),
        legend.key.height=unit(0.5,'cm'), 
        legend.background=element_blank())+
  annotation_custom2(grob = rectGrob(gp = gpar(fill="grey90",col="black")),
                     data = df1 %>% filter(measure=="Both-ASDR per 100,000"),
                     xmin =1954.5,xmax =2055.5,ymin=18.5,ymax=20)

结果保存

代码语言:javascript
复制
ggsave("Figure-1.pdf",width=11.79,height=10.42,units="in",dpi=300)

数据获取

❝好的本文介绍到此介绍,相信看过相关文档的都很了解具体内容,小编也不过多阐述了;

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 R语言数据分析指南 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 加载R包
  • 导入数据
  • 数据清洗
  • 定义因子
  • 定义分面背景
  • 定义注释函数
  • 数据可视化
  • 结果保存
  • 数据获取
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档