R语言之ggplot

Python020

R语言之ggplot,第1张

ggplot2的核心理念是将绘图与数据分离,数据相关的绘图与数据无关的绘图分离。按图层作图,保有命令式作图的调整函数,使其更具灵活性,并将常见的统计变换融入到了绘图中。

ggplot的绘图有以下几个特点:第一,有明确的起始(以ggplot函数开始)与终止(一句语句一幅图);其二,图层之间的叠加是靠“+”号实现的,越后面其图层越高。

ggplot2里的所有函数可以分为以下几类:

一个图形对象就是一个包含数据,映射,图层,标度,坐标和分面的列表,外加组件options

ggplot(数据, 映射) geom_xxx(映射, 数据) stat_xxx(映射, 数据)

点(point, text):往往只有x、y指定位置,有shape但没有fill

线(line,vline,abline,hline,stat_function等):一般是基于函数来处理位置

射(segment):特征是指定位置有xend和yend,表示射线方向

面(tile, rect):这类一般有xmax,xmin,ymax,ymin指定位置

棒(boxplot,bin,bar,histogram):往往是二维或一维变量,具有width属性

带(ribbon,smooth):透明是特征是透明的fill

补:包括rug图,误差棒(errorbar,errorbarh)

然后,就是按照你的需要一步步加图层了(使用“+”)。

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:

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",不显示