美文网首页
2024-02-13 ggplot2

2024-02-13 ggplot2

作者: 胜果铺子 | 来源:发表于2024-02-12 00:40 被阅读0次

基本格式

ggplot(数据集, aes(..............)) 
  + 图层 goem_xxxx()
  + 图层 goem_xxxx()
  + 坐标轴设置(比如xlim, scale_x_continuous)
  + 坐标轴标题(比如xlab)

每个图层其实就对应传统的折线图的折现、散点图的点、或者其他东西。

图层

大部分是以geom_开头的函数。

另外有一部分是以stat_开头的。比如:stat_ecdf() 绘制经验累积分布函数。

数据集哪个列做x,哪个列做y是在哪里定义的?

在aes(x, y, ....) 。前2个参数就是定义x和y。这个aes是可以继承的。在ggplot定义,也可以在图层goem_xxx(aes(...))里定义。

疑问:xlim和xmax,xmin是什么关系?

这两组东西功能是否重复呢?

不重复。xlim控制的是坐标轴。而xmax,xmin,ymax,ymin是控制geom_rect()绘制的矩形。

aes()里面可以装什么东西?

fill,color等等

可以查下面的参考。搜索“aesthetics”,就能找到比如Position related aesthetics,Differentiation related aesthetics, Colour related aesthetics, Aesthetics: grouping

scale__ 是什么?

scale是坐标轴,能设置刻度。
| scale_x_continuous | Position scales for continuous data (x & y) |

facet是什么?

分类(分组)。根据一个变量或者2个变量的值,把原来的数据集分成多个组,然后为每个组画子图。如果是1个变量,可以用facet_wrap画一维的(但是自动换行)。如果是2个分类变量,则用facet_grid画二维的图。

参考

ggplot2参考


《A Layered Grammar of Graphics》p6

To be precise, the layered grammar defines the components of a plot as:
• a default dataset and set of mappings from variables to aesthetics,
1 数据集、aes
• one or more layers, with each layer having one geometric object, one statistical transformation, one position adjustment, and optionally, one dataset and set of aesthetic
mappings,
2 图层
• one scale for each aesthetic mapping used,
3 刻度
• a coordinate system,
4 坐标系
• the facet specification.
5 分面


ggplot2-wires.pdf

文章只有10页。通过实例来介绍ggplot2。


复杂度有现成的函数

Modern Data Visualization with R

比如上面文章做的瀑布图。现在有函数可以直接调用。


R for data science

包罗各个方面话题的书。不只是绘图了。还有数据处理。模型等等。

R for data science

相关文章

网友评论

      本文标题:2024-02-13 ggplot2

      本文链接:https://www.haomeiwen.com/subject/wqbdadtx.html