本篇我们着重介绍ggplot中的scale_*_*()函数。
借助代码:
help(package = "ggplot2")
获取ggolot2中的所有函数名称,检索到scale_*_*,可以看到有近百个函数,总结下来,可以分为几个大类,分别是:
- scale_alpha_*() 【设置透明度】
- scale_color_*() 或 scale_colour_*() 【设置边框/散点颜色】
- scale_fill_*() 【设置填充颜色和图例相关内容】
- scale_linetype_*() 【设置线条样式】
- scale_shape_*() 【设置散点样式】
- scale_size_*() 【设置散点/文本大小】
- scale_radius_*() 【设置散点半径大小】
- scale_x_*() 【设置横坐标相关参数】
- scale_y_*() 【设置纵坐标相关参数】
其中,alpha, size, radius语法较为相似,linetype, shape较为相似,color, fill较为相似,x, y较为相似,因此主要分为以下四个部分学习。
1. alpha, size, radius
常用语句如下(以alpha为例):
p <- ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(alpha = year))
p
#根据year的分类赋予散点不同的透明度
p + scale_alpha("cylinders")
#将注释标题更改为"cylinders"
p + scale_alpha(range = c(0.1, 0.8))
#限定alpha的取值在0.1到0.8之间,系统会根据类别数目在0.1到0.8内等距取值
p + scale_alpha(guide = "none")
#guide参数也较为常用,设置为"none"可取消注释,设置为"legend"为添加注释
size, radius语法类似。
2. linetype, shape
R中介绍默认的几种linetype由曼彻斯特大学的Richard Pearson教授创建,连续型变量无法映射给linetype;关于shape,R提示如果变量类型多于六种,则可能需要使用scale_shape_manual()函数进行自定义设置,因为系统默认的shape仅有六种。【连续性变量无法映射给linetype/shape】
2.1 linetype/shape 展示
df_lines <- data.frame(
linetype = factor(1:4,
labels = c("solid", "longdash", "dashed", "dotted")
)
)
ggplot(df_lines) +
geom_hline(aes(linetype = linetype, yintercept = 0), size = 2) +
scale_linetype_identity() +
facet_grid(linetype ~ .) +
theme_void(20)
#依次绘制实线、长虚线、虚线、点线
df_shapes <- data.frame(shape = 0:24)
ggplot(df_shapes, aes(0, 0, shape = shape)) +
geom_point(aes(shape = shape), size = 5, fill = 'red') +
scale_shape_identity() +
facet_wrap(~shape) +
theme_void()
#展示可用的几种shape
2.2 linetype/shape 实际应用
base <- ggplot(economics_long, aes(date, value01))
base + geom_line(aes(linetype = variable)) + scale_linetype(na.value = "blank", "type")
#允许缺失值存在,设置注释标题为"type"
前面提到连续型变量无法映射到linetype与shape中,同时对于多余六个分类变量的映射,shape只能展示前六个。
set.seed(100)
smalldata <- diamonds[sample(nrow(diamonds), 100), ]
p <- ggplot(smalldata, aes(carat, price, shape = color)) + geom_point()
p
#color变量有七个类别,R会提示warning
可以看到颜色类别为J的数据被移除了,系统默认仅提供六种shape,但是可以通过下列代码解决:
p + scale_shape_identity(guide = "none")
#对于超过六个分类变量,系统会使用字母作为散点形状
p + scale_shape_manual(values = 1:7)
#用户赋值映射给values改变形状
3. color, fill
3.1 离散映射:分类变量(以fill为例)
填充颜色的离散映射主要有三种,分别是:hue;brewer 和 manual(在最后介绍)。
3.11 hue
对于hue,R中对其主要参数的介绍为
- h range of hues to use, in [0, 360] 【色相,h范围越大,颜色区分度越大】
- c chroma (intensity of colour), maximum value varies depending on combination of hue and luminance.【饱和度,c值越大,颜色越鲜亮】
- l luminance (lightness), in [0, 100] 【明度】
作图代码如下:
p <- ggplot(mtcars, aes(factor(cyl))) + geom_bar(aes(fill = factor(cyl)))
p
#p1(默认c = 100, l = 65)
p + scale_fill_hue(c = 5)
#p2
p + scale_fill_hue(l = 100)
#p3
p1
p2
p3
3.12 brewer
使用调色板进行调色,有兴趣的小伙伴可以自己了解一下RColorBrewer这个包。
3.2 连续映射:连续型变量(以fill为例)
连续映射的实现主要有scale_fill_gradient() 【默认渐变着色】;scale_fill_distiller() 【调色板着色】,这里仅介绍渐变着色。
3.21 渐变着色
- scale_fill_gradient() 【双色渐变,参数:low, high】
- scale_fill_gradient2() 【三色渐变,参数:low, mid, high】
- scale_fill_gradientn() 【n色渐变,参数:colours/colors】
4. scale_x_*() 和 scale_y_*()
scale_x/y_*()函数用于设置坐标轴的有关性质,如对坐标轴命名、修改刻度、显示部分信息等,本文介绍同样分为离散和连续两种类型。
4.1 离散型(本例中x为离散变量/分类变量)
p <- ggplot(smalldata, aes(cut, price)) + geom_point()
p
p + scale_x_discrete("CUT")
#重新命名横坐标轴为"CUT"
p + scale_x_discrete(labels = c("Fair" = "F", "Good" = "G"))
#重新命名刻度,可以只重命名其中两个
p + scale_x_discrete(limits = c("Good", "Fair", "Premium", "Ideal"))
#可以仅显示指定类别的数据,同时按给定顺序绘图
4.2 连续型(本例中y为连续变量)
p + scale_y_continuous("PRICE")
#命名同离散型一致
p + scale_y_continuous(breaks = c(0, 7500, 15000))
#手动设置坐标节点
p + scale_y_continuous(limits = c(7500, 15000))
#仅显示price落在[7500, 15000]的数据
4.3 其他参数
scale_x_discrete(reorder(x, m))
#根据变量m对离散变量x进行排序
scale_x_continuous(reverse())
#将连续变量x大小值反转
scale_x/y_discrete/continuous(position = "top/bottom/left/right")
#调整坐标刻度和标题的位置
5. scale_*_manual()
通过函数scale_*_manual()可以自定义参数值,主要有以下几种:
• scale_colour_manual(..., values, aesthetics = "colour")
• scale_fill_manual(..., values, aesthetics = "fill")
• scale_size_manual(..., values)
• scale_shape_manual(..., values)
• scale_linetype_manual(..., values)
• scale_alpha_manual(..., values)
• scale_discrete_manual(aesthetics, ..., values)
常用语句如下:
ggplot() + scale_*_manual(..., values = vector)
#将vector映射给values实现自定义参数值
ggplot() + scale_discrete_manual(..., aesthetics = c("fill", "shape"), values = vector)
#实现同时调整两个离散型参数