R语言绘图(ggplot2、ggpubr)从入门到精通04--柱状图美化之调色

Python010

R语言绘图(ggplot2、ggpubr)从入门到精通04--柱状图美化之调色,第1张

本系列课程要求大家有一定的R语言基础,对于完全零基础的同学,建议去听一下师兄的《生信必备技巧之——R语言基础教程》。本课程将从最基本的绘图开始讲解,深入浅出的带大家理解和运用强大而灵活的ggplot2包。内容包括如何利用ggplot2绘制散点图、线图、柱状图、添加注解、修改坐标轴和图例等。

本次课程所用的配套书籍是: 《R Graphic Cookbooks》

除了以上的基本图形外,师兄还会给大家讲解箱线图、提琴图、热图、火山图、气泡图、桑基图、PCA图等各种常用的生信图形的绘制,还不赶紧加入收藏夹,跟着师兄慢慢学起来吧!

柱状图可能是最常用的一种数据可视化。它们通常用于显示数值(在y轴上),用于显示不同类别的数值(在x轴上)。例如,柱状图可以用来显示四种不同商品的价格。柱状图通常不适合显示一段时间内的价格,因为时间是一个连续的变量。

在制作柱状图时,您应该注意一个重要的区别:柱状图的高度有时表示数据集中的案例数,有时表示数据集中的值。记住这一区别——这可能会引起混淆,因为它们与数据的关系非常不同,但两者使用相同的术语。

拓展: position参数: 此处的position主要是指对图像的微调,最常见的应用是在分组的柱形图(bar)中,因为分组的柱形图会产生组内堆积和不堆积两种主要效果。

R 有几种用于制作图形的系统,但 ggplot2 是最优雅和最通用的系统之一。与大多数其他图形包不同,ggplot2 具有基于图形语法的底层语法,它允许您通过组合独立组件来组合图形。如果想要更加了解ggplot2,请阅读 ggplot2: Elegant Graphics for Data Analysis ,可以从 https://ggplot2-book.org/getting-started.html 学习本书

所有的图都由数据data,想要可视化的信息,映射mapping(即数据变量如何映射到美学属性的描述)组成

1. 图层(layers) 是几何元素和统计变换的集合。几何对象,简称 geoms ,代表你在图中实际看到的东西:点、线、多边形等等。 统计转换,简称 stats ,总结数据:例如,装箱和计数观察,以创建一个直方图,或拟合一个线性模型。

2. Scales 将数据空间中的值映射到美学空间中的值。这包括颜色、形状和大小的使用。Scale还绘制图例和轴,这使得从图中读取原始数据值成为可能(反向映射)。

3. 坐标(coords) 或坐标系统描述如何将数据坐标映射到图形的平面。它还提供了轴和网格线来帮助读取图形。我们通常使用笛卡尔坐标系,但也可以使用其他一些坐标系,包括极坐标和地图投影。

4. 刻面(facet) 指定如何拆分数据子集并将其显示为小倍数。这也被称为条件反射或网格/格子。

5. theme 控制更精细的显示点,如字体大小和背景颜色。

ggplot2有许多参数,可根据需求自行选取,具体参数详情可见 https://ggplot2.tidyverse.org/reference/index.html

基础绘图:由 ggplot(data,aes(x,y))+geom_ 开始,至少包含这三个组件,可以通过"+"不断的添加layers, scales, coords和facets。

Geoms :几何对象,通常,您将使用geom_函数创建层,以下为常用的图形:

geom_bar() :直方图,条形图

geom_boxplot() :box图

geom_density() :平滑密度估计曲线

geom_dotplot() :点图

geom_point() :点图

geom_violin() :小提琴图

aes(),颜色、大小、形状和其他审美属性

要向绘图添加其他变量,我们可以使用其他美学,如颜色、形状和大小。

按照属性定义

它们的工作方式与 x 和 y 相同,aes():

aes(displ, hwy, colour = class) #按照某个属性着色

aes(displ, hwy, shape = drv) #按照某个属性定义

aes(displ, hwy, size = cyl) #按照某个属性定义

整体自定义

geom_xxx(colour =自定义颜色)

geom_xxx(shape=形状编号)

geom_xxx(size =编号大小定义 0-10)

注意根据需求按照aes()还是geom进行添加属性

以下为R语言中各shape形状编号

scale控制如何将数据值转换为视觉属性的细节。

labs()和lims() 是对标签和限制进行最常见调整。

labs() ,主要对图形进行调整,注释等

labs()括号内参数:title主标题,subtitle副标题,caption右下角描述,tag左上角

xlab() ,x轴命名

ylab() ,y轴命名

ggtitle() ,标题

lims()

xlim() , xlim(a,b) 限制坐标(a,b)

ylim() , ylim(a,b) 限制坐标(a,b)

scale_alpha() 透明度尺度

scale_shape() , 搭配aes(shape=某个属性)使用

参数:name ,solid =T/F是否填充

scale_size()搭配aes(size=某个属性)使用

参数:name,range =c(0, 10)

1.适用于发散和定性的数据

a. scale_colour_brewer() ,scale_colour_brewer(palette =" "),scale_colour_brewer(palette ="Green ")

palette来自RcolorBrewer包,所有面板:

b. scale_colour_manual()

scale_colour_manual(values=c( )) 可以 自定义颜色 ,常用的参数

values可直接定义颜色,但是建议使用命名向量,例如

values=c("8" = "red", "4" = "blue", "6" = "darkgreen", "10" = "orange")

PS:注意在aes(colour=factor()),一定要把因素转换为factor型,否则无效

2.适用于连续的值,渐变颜色

a. scale_colour_gradient()

scale_colour_gradient (low =" ",high=" "),根据值大小定义颜色,创建两个颜色梯度(低-高),

b. scale_colour_gradient2()

scale_colour_gradient2(low = " ",mid = " ",high = " ")创建一个发散的颜色梯度(低-中-高)

c. scale_colour_gradientn()

创建一个n色渐变,scale_colour_gradientn(colours =许多R语言中的颜色面板),

默认坐标系是笛卡尔 coord_cartesian()

一般不会修改

facet_grid() ,在网格中布置面板

facet_grid(rows = vars() ) cols或rows = vars(因素),图形按列或行分割

facet_wrap()

facet_wrap(vars( ), ncol =n) , ncol或者nrow,分为多少行多少列

theme_bw() ,可以覆盖所有主题,背景变为白色,我们在文章中所用的图片大都需要该背景。

或者用 theme_classic() ,同时去除了网格线

theme() ,修改主题的组件,里面涉及多个参数,根据需求调整

常见参数:

legend.position,图例的位置,包括 "left" 左, "right" 右, "bottom" 下, "top" 上和"none",不显示

ggplot2是R语言第三方可视化扩展包,在某种程度上它基本代替了R可视化。该包是RStudio首席科学家Hadley Wickham读博期间的作品,它强大的画图逻辑使得它称为R最流行的包之一。更多知识分享请到 https://zouhua.top/

ggplot2 is based on the grammar of graphics, the idea that you can build every graph from the same few components: a data set, a set of geoms—visual marks that represent data points, and a coordinate system。

To display data values, map variables in the data set to aesthetic properties of the geom like size, color, and x and y locations

aesthetic map variables in data to graphic properties. mappings control the relationship between data and graphic properties.

Aesthetic mapping means "something you can see"

Each type of geom accepts only a subset of all aesthetics-refer to the geom help pages to see what mappings each geom accepts. Aesthetic mappings are set with the aes() function.

scales map values in the data space to values in the aesthetic space(color, size, shape ...). scales are reported on the plot using axes and legends. Control aesthetic mapping.

Scales are modified with a series of functions using a scale_<aesthetic>_<type>naming scheme

The following arguments are common to most scales in ggplot2:

geometric objects are the actual marks we put on a plot

A plot must have at least one geometric object, and there is no upper limit. adding a geom by using the + operator.

It's often useful to transform your data before plotting, and that's what statistical transformations do.

Every geom function has a default statistic:

The ggplot2 theme system handles non-data plot elements such as

Built-in themes include: