Scales, Axes and Legends
文章目录
- Scales, Axes and Legends
-
- 6.5 限制范围(Limits)
- 6.6 标度工具箱(Scales Toolbox)
-
- 6.6.1 连续位置标度(Continuous Position Scales)
- 6.6.2 颜色标度(Colour Scales)
-
- 6.6.2.1 连续型(Continuous)
- 6.6.2.2 离散型(Discrete)
- 6.6.3 手动离散标度(The Manual Discrete Scale)
- 6.6.4 同一型标度(The Identity Scale)
6.5 限制范围(Limits)
坐标轴在图表上的范围通常来自默认生成的数据范围。但有时我们想修改这个范围,扩大数据覆盖范围或缩小,使数据更集中和突出,然后我们可以使用它limits
参数:
-
对于连续变量:应设置为长度为2的数字向量;如果只想设置上限或下限,则设置另一个值
NA
-
离散变量:设置字符向量;列出所有数据点
df <- data.frame(x = 1:3, y = 1:3) base <- ggplot(df, aes(x, y)) geom_point() #直接查看图表 base #查看其中一个区间,系统会提示您丢失两个数据点 base scale_x_continuous(limits = c(1.5, 2.5)) #> Warning message:Removed 2 rows containing missing values (geom_point). ##x轴的范围通过标准设置 base scale_x_continuous(limits = c(0, 4))
我们经常修改限制范围,以下函数将简化操作,xlim()
, ylim()
and lims()
我们可以设置如下:
xlim(10, 20)
:x轴10-20的连续刻度ylim(20, 10)
:y轴20-10的连续反转刻度xlim("a", "b", "c")
:x三个离散词作为刻度点xlim(as.Date(c("2008-05-01", "2008-08-01")))
:x2008年5月1日至2008年8月1日
让我们简化以上代码:
base xlim(0, 4) base xlim(4, 0) base
lims
(x
=
c
(
0
,
4
)
)
细细看一下就发现,图形生成的坐标范围要比我们设定的大一丢丢,这是默认的。设置参数expand = c(0, 0)
可清除多余的刻度范围。一般我们与geom_raster()
结合使用:
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density)) +
theme(legend.position = "none")
ggplot(faithfuld, aes(waiting, eruptions)) +
geom_raster(aes(fill = density)) +
scale_x_continuous(expand = c(0,0)) +
scale_y_continuous(expand = c(0,0)) +
theme(legend.position = "none")
:xlim()
,ylim()
中并没有expand
参数
默认情况下,超出限制的任何数据都转换为NA
,可以使用参数oob(out of bounds)
将其覆盖到刻度。默认情况下即参数obb = scales::censor
;另一个选项是scales::squish()
,它将所有值压缩到范围内。来看例子:
df <- data.frame(x = 1:5)
p <- ggplot(df, aes(x, 1)) + geom_tile(aes(fill = x), colour = "white")
p
p + scale_fill_gradient(limits = c(2, 4))
p + scale_fill_gradient(limits = c(2, 4), oob = scales::squish)
6.6 标度工具箱(Scales Toolbox)
调整默认标度之外,还可创建新的标度覆盖默认值,有四类:
-
位置标度:将连续型、离散型、日期-时间数据映射到坐标轴上;
-
颜色标度:将连续型或离散型变量映射到颜色;
-
手动标度:将离散型变量映射到选择的大小、形状、颜色、线条等;
-
同一型标度:数据能被R中的绘图函数理解时,且数据空间和图形属性空间相,可以使用同一型标度,此时默认不绘制图例。
我们一个一个看:
6.6.1 连续型位置标度(Continuous Position Scales)
每个图表都有两个位置标度,即x和y。最常见的连续型位置标度就是scale_x_continuous()
和 scale_y_continuous()
,它们可以将数据映射到x轴和y轴。
每个连续型标度都可以接受一个trans
参数,允许使用各种变换:
# Convert from fuel economy to fuel consumption
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
scale_y_continuous(trans = "reciprocal")
# Log transform x and y axes
ggplot(diamonds, aes(price, carat)) +
geom_bin2d() +
scale_x_continuous(trans = "log10") +
scale_y_continuous(trans = "log10")
每一种变换都是由所谓的变换器(transformer)实现的,下表是比较常用的变换器:
函数名 | 变换函数 f ( x ) f(x) f(x) | 逆变换函数 f − 1 ( y ) f^{-1}(y) f−1(y) |
---|---|---|
asn | tanh − 1 ( x ) \tanh^{-1}(x) tanh−1(x) | tanh ( y ) \tanh(y) tanh(y) |
exp | e x e^x ex | l o g ( y ) log(y) log(y) |
identity | x x x | y y y |
log | log ( x ) \log(x) log(x) | e y e^y ey |
log10 | log 10 ( x ) \log_{10}(x) log10(x) | 1 0 y 10^y 10y |
log2 | l o g 2 ( x ) log_2(x) log2(x) | 2 y 2^y 2y |
logit | log ( x 1 − x ) \log(\frac{x}{1 - x}) log(1−xx) | 1 1 + e ( y ) \frac{1}{1 + e(y)} 1+e(y)1 |
pow10 | 1 0 x 10^x 10x | log 10 ( y ) \log_{10}(y) log10(y) |
probit | Φ ( x ) \Phi(x) Φ(x) | Φ − 1 ( x ) \Phi^{-1}(x) Φ−1(x) |
reciprocal | x − 1 x^{-1} x−1 | y − 1 y^{-1} y−1 |
reverse | − x -x −x | − y -y −y |
sqrt | x 1 / 2 x^{1/2} x1/2 | y 2 y^2 y2 |
其中有一些参数有简写形式,如,scale_x_log10()
、scale_x_sqrt()
、scale_x_reverse()
另外,变换器同样可以用于coord_trans()
中,此时变换将在统计量计算完成后进行,详见第七章
日期和时间值基本上属于连续型,但在标注坐标轴时处理方式稍有不同。我们使用Date
和POSIXct
类的时间值。如果是其他格式的,要用as.Date
和as.POSIXct
进行转换。
scale_x_date()
和scale_x_datetime()
的用法和scale_x_continous
用法相似。
date_breaks()
和date_labels()
的用法稍有不同:
date_breaks()
和date_minor_breaks()
:可以设置日期间隔(年、月、周、日、小时、分钟、秒)作为断点,例如date_breaks = "2 weeks"
date_labels()
:通过strptime()
和format()
指定特殊格式,如下表
String | Meaning |
---|---|
%S |
second (00-59) |
%M |
minute (00-59) |
%l |
hour, in 12-hour clock (1-12) |
%I |
hour, in 12-hour clock (01-12) |
%p |
am/pm |
%H |
hour, in 24-hour clock (00-23) |
%a |
day of week, abbreviated (Mon-Sun) |
%A |
day of week, full (Monday-Sunday) |
%e |
day of month (1-31) |
%d |
day of month (01-31) |
%m |
month, numeric (01-12) |
%b |
month, abbreviated (Jan-Dec) |
%B |
month, full (January-December) |
%y |
year, without century (00-99) |
%Y |
year, with century (0000-9999) |
例如,如果想以14/10/1979
的形式显示日期,可以使用字符串%d/%m/%y
拿经济数据economics
(时间序列)举个栗子,注意横轴的日期。
- 完整的时间段
base <- ggplot(economics, aes(date, psavert)) +
geom_line(na.rm = TRUE) +
labs(x = NULL, y = NULL)
base # Default breaks and labels
base + scale_x_date(date_labels = "%Y", date_breaks = "5 years")
- 取其中一小段时间
#按一个月为间隔显示刻度
标签 base + scale_x_date( limits = as.Date(c("2004-01-01", "2005-01-01")), date_labels = "%y年 %b", date_minor_breaks = "1 month" ) #按两周为间隔显示刻度标签 base + scale_x_date( limits = as.Date(c("2004-01-01", "2004-06-01")), date_labels = "%m/%d", date_minor_breaks = "2 weeks" )
:一个好玩的现象,一个疑问,字符串%y
本该是英文月份缩写,在R ×64 3.6.2
中为啥会变成中文
6.6.2 颜色标度(Colour Scales)
除了位置之外,颜色是最常用的图形属性。有很多方法可以将“值”映射成颜色
在ggplot2中,使用了一种名为HCL色彩空间(Hue-Chroma-Luminance)的现代方案,由三个部分构成,分别是色相(hue)、彩度(chroma)以及明度(luminance)
- 色相:是一个从0到360的角度值,将一种色彩赋予颜色属性(如红橙黄蓝等等) ;
- 明度:颜色的明暗程度,即看其接近黑色或白色的程度,明度0是黑色,1是白色;
- 彩度:色彩的纯度。0是灰色,彩度的最大值随明度变化而不同。
下图是这个色彩空间的三维形状,每个分面中明度是一个常数,色相被映射成一个角度,彩度被映射成半径,可以看到每个分面的中心都是灰色的,离边缘越近颜色越浓烈:
6.6.2.1 连续型(Continuous)
这部分我们使用faithful
数据集利用2d密度估计进行绘图。数据集记录了黄石公园中Old Faithful geyser
(泉)在喷发时刻以及每次喷发的等待时间。
faithful
#eruptions waiting
#1 3.600 79
#2 1.800 54
#3 3.333 74
#4 2.283 62
#5 4.533 85
#6 2.883 55
...
在最新的版本里,对于连续型变量,有四种基于渐变设置颜色的标度:
- :
scale_colour_gradient()
或scale_fill_gradient()
:两种颜色(浅蓝色-深蓝色)从低到高渐变,使用low
和high
两个参数控制此梯度两端的颜色
如果想要更改颜色,使用munsell colour system
(颜色系统)会很方便,运行munsell::hue_slice("5G")
会看到可用的颜色
下面的图示将隐藏图例并设置expand = 0
:
erupt <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
geom_raster() +
scale_x_continuous(NULL, expand = c(0, 0)) +
scale_y_continuous(NULL, expand = c(0, 0)) +
theme(legend.position = "none")
#不修改颜色标度,默认效果
erupt
#黑白渐变
erupt + scale_fill_gradient(low = "white", high = "black")
#munsell colour system
erupt + scale_fill_gradient(
low = munsell::mnsl("5G 9/2"),
high = munsell::mnsl("5G 6/8")
)
- :
scale_colour_gradient2()
或scale_fill_gradient2()
:和双色渐变类似,顺序低中高(红白蓝)。中点的默认值是0,但是可以使用参数midpoint
加以设置为任意值:
提取faithfuld
数据集中density
变量的中间值,将其赋予mid作为中点
mid <- median(faithfuld$density) 提取faithfuld数据集中density变量的中间值,将其赋予mid
erupt + scale_fill_gradient2(midpoint = mid)
- 自定义n色梯度:
scale_colour_gradientn()
或scale_fill_gradientn()
:此标度需要赋予参数colours
一个颜色向量,这对于有特殊意义的数据(比如表现地形等)很有用。以下代码包括从colorspace
包中的例程生成的调色板:
erupt + scale_fill_gradientn(colours = terrain.colors(7))
erupt + scale_fill_gradientn(colours = colorspace::heat_hcl(7))
erupt + scale_fill_gradientn(colours = colorspace::diverge_hcl(7))
默认情况下,colours
将沿数据范围均匀分布。要使它们间隔不均匀,请使用values
参数,该参数应该是0到1之间的二维向量。
- :
scale_color_distiller()
和scale_fill_gradient()
:可以通过设置palette
参数调整颜色:
erupt + scale_fill_distiller()
erupt + scale_fill_distiller(palette = "RdPu")
erupt + scale_fill_distiller(palette = "YlOrBr")
当数据集中包含缺失值NA时,可以通过na.value
参数调解颜色。默认情况下,它设置为灰色。
举个栗子,设置一个如下文本框,z变量中含有一个缺失值,将z变量映射为颜色图形属性:
df <- data.frame(x = 1, y = 1:5, z = c(1, 3, 2, NA, 5))
p <- ggplot(df, aes(x, y)) + geom_tile(aes(fill = z), size = 5)
p
#缺失值处不显示
p + scale_fill_gradient(na.value = NA)
#缺失值突出显示
p + scale_fill_gradient(low = "black", high = "white", na.value = "red")
6.6.2.2 离散型(Discrete)
离散数据有四种颜色标度,栗子使用最基本的条形图来举。
df <- data.frame(x = c("a", "b", "c", "d"), y = c(3, 4, 1, 2))
bars <- ggplot(df, aes(x, y, fill = x)) +
geom_bar(stat = "identity") +
labs(x = NULL, y = NULL) +
theme(legend.position = "none")
默认配色方案:
bars
scale_colour_hue()
或scale_fill_hue()
函数:修改默认配色,在HCL色轮周围拾取均匀间隔的色相,可以使用h
、c
和l
参数控制默认的色度(chroma)和亮度(luminance)以及色调(hues)范围:
bars + scale_fill_hue(c = 40)
bars + scale_fill_hue(h = c(180, 300))
默认配色方案有一个缺点,所有颜色都具有相同的亮度和色度,当以打印黑白图形时,显示为相同的灰色阴影。
-
scale_colour_brewer()
或scale_fill_brewer()
函数:采用ColorBrewer配色方案。对于类别型数据中的点而言,最好使的调色板是“
Set1
”、“Dark2
”;对于面积型数据而言,最好用的调色板是“Set2
”、“Pastel1
”、“Pastel2
”以及“Accent
”。
使用RColorBrewer::brewer.pal.info
可以以数据框的形式列出所有调色板,brewer.pal.info
是变量
RColorBrewer::brewer.pal.info
bars + scale_fill_brewer(palette = "Set1")
bars + scale_fill_brewer(palette = "Set2")
bars + scale_fill_brewer(palette = "Accent")
scale_colour_grey()
或scale_fill_grey()
函数:将离散型变量映射为从浅到深的灰度:
bars + scale_fill_grey()
bars + scale_fill_grey(start = 0.5, end = 1)
bars + scale_fill_grey(start = 0, end = 0.5)
scale_colour_manual()
或scale_fill_manual()
函数:可以自制离散型颜色标度。点击了解wesanderson
包,你可以自己设计自己的调色板,然后用在你的作图中。举个栗子代码如下:
#install.packages("wesanderson")
library(wesanderson)
bars + scale_fill_manual(values = wes_palette("GrandBudapest1"))
bars + scale_fill_manual(values = wes_palette("Zissou1"))
bars + scale_fill_manual(values = wes_palette("Rushmore1"))
:对于散点图明亮的颜色更适合;对于条形图淡色适合,明亮的颜色会过于刺眼。感受一下子:
下面是散点图:
# Bright colours work best with points df <- data.frame(x = 1:3 + 标签:
soric传感器ogutisoric高性能传感器oepqsoric传感器les