原创 小果 生信果
本篇将介绍如何利用ggplot2绘制柱状图以清楚地展示各肿瘤某一指标的比较(如肿瘤缓解率)和GSVA分析结果。
1 肿瘤缓解率结果展示
首先启动程序包
library(ggplot2)
然后读取数据
df<-read.csv("easy_input1.csv")
数据结构如下图,为2列。第一列为不同癌症,第二列为score
按照score排序,并画图:
df<-df[order(df$score,decreasing = T),]
df$index<-seq(1,nrow(df))
p<-ggplot(df,aes(x=index,y=score,fill=ID)) +
geom_bar(stat = 'identity',width = 0.8) +
scale_fill_brewer(type = "Qualitative", palette = "Paired") + #bar的颜色
scale_y_continuous(breaks=seq(-100, 100, 10), #y轴刻度
expand = c(0,0)) + #上下都不留空
scale_x_discrete(expand = expand_scale(mult = c(0.01,0))) + #左边留空,右边到头
#画3条横线
geom_hline(yintercept = c(-30,0,20),
linetype = 5, #画虚线
size = 0.3) + #线的粗细
#其他主题
labs(x = "", y = "Maximum Change in Tumor Size (%)",
title = "A Maximum Change in Tumor Size, According to Tumor Type") +
theme_bw() + #去除背景色
theme(panel.grid =element_blank()) + #去除网格线
theme(panel.border = element_blank()) + #去除外层边框
theme(axis.line = element_line(colour = "black")) + #沿坐标轴显示直线
theme(axis.line.x = element_blank(), axis.ticks.x = element_blank(), axis.text.x = element_blank()) + #去除x轴
#图例
guides(fill = guide_legend(ncol = 5,title = NULL)) + #图例分5列
scale_size(range=c(5,20)) +
theme(legend.background = element_blank(), #移除整体边框
#图例的左下角置于绘图区域的左下角
legend.position=c(0,0),legend.justification = c(0,0))
#改用下面这行,图例就会位于顶部
#legend.position="top")
由于Cancer12值很高,使得图片右侧很空。对其进行修改,让y轴适合大部分数据,然后在最高的那个bar上标出实际数据。
#设置坐标轴范围,最大值设为50,以适应大多数数据
P <- p + coord_cartesian(ylim = c(-90,50)) + #y轴范围,根据实际情况调整
#添加数据标签
geom_text(data = subset(df, score > 50),
aes(index, 48,label=round(score))) + #在超过50的bar上标出实际数据
geom_text(data = subset(df, index == 3),
aes(index, score + 1,label = "*")) + #作者的特殊标记
geom_text(data = subset(df, index == nrow(df)),
aes(index, score - 3, label = "T")) #作者的特殊标记
2 GSVA结果展示
2.1 score绝对值小于阈值的bar显示为灰色
输入数据,包含两列:ID和score
df<-read.csv("easy_input2.csv")
按照score的值分组
df$group<-cut(df$score, breaks = c(-Inf,-4,4,Inf),labels = c(1,2,3))
按照score排序
df<-df[order(df$score,decreasing = F),]
df$index<-seq(1,nrow(df))
开始画图:
ggplot(df,aes(x=index,y=score,fill=group)) +
geom_bar(stat = 'identity',width = 0.8) +
scale_fill_manual(values = c("palegreen3","snow3","dodgerblue4")) + #bar的颜色
scale_x_discrete(expand = expand_scale(add = .6)) +
scale_y_continuous(breaks=seq(-30, 20, 5)) +
coord_flip() + #坐标轴互换
#画2条横线
geom_hline(yintercept = c(-4,4),
color="white",
linetype = 2,#画虚线
size = 0.3) + #线的粗细
#写label
geom_text(data = subset(df, score > 0),
aes(x=index, y=0, label=paste0(ID," "), color = group),#bar跟坐标轴间留出间隙
size = 3, #字的大小
hjust = "inward" ) + #字的对齐方式
geom_text(data = subset(df, score < 0),
aes(x=index, y=0, label=paste0(" ",ID), color = group),
size = 3, hjust = "outward") +
scale_colour_manual(values = c("black","snow3","black")) +
#其他主题
labs(x = "", y = "t value of GSVA score, tumor
versus non-malignant",
title = "Endothelial cells, tumour versus non-malignant") +
theme_bw() + #去除背景色
theme(panel.grid =element_blank()) + #去除网格线
theme(panel.border = element_rect(size = 0.6)) + #边框粗细
theme(axis.line.y = element_blank(), axis.ticks.y = element_blank(), axis.text.y = element_blank()) + #去除y轴
guides(fill=FALSE,color=FALSE) #不显示图例
2.2 pvalue>0.05的bar显示为灰色
输入数据,包含三列,ID、score和pvalue
df<-read.csv("easy_input3.csv")
#按照pvalue分组
df$p.group<-cut(df$pval, breaks = c(-Inf,0.05,Inf),labels = c(1,0))
#按照score分组
df$s.group<-cut(df$score, breaks = c(-Inf,0,Inf),labels = c(0,1))
#合并
df$ps.group <- paste0(df$p.group,df$s.group)
#根据pvalue和score分为3组
df$group<-ifelse(df$ps.group=='10','1',ifelse(df$ps.group=='11','2','3'))
按照score排序
df<-df[order(df$score,decreasing = F),]
df$index<-seq(1,nrow(df))
开始画图:
只调整了颜色顺序,其余跟“2.1”的画图代码是一样的
scale_fill_manual(values = c("palegreen3","dodgerblue4","snow3")) + #颜色
推荐阅读
页面更新:2024-04-23
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号