
❝在正式介绍ggplot2绘图之前,我们先来介绍一下ggplot2的绘图框架,以便后面介绍时更容易理解。一般来说,ggplot2绘图框架分为:图层、几何对象、映射、标度和主题。 本次以散点图为例简略带领大家快速了解ggplot2的绘图逻辑,更详细内容后面我们会一一推送。 ❞
安装并加载绘图所需的R包ggplot2。
# 安装ggplot2
install.packages("ggplot2")
# 加载ggplot2
library(ggplot2)
本次演示我们以R自带的数据集diamonds为例进行绘图,由于数据量比较大我们使用tidyverse随机抽取1000条数据进行演示。
# 载入数据
data("diamonds")
# 抽取1000条数据
library(tidyverse)
test_data <- sample_n(diamonds,1000)
# 查看数据
head(test_data)
# A tibble: 6 x 10
carat cut color clarity depth table price x y z
<dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 1.27 Very Good H SI1 62.3 58 7125 6.87 6.94 4.3
2 0.3 Ideal G IF 61.9 55 863 4.33 4.36 2.69
3 1.09 Ideal D SI2 61.7 59 5410 6.64 6.62 4.09
4 0.7 Premium F SI1 59.4 60 2239 5.77 5.74 3.42
5 0.71 Premium F SI2 62.2 57 2040 5.66 5.6 3.5
6 1.08 Fair E SI1 53.8 63 4790 6.99 6.81 3.71
通过在ggplot函数内使用data=指定绘图数据,通过aes进行映射,指定x轴和y轴;
theme_bw()函数用于设置主题。
ggplot(data = test_data,aes(x = carat,y = price)) + theme_bw()

在这里以geom_point()指定几何图层(散点图),出现图形元素。
ggplot(data = test_data,aes(x = carat,y = price)) +
geom_point() +
theme_bw()

我们可以通过在geom_point()中使用aes()函数为点添加新的映射。
在这里我们将cut这个分类变量映射给color。
ggplot(data = test_data,aes(x = carat,y = price)) +
geom_point(aes(color = cut)) +
theme_bw()

当然不通过aes()映射的话,我们也可以直接color =指定点的颜色。
p1 <- ggplot(data = test_data,aes(x = carat,y = price)) +
geom_point(color = "red") +
theme_bw()

标度(scale)可以控制映射的属性,ggplot2中内置了很多标度;
在这里我们使用ggsci包中的标度scale_color_d3()来控制color属性,改变cut的默认颜色。
# 安装加载包
installed.packages("ggsci")
library(ggsci)
# 使用scale控制颜色属性
ggplot(data = test_data,aes(x = carat,y = price)) +
geom_point(aes(color = cut)) +
scale_color_d3() +
theme_bw()

R中常用的点的形状如下图所示,默认时使用16号形状。

这里我们使用mtcars这个数据集进行演示。
在这里我们首先在aes()外使用shape指定使用21号形状(该点内部为空,可以使用颜色进行填充);通过alpha指定点的透明度;
再在aes()内部将drat映射给size;通过factor()将连续变量cyl转换为因子后将其映射给fill;
最后使用scale_size设置点的大小范围,使用scale_fill_npg()设置点的内部填充色。
ggplot(mtcars,aes(x =wt ,y = mpg)) +
geom_point(shape = 21,alpha = 0.6,aes(size = drat,fill = factor(cyl))) +
scale_size(range = c(1,10)) +
scale_fill_npg() +
theme_bw()

当x,y为连续性变量时,我们分别可以通过scale_x_continuous和scale_y_continuous来控制x轴和y轴;
通过breaks=seq()控制刻度范围,labels=c()设置刻度内容。
ggplot(mtcars,aes(x =wt ,y = mpg)) +
geom_point(shape = 21,alpha = 0.6,aes(size = drat,fill = factor(cyl))) +
scale_size(range = c(1,10)) +
scale_fill_npg() +
scale_x_continuous(breaks = seq(0,6,0.5))+
scale_y_continuous(breaks = seq(0,40,10),labels=c("0 M/US","10 M/US","20 M/US","30 M/US","40 M/US")) +
theme_bw()

通过在labs()内指定title=设置主标题,x=设置x轴名,y=设置y轴名,size=和fill=分别对应了geom_point中的aes(size = drat,fill = factor(cyl))用来设置图例名。
ggplot(mtcars,aes(x =wt ,y = mpg)) +
geom_point(shape = 21,alpha = 0.6,aes(size = drat,fill = factor(cyl))) +
scale_size(range = c(1,10)) +
scale_fill_npg() +
scale_x_continuous(breaks = seq(0,6,0.5))+
scale_y_continuous(breaks=seq(0,40,10),labels=c("0 M/US","10 M/US","20 M/US","30 M/US","40 M/US")) +
labs(title = "mtcars",x = "Weight (1000 lbs)",y = "Miles/(US) gallon",size = "Displacement (cu.in.)",fill = "Number of cylinders") +
theme_bw()

我们可以通过在theme()内使用plot.title修改主标题属性,axis.title可以同时修改x和y轴标签属性,legend.position可以修改图例位置。
需要注意的是,这部分代码需要放在theme_bw()后,否则则无法生效。
ggplot(mtcars,aes(x =wt ,y = mpg)) +
geom_point(shape = 21,alpha = 0.6,aes(size = drat,fill = factor(cyl))) +
scale_size(range = c(1,10)) +
scale_fill_npg() +
scale_x_continuous(breaks = seq(0,6,0.5))+
scale_y_continuous(breaks=seq(0,40,10),labels=c("0 M/US","10 M/US","20 M/US","30 M/US","40 M/US")) +
labs(title = "mtcars",x = "Weight (1000 lbs)",y = "Miles/(US) gallon",size = "Displacement (cu.in.)",fill = "Number of cylinders") +
theme_bw() +
theme(plot.title = element_text(hjust = 0.5,size = 20),axis.title = element_text(size = 15),legend.position = "top")

除此之外,我们还可以通过ggThemeAssist包美化主题。
# 安装
install.packages("ggThemeAssist")
# 加载
library(ggThemeAssist)
① 安装加载后,我们首先需要选中需要加载的代码,点击如图所示的位置ggplot Theme Assistant进行修改主题。

② 如图所示,手动修改选项参数即可,完成点击右上角done即可加载新代码。

本次内容旨在抛砖引玉,更详细的内容后续会推送。