其他
ggplot2优雅的绘制森林图
欢迎关注R语言数据分析指南
❝本节来介绍如何使用ggplot2来绘制森林图,下面通过一个小例子来进行展示
❞
❝近期系统整理了一下公众号所写过的一下经典文档,如果需要获取之前绘图文档数据的朋友欢迎加入小编的VIP交流群,「99元即可获取上百篇文档」的代码,公众号右下角添加微信咨询即可
❞
加载R包
library(tidyverse)
导入数据
unicox <- read_csv("AKT3_mRNA_OS_pancan_unicox.csv")
绘制森林图
p1 <- ggplot(unicox,aes(HR_log, cancer, col=Type))+
geom_point(aes(size=-log10(p.value)))+
geom_errorbarh(aes(xmax =upper_95_log, xmin = lower_95_log), height = 0.4)+
scale_x_continuous(limits= c(-2, 2), breaks= seq(-1, 1, 1))+
geom_vline(aes(xintercept = 0))+
xlab('HR(95%CI)') + ylab(' ')+
theme_bw(base_size = 12)+
theme(axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.x=element_text(color="black"),
axis.title.y=element_blank(),
plot.margin = unit(rep(0,4),"cm"),
legend.title = element_blank(),
legend.key=element_blank(), # 图例键为空
legend.text = element_text(color="black",size=9), # 定义图例文本
legend.spacing.x=unit(0.1,'cm'), # 定义文本书平距离
legend.spacing.y=unit(0.1,'cm'),
legend.key.width=unit(0.2,'cm'), # 定义图例水平大小
legend.key.height=unit(0.2,'cm'), # 定义图例垂直大小
legend.background=element_blank(), # 设置背景为空
legend.box.background=element_rect(colour="black"), # 图例绘制边框
legend.position=c(1,0),legend.justification=c(1,0))+
scale_color_manual(values = c("gray", "steelblue", "red"))
绘制基因名称
p2 <- unicox %>% select(cancer) %>% distinct() %>% mutate(group="A") %>%
ggplot(aes(group,cancer))+
geom_text(aes(group,cancer,label=cancer ),size=3,color="black") +
geom_stripped_rows()+
theme(panel.grid.major = element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(rep(0,4),"cm"))+
labs(x="Gene")+
scale_x_discrete(position = "top")
展示置信区间
p3 <- unicox %>% select(6,7,8) %>% mutate(across(where(is.numeric),~round(.,2))) %>%
mutate(`HR(95%Cl)`=paste(HR_log,"[",lower_95_log," to ",upper_95_log,"]",sep="")) %>% select(4) %>%
mutate(group="HR(95%Cl)") %>%
ggplot(aes(group,`HR(95%Cl)`))+
geom_text(aes(group,`HR(95%Cl)`,label=`HR(95%Cl)`),size=3,color="black") +
geom_stripped_rows()+
theme(panel.grid.major = element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(rep(0,4),"cm"))+
labs(x="HR(95%Cl)")+
scale_x_discrete(position = "top")
展示pvalue
p4 <- unicox %>% select(p.value) %>%
mutate(across(where(is.numeric),~round(.,2))) %>%
mutate(group="A",p.value=as.character(p.value)) %>%
ggplot(aes(group,p.value))+
geom_text(aes(group,p.value,label=p.value),size=3,color="black") +
geom_stripped_rows()+
theme(panel.grid.major = element_blank(),
axis.text=element_blank(),
axis.ticks = element_blank(),
axis.title.y=element_blank(),
plot.margin = unit(rep(0,4),"cm"))+
labs(x="p.value")+
scale_x_discrete(position = "top")
拼图
library(patchwork)
(p2+p3+p4+p1)+plot_layout(widths = c(.12,0.28,.15,.8))
❝本节介绍到此结束,喜欢的观众老爷们欢迎点赞转发,如果需要获取之前绘图文档数据及代码的欢迎加入小编的
❞2022年VIP交流群,付费99元
,群内会同步上传公众号文档代码;目前已上传2021-2022约150篇文档代码
;添加小编微信时,请备注单位-方向-姓名以及来意以便高效处理
免费交流群
欢迎大家扫描下方二维码加入「QQ交流群」,与全国各地上千位小伙伴交流