社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Git

文献配套GitHub发表级别绘图03-条形图

生信技能树 • 3 年前 • 553 次点击  


给学徒们收集整理了几套带GitHub源代码的文献图表合辑,让优秀者一点一滴拆解开来分享给大家。(全部的代码复制粘贴即可运行,欢迎尝试以及批评指正)

下面是去年实习生的分享
 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包提供三种配色方案

  1. 连续型Sequential, 颜色渐变。

  2. 极端型Diverging,生成深色强调两端、浅色表示中部的颜色,可用来标注数据中的离群点。

  3. 离散型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函数添加颜色

  1. 先选择色块:调用Spectral调色板,取11个颜色,赋值给cols
  2. 把cols赋值给colorRampPalette
  3. 添加到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. theme用法

相信大家也留意到上面设置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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/126520
 
553 次点击