# 第 23 章 ggplot2之标度

ggplot2图层语法框架

## 23.1 标度

22章，我们了解到ggplot2中，映射是数据转化到图形属性，这里的图形属性是指视觉可以感知的东西，比如大小，形状，颜色和位置等。我们今天讨论的标度（scale）是控制着数据到图形属性映射的函数，每一种标度都是从数据空间的某个区域（标度的定义域）到图形属性空间的某个区域（标度的值域）的一个函数。

## 23.2 图形属性和变量类型

library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.2.3
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'tidyr' was built under R version 4.2.2
## Warning: package 'readr' was built under R version 4.2.2
## Warning: package 'purrr' was built under R version 4.2.2
## Warning: package 'dplyr' was built under R version 4.2.3
## Warning: package 'stringr' was built under R version 4.2.2
## Warning: package 'lubridate' was built under R version 4.2.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
mpg %>%
ggplot(aes(x = displ, y = hwy)) +
geom_point(aes(colour = class)) 

ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(colour = class)) +

scale_x_continuous() +
scale_y_continuous() +
scale_colour_discrete()

ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(colour = class)) +

scale_x_continuous(name = "This is my x axis") +
scale_y_continuous(name = "This is my y axis") +
scale_colour_brewer()

## 23.4 丰富的标度体系

scale_colour_manual(
palette = function(),
limits = NULL,
name = waiver(),
labels = waiver(),
breaks = waiver(),
minor_breaks = waiver(),
values = waiver(),
...
)
• 参数name，坐标和图例的名字，如果不想要图例的名字，就可以 name = NULL

• 参数limits, 坐标或图例的范围区间。连续性c(n, m)，离散型c("a", "b", "c")

• 参数breaks, 控制显示在坐标轴或者图例上的值（元素）

• 参数labels, 坐标和图例的间隔标签

• 一般情况下，内置函数会自动完成
• 也可人工指定一个字符型向量，与breaks提供的字符型向量一一对应
• 也可以是函数，把breaks提供的字符型向量当做函数的输入
• NULL，就是去掉标签
• 参数values 指的是（颜色、形状等）视觉属性值,

• 要么，与数值的顺序一致；
• 要么，与breaks提供的字符型向量长度一致
• 要么，用命名向量c("数据标签" = "视觉属性")提供
• 参数expand, 控制参数溢出量

• 参数range, 设置尺寸大小范围，比如针对点的相对大小

## 23.5 案例详解

gapdata <- read_csv("./demo_data/gapminder.csv")
## Rows: 1704 Columns: 6
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): country, continent
## dbl (4): year, lifeExp, pop, gdpPercap
##
## ℹ Use spec() to retrieve the full column specification for this data.
## ℹ Specify the column types or set show_col_types = FALSE to quiet this message.
newgapdata <- gapdata %>%
group_by(continent, country) %>%
summarise(
across(c(lifeExp, gdpPercap, pop), mean)
)
## summarise() has grouped output by 'continent'. You can override using the
## .groups argument.
newgapdata
## # A tibble: 142 × 5
## # Groups:   continent [5]
##    continent country                  lifeExp gdpPercap       pop
##    <chr>     <chr>                      <dbl>     <dbl>     <dbl>
##  1 Africa    Algeria                     59.0     4426. 19875406.
##  2 Africa    Angola                      37.9     3607.  7309390.
##  3 Africa    Benin                       48.8     1155.  4017497.
##  4 Africa    Botswana                    54.6     5032.   971186.
##  5 Africa    Burkina Faso                44.7      844.  7548677.
##  6 Africa    Burundi                     44.8      472.  4651608.
##  7 Africa    Cameroon                    48.1     1775.  9816648.
##  8 Africa    Central African Republic    43.9      959.  2560963
##  9 Africa    Chad                        46.8     1165.  5329256.
## 10 Africa    Comoros                     52.4     1314.   361684.
## # ℹ 132 more rows
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_continuous()
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10()
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10(breaks = c(500, 1000, 3000, 10000, 30000),
labels = scales::dollar)
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10(
name = "GDP per capita",
breaks = c(500, 1000, 3000, 10000, 30000),
labels = scales::unit_format(unit = "dollar"))
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10() +
scale_color_viridis_d()

newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10() +
scale_color_brewer(type = "qual", palette = "Set1")
newgapdata %>%
ggplot(aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent, size = pop)) +
scale_x_log10() +
scale_color_manual(
name = "continents",
values = c("Africa" = "red", "Americas" = "blue", "Asia" = "orange",
"Europe" = "black", "Oceania" = "gray"),
breaks = c("Africa", "Americas", "Asia", "Europe", "Oceania"),
labels = c("africa", "americas", "asia", "europe", "oceania")
) +
scale_size(
name = "population size",
breaks = c(2e8, 5e8, 7e8),
labels = c("200 million", "500 million", "700 million")
)