R数据科学——ggplot2笔记(1)

一个说明

ggplot2::ggplot()指使用ggplot2包中的ggplot函数,::表示快速调用某个包里的函数。

示例

使用ggplot2包中的mpg数据框,这份数据是一份汽车资料:

1
knitr::kable(head(ggplot2::mpg, 10))
manufacturermodeldisplyearcyltransdrvctyhwyflclass
audia41.819994auto(l5)f1829pcompact
audia41.819994manual(m5)f2129pcompact
audia42.020084manual(m6)f2031pcompact
audia42.020084auto(av)f2130pcompact
audia42.819996auto(l5)f1626pcompact
audia42.819996manual(m5)f1826pcompact
audia43.120086auto(av)f1827pcompact
audia4 quattro1.819994manual(m5)41826pcompact
audia4 quattro1.819994auto(l5)41625pcompact
audia4 quattro2.020084manual(m6)42028pcompact

创建ggplot2图形,散点图:

1
2
3
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) -> p
p

plot of chunk unnamed-chunk-2

其中mapping是映射的意思,在ggplot()()中出现的内容将传递之后的绘图函数,p保存了绘制好的图形,可以进一步进行美化和调整:

1
2
3
4
p + theme_minimal(base_family = enfont) +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title = element_text(size = 12))

plot of chunk unnamed-chunk-3 可以将一些图片调整的代码存放在modify命名的一个对象中:

1
2
3
4
theme_minimal(base_family = enfont) +
theme(axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.title = element_text(size = 12)) -> modify

实际上,ggplot2的魅力之一在于通过颜色(color)、形状(shape)、透明度(alpha)等等属性展示超越2维的信息,比如:

1
2
3
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
modify

plot of chunk unnamed-chunk-5

另外一种展示超越2维属性的方法就是分面

1
2
3
4
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2) +
modify

plot of chunk unnamed-chunk-6

facet_wrap()中的第一个参数是R中的一种数据结构,叫作“公式”,并非数学意义上的意思。也可以使用2个变量进行分面:

1
2
3
4
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap(drv ~ cyl, nrow = 2) +
modify

plot of chunk unnamed-chunk-7

可以使用占位符.来代替facet_wrap()第一个参数中的drv,效果如下:

1
2
3
4
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap(. ~ cyl, nrow = 2) +
modify

plot of chunk unnamed-chunk-8

几何对象

R并非只有ggplot2一种绘图方案,比如:

1
2
3
4
5
6
set.seed(123)
n <- 1000
x1 <- matrix(rnorm(n), ncol = 2)
x2 <- matrix(rnorm(n, mean = 3, sd = 1.5), ncol = 2)
x <- rbind(x1, x2)
head(x)
1
2
3
4
5
6
7
#>             [,1]        [,2]
#> [1,] -0.56047565 -0.60189285
#> [2,] -0.23017749 -0.99369859
#> [3,] 1.55870831 1.02678506
#> [4,] 0.07050839 0.75106130
#> [5,] 0.12928774 -1.50916654
#> [6,] 1.71506499 -0.09514745
1
smoothScatter(x, xlab = "x1", ylab = "x2")

plot of chunk unnamed-chunk-9

而ggplot2肯定并非只有一类几何对象,比如geom_smoothgeom_linegeom_bar等等,而这些对象生成的图层可以堆叠,比如:

1
2
3
4
5
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = drv),
show.legend = FALSE) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv),
show.legend = FALSE)

plot of chunk unnamed-chunk-10

geom_smooth()使用单个几何对象表示多行数据(一条曲线),而geom_bar()geom_point则是多个几何对象(多个矩形或点),将geom_smooth()group属性映射为一个离散变量时,这样ggplot2就会为这个分类变量的每个唯一值绘制一个独立的几何对象。

1
2
3
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv)
)

plot of chunk unnamed-chunk-11

也可以为不同的图层指定不同的数据:

1
2
3
4
5
6
7
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = drv)) +
geom_smooth(data = dplyr::filter(mpg, class == "subcompact"),
mapping = aes(x = displ, y = hwy),
se = FALSE,
show.legend = FALSE
)

plot of chunk unnamed-chunk-12

统计变换

geom_bar()函数可以绘制的基本条形图。

1
2
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))

plot of chunk unnamed-chunk-13

条形图 x 轴显示的是 cut ,这是 diamonds 数据集中的一个变量。 y 轴显示的是 count ,但 count 不是 diamonds 中的变量! count 来自哪里呢?很多图形绘制的是数据集的原始数据,比如散点图。另外一些图形则可以绘制那些计算出的新数据,比如条形图。

绘图时用来计算新数据的算法称为stat(statistical transformation),即统计变换。你可能想要覆盖默认的统计变换。在以下代码中,我们将geom_bar()函数的统计变换从计数(默认值)修改为标识。这样我们就可以将条形的高度映射为y轴变量的初始值。遗憾的是,当随意说起条形图时,人们指的可能就是这种条形图,其中条形高度已经存在于数据中,而不是像前一个图一样,条形高度由对行进行计数来生成:

1
2
3
4
5
6
7
8
9
10
11
demo <- tribble(
~a, ~b,
"bar_1", 20,
"bar_2", 30,
"bar_3", 40
)

ggplot(data = demo) +
geom_bar(
mapping = aes(x = a, y = b), stat = "identity"
)

plot of chunk unnamed-chunk-14

也可以改变统计变换的默认映射:

1
2
3
4
ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, y = ..prop.., group = 1)
)

plot of chunk unnamed-chunk-15

你可能想要在代码中强调统计变换。例如,你可以使用stat_summary()函数将人们的注意力吸引到你计算出的那些摘要统计量上。stat_summary()函数为x的每个唯一值计算y值的摘要统计:

1
2
3
4
5
6
7
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)

plot of chunk unnamed-chunk-16

ggplot2提供了20多个统计变换以供你使用。每个统计变换都是一个函数,因此你可以按照通用方式获得帮助,例如?stat_bin

Thanks for sponsoring. 谢谢老板! 老板大气!身体健康!

欢迎关注我的其它发布渠道