author: "ylchen" 文章来源:"Preoperative immune landscape predisposes adverse outcomes in hepatocellular carcinoma patients with liver transplantation" (2021,npj Precision Oncology),数据与代码全部公开在https://github.com/sangho1130/KOR_HCC。
下面来实现Fig.2a的条形图(barplots)
一、数据载入
rm(list = ls()) library(reshape2) library(ggplot2) library(RColorBrewer) data # 变成因子,调整顺序 data$Status # 行为样本类型,列为细胞 head(data) # ggplot 画图需要 宽数据变成 长数据 melt.data head(melt.data)
基础R包---reshape2包
melt-把宽格式数据转化成长格式。
cast-把长格式数据转化成宽格式。(dcast-输出时返回一个数据框。acast-输出时返回一个向量/矩阵/数组。)
cast 函数的作用除了还原数据外,还可以对数据进行整合。
dcast 输出数据框。公式的左边每个变量都会作为结果中的一列,而右边的变量被当成因子类型,每个水平都会在结果中产生一列。
reshape2 (另外,tidyr包中gather和spread函数也能实现功能哦!)
二、条形图(相对比例)# 8个样品组的 22种免疫细胞比例 p y = Relative, # 设置y轴 fill = Cell))+ # 设置图形填充变量 geom_bar(stat="identity") + #stat="count"表示条形的高度是y变量的数量 #stat="identity"表示条形的高度是y变量的值 scale_fill_manual(values = colorRampPalette(brewer.pal(11, "Spectral"))(22)) + # 设置填充颜色 theme_bw() + # 空白背景 theme(axis.text = element_text(colour = 'black'), #刻度值 axis.text.x = element_text(angle = 90, hjust = 1), # x轴刻度值 panel.grid = element_blank()) + # 空白网格线 labs(x = '', y = 'Relative fraction') # 设置xy轴标签 p #ggsave('../results/Figure 2A input relative score.pdf', p)
两个知识点 上面画图函数涉及到了颜色与theme的设置,接下来介绍下这两个知识点。
1. 详解RColorBrewer包
ggplot2画图时会自带配色设置,但一般比较难看。当想使用一些高级,现有的颜色搭配时,不妨考虑下RColorBrewer包。
library(RColorBrewer) display.brewer.all() # 查看所有颜色 # brewer.pal.info # 返回画板名,最大颜色数,调色板类型,是否对色盲友好
RColorBrewer包提供三种配色方案
极端型Diverging,生成深色强调两端、浅色表示中部的颜色,可用来标注数据中的离群点。
离散型Qualitative,生成彼此差异明显的颜色,通常用来标记分类数据。
说明书:https://cran.r-project.org/web/packages/RColorBrewer/RColorBrewer.pdf
在线版:https://colorbrewer2.org/#type=sequential&scheme=BuGn&n=3
display函数显示颜色# 单独查看分类 display.brewer.all(type = "seq") # 单独查看第1类颜色 display.brewer.all(type = "qual") display.brewer.all(type = "div")
# 选择颜色块及数量 cols # name: the name of the color palette you want to use # n: the number of colors you want from the palette (integer) cols # 可视化 display.brewer.pal(n=3, name="BuGn")
加入到ggplot2主要通过scale_fill_manual
函数添加颜色
先选择色块:调用Spectral调色板,取11个颜色,赋值给cols 添加到ggplot2:scale_fill_manual # 过程如下:先选择色块 # 调用Spectral调色板,取11个颜色,赋值给cols cols # 可视化 display.brewer.pal(11, "Spectral") # 把cols赋值给colorRampPalette pal image(volcano, col=pal(22)) # 数据集volcano,颜色设置为:Spectral调色板选择11个颜色,在这11个颜色之间进行连续取值(共22个颜色) # 若添加到ggplot2 # scale_fill_manual(values = colorRampPalette(brewer.pal(11, "Spectral"))(22))
2. th eme用法 相信大家也留意到上面设置theme时使用了几个函数,但实际上还有很多内容可以调整,下面这个图简直是宝藏!
theme(axis.text = element_text(colour = 'black'), # 刻度值 axis.text.x = element_text(angle = 90, hjust = 1), # x轴刻度值 panel.grid = element_blank()) # 空白网格线
参考:https://ggplot2.tidyverse.org/reference/theme.html
三、条形图(相对比例)data head(data) data$Status melt.data head(melt.data) p geom_bar(stat="identity") + scale_fill_manual(values = colorRampPalette(brewer.pal(11, "Spectral"))(22)) + theme_bw() + theme(axis.text = element_text(colour = 'black'), axis.text.x = element_text(angle = 90, hjust = 1), panel.grid = element_blank()) + labs(x = '', y = 'Absolute fraction') p #ggsave('../results/Figure 2A input absolute score.pdf', p)