分享

ggplot2笔记4 语法基础

 萌小芊 2018-03-28



ggplot2绘图基础:


碎碎念ing:终于结束了《ggplot2》的第一部分“Getting Started”,今天开始看第二部分——语法,第四章(Mastering the Grammar)介绍了ggplot2的一些基础语法知识,大概是对前期内容在理论上做一个总结。

建立一个散点图

首先还以“耗油量”数据集(mpg)为例。

  1. library(ggplot2)

  2. mpg

  3. # A tibble: 234 x 11

  4.   manufacturer model      displ  year   cyl trans      drv     cty   hwy fl    class

  5.                      

  6. 1 audi         a4          1.80  1999     4 auto(l5)   f        18    29 p     comp…

  7. 2 audi         a4          1.80  1999     4 manual(m5) f        21    29 p     comp…

  8. 3 audi         a4          2.00  2008     4 manual(m6) f        20    31 p     comp…

  9. 4 audi         a4          2.00  2008     4 auto(av)   f        21    30 p     comp…

  10. 5 audi         a4          2.80  1999     6 auto(l5)   f        16    26 p     comp…

  11. 6 audi         a4          2.80  1999     6 manual(m5) f        18    26 p     comp…

  12. 7 audi         a4          3.10  2008     6 auto(av)   f        18    27 p     comp…

  13. 8 audi         a4 quattro  1.80  1999     4 manual(m5) 4        18    26 p     comp…

  14. 9 audi         a4 quattro  1.80  1999     4 auto(l5)   4        16    25 p     comp…

  15. 10 audi         a4 quattro  2.00  2008     4 manual(m6) 4        20    28 p     comp…

  16. # ... with 224 more rows

我们想建立一个展示发动机排量(displ)和高速公路行驶记录每加仑行驶的英里数(hwy)关系的散点图,并用不同颜色标记汽缸变量(cyl)。代码如下:

  1. ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +

  2.  geom_point()

You can create plots like this easily, but what is going on underneath the surface? How does ggplot2 draw this plot?

1. 数据的美学映射(Mapping Aesthetics to Data)

散点图到底是啥?你以前见过很多,有可能也用手绘制过。散点图将每个观测值表示为一个点,根据两个变量的值进行定位。除了水平和垂直位置,每个点还具有大小,颜色和形状。这些属性被称为美学(aesthetics),是能在图形上感知的属性。每个美学可以映射到一个变量,或设置为一个常量值。在上图中, displ 映射到横轴上, hwy映射到纵轴上, cyl设置为彩色分类。大小和形状没有映射到变量,但保持其(恒定)默认值。

映射设定好之后,我们可以通过改变函数,画出除了散点图之外的折线图 geom_line()、柱形图 geom_bar()等:

  1. ## 折线图

  2. ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +

  3. geom_line() +

  4. theme(legend.position = 'none')

  5. ## 柱形图

  6. ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +

  7. geom_bar(stat = 'identity', position = 'identity', fill = NA) +

  8. theme(legend.position = 'none')

这些图虽然语法上是对的,但毫无意义。

点、线和条形都是不同的几何图形,他们组合起来可以有很多种plot:

Named plotGeomOthers
ScatterplotPoint---
BubblechartPointSize mapped to a variable
BarchartBar---
Box-and-whisker plotBoxplot---
Line chartLine---

有些图是根据实际需要添加的几何图形,他们没有确定的名称,如下:

  1. ggplot(mpg, aes(displ, hwy, colour = factor(cyl))) +

  2.  geom_point() +

  3.  geom_smooth(method = 'lm')

2. 标度(Scaling)

实际上,数据集中的数值对计算机没有任何意义。我们需要将它们从数据单位(例如,升,英里每加仑,气缸数量等)转换成计算机可以显示的图形单位(例如,坐标和颜色)。这种转换过程称为Scaling(scales)。

要生成一个完整的图表,需要三个主要元素:

  • data:数据

  • geom:代表数据的几何图形

  • scales and coordinate system:坐标系和标度

复杂化(Adding Complexity)

在普通的散点图中加线条和分面:

  1. ggplot(mpg, aes(displ, hwy)) +

  2.  geom_point() +

  3.  geom_smooth() +

  4.  facet_wrap(~year)

以上代码中增加了三个部分

  1. 分面(facets)

  2. 多个图层(multiple layers)

  3. 统计变换(statistics)

平滑图层 geom_smooth()与点图层 geom_point()不同,因为它不显示原始数据,而是显示数据的统计转换。所以,这个过程的形成是在将数据映射到美学之后,传递到统计转换(stat)加以处理。

图层语法的组成(Components of the Layered Grammar)

图表包括了数据、映射、统计变换、几何图形以及位置调整(position adjustment)。

图层 layers

图层由以下五个方面组成(具体见第五章):

  1. Data

  2. Aesthetic mappings.

  3. A statistical transformation (stat).

  4. A geometric object (geom).

  5. A position adjustment.

标度 Scales

标度控制的是从数据到图形属性的映射。下面是几种标度的例子:

(从左到右依次是连续型变量、离散型变量的形状和颜色标度)

(具体见第六章)

坐标系 Coordinate System

坐标系(简称coord)是指数据映射所在的图表平面,位置通常由两个坐标(x, y)决定。

坐标系的不同类型如下图:

(从左到右依次是笛卡尔坐标系、半对数坐标系、极坐标系)

分面 Facetting

(详见第七章)

= = = = = = = = 我是懵懵的分割线 = = = = = = = =

碎碎念2:这一章看下来感觉干货不多,貌似更像是本书中间的一个过渡章节(也可能是我没有get到,不是很懂作者的用意)。囧。最后还是要放上参考资料,镇楼。


参考资料:

  1. Hadley Wickham(2016). ggplot2. Springer International Publishing. doi: 10.1007/978-3-319-24277-4

  2. 《R语言应用系列丛书·ggplot2:数据分析与图形艺术》

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多