1. 第一章
1.1. ggplot2语法
1.1.1. geom_xxx()与stat_xxx()
仅当绘制图表涉及统计变换时,我们才会使用统计变换函数stat_xxx()
函数分类
| ggplot2函数分类 |
![](https://img.haomeiwen.com/i13029048/429adc1adec09714.jpg)
(1)图元函数
(2)误差函数
geom_crossbar()绘制误差框、geom_errorbar()竖直误差线、geom_errorbarh()水平误差线、geom_pointrange()带误差棒的均值点
此类函数,需先设置统计变换参数
1.1.2. stat_xxx():统计变换函数
stat()函数必须与geom()对应,才能进行数据计算
图层概念
stat_xxx()开始的图层
无须设定统计变换参数,但须指定集合对象名称图表类型geom,使作图过程更加侧重统计变换过程,见以下a1
geom_xxx()绘制的图层
更侧重图表类型的绘制,见以下b1
当绘制的图表不涉及统计变换时,可直接使用geom_xxx()函数,无须设定stat参数,stat参数默认值为stat='identity'(无数据变换)
#a1
ggplot(mydata, aes(Class, Value.fill = Class)) + stat_summary(fun.y = 'mean', fun.args = list(mult = 1), geom = 'point' , color = 'white', size = 4)
#b1
ggplot(mydata, aes(Class, Value.fill = Class)) + geom_point(stat = 'summary', fun.y = 'mean',fun.args = list(mult = 1), color = 'white', size = 4)
1.1.3. 视觉通道映射
参数包括:
color/col/colour、fill、size、angle、linetype、shape、vjust和hjust。
linetype、shape等部分参数只适用于类别型变量
列出几个需要注意的参数
(1)angle:仅部分集合对象有,如geom_text()中文本的摆放角度
(2)vjust指垂直位置微调,在(0,1)区间,0 = 'buttom', 0.5 = 'middle', 1 = 'top';hjust指水平位置微调,在在(0,1)区间,0 = 'left', 0.5 = 'center', 1 = 'right'。
(3)color/col/colour指轮廓的颜色,fill指填充区域的颜色
![](https://img.haomeiwen.com/i13029048/e281c6cc656e4cc5.jpg)
1.1.4. 度量调整
度量用于控制变量映射到视觉对象的具体细节
![](https://img.haomeiwen.com/i13029048/e98cf32273eea4bf.jpg)
![](https://img.haomeiwen.com/i13029048/8d025ba72ca793f4.jpg)
aes()内部、外部的区别
(1)内部,指定的视觉通道映射参数需个性化映射时,写于内部
(2)外部,统一设定某些图标元素对象(共性、统一化)时,写于外部,所有观测值会按统一树形进行映射
data("iris")
library(ggplot2)
str(iris)
colnames(iris)
ggplot(iris,aes(x=Sepal.Length, y=Sepal.Width,fill=Species))+
geom_boxplot()
ggplot(iris,aes(x=Sepal.Length, y=Sepal.Width,,group=Species) )+
geom_boxplot(fill = 'lightblue')
1.1.5. 坐标系
三种坐标系:
直角坐标系、极坐标系、地理坐标系
data("iris")
library(ggplot2)
str(iris)
colnames(iris)
ggplot(iris,aes(x=Sepal.Length, y=Sepal.Width,fill=Species))+
geom_bar(stat = 'identity') #直角坐标系
ggplot(iris,aes(x=Sepal.Length, y=Sepal.Width,fill=Species))+
geom_bar(stat = 'identity')+
coord_polar() #极坐标系
ggplot(iris,aes(x=Sepal.Length, y=Sepal.Width,fill=Species))+
geom_bar(stat = 'identity')+
coord_map() #地理坐标系,这个例子呈现比较差,但是我懒得找了。。。。
坐标轴度量
结合我的个人需要,我比较关注对数坐标轴度量,分类坐标轴度量,顺序坐标轴度量
![](https://img.haomeiwen.com/i13029048/3ba23515cd8f8927.jpg)
时间坐标轴度量
时间是连续变量,可以对应到线性度量,也可以划分时刻、星期、月份、季节或年份
网友评论