6.1 介绍

6.2 修改标度

library(tidyverse)
## -- Attaching packages ----------------------------------------- tidyverse 1.3.0 --
## √ ggplot2 3.2.1     √ purrr   0.3.3
## √ tibble  2.1.3     √ dplyr   0.8.3
## √ tidyr   1.0.0     √ stringr 1.4.0
## √ readr   1.3.1     √ forcats 0.4.0
## -- Conflicts -------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(DT)

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point(aes(col = class))

# 等价于
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  scale_x_continuous() +
  scale_y_continuous() +
  scale_colour_discrete()
修改标度修改标度

修改标度

If you want to override the defaults,you’ll need to add the scale yourself, like this:

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  scale_x_continuous("A really awesome x axis ") +
  scale_y_continuous("An amazingly great y axis ")

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_x_continuous("Label 1") +
  scale_x_continuous("Label 2")
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.
ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  scale_x_continuous("Label 2")

Note the message: if you see this in your own code, you need to reorganise your code specification to only add a single scale. You can also use a different scale altogether:

ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = class)) +
  scale_x_sqrt() +
  scale_colour_brewer()

6.2.1 练习

1 如果将一个离散变量与一个连续的尺度配对会发生什么?如果你把一个连续的变量和一个离散的尺度配对会发生什么?

2 简化以下情节规范,使其更容易理解

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point() +
  scale_x_continuous() +
  scale_y_continuous("Highway mpg")

ggplot(mpg, aes(y = displ, x = class)) +
  geom_point(aes(col = drv)) +
  scale_x_discrete("Type of car") +
  scale_y_continuous("Displacement (l)") +
  scale_colour_discrete("Drive\ntrain")

# 等价于上图
ggplot(mpg, aes(y = displ, x = class)) +
  geom_point(aes(col = drv)) +
  scale_x_discrete("Type of car") +
  scale_y_continuous("Displacement (l)") +
  labs(col = "Drive\ntrain")
修改标度修改标度

修改标度

6.3 指南:图例和坐标轴

如果您正在努力获得您想要的图例,那么很可能您的数据格式是错误的。阅读第九章找出正确的格式。

library(knitr)
knitr::include_graphics("a.jpg")

6.3.1 Scale Title

df <- data.frame(x = 1:2, y = 1, z = "a")
df
##   x y z
## 1 1 1 a
## 2 2 1 a
p <- ggplot(df, aes(x, y)) + geom_point()
p + scale_x_continuous("X axis")
p + scale_x_continuous(quote(a + mathematical^expression))
Scale TitleScale Title

Scale Title

Because tweaking these labels is such a common task, there are three helpers that save you some typing: xlab(), ylab() and labs()

p <- ggplot(df, aes(x, y)) + geom_point(aes(colour = z))
p +
  xlab("X axis") +
  ylab("Y axis")
p + labs(x = "X axis", y = "Y axis", colour = "Colour\nlegend")
Scale TitleScale Title

Scale Title

有两种方法可以删除axis标签。将其设置为“”会省略标签,但仍会分配空间;NULL删除标签及其空间,仔细查看下面两个图的左侧和底部边界。我在图的周围画了一个灰色矩形,以便更容易地看到区别。

p <- ggplot(df, aes(x, y)) +
  geom_point() +
  theme(plot.background = element_rect(colour = "grey50"))
p + labs(x = "", y = "")

# 观察两幅图的区别
p + labs(x = NULL, y = NULL)
Scale TitleScale Title

Scale Title

6.3.2 Breaks and Labels

If you set labels, you must also set breaks; otherwise, if data changes, the breaks will no longer align with the labels.

下面的代码展示了坐标轴图例的一些基本示例

df <- data.frame(x = c(1, 3, 5) * 1000, y = 1)

df
##      x y
## 1 1000 1
## 2 3000 1
## 3 5000 1
axs <- ggplot(df, aes(x, y)) +
  geom_point() +
  labs(x = NULL, y = NULL)
axs
axs + scale_x_continuous(breaks = c(2000, 4000))
axs + scale_x_continuous(breaks = c(2000, 4000), labels = c("2k", "4k"))  # labels 和 breaks都存在
Scale TitleScale TitleScale Title

Scale Title

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point() +
  scale_x_continuous(breaks = seq(0,10,0.5)) +
  scale_x_continuous(breaks = seq(0,10,0.5),labels = str_c(seq(0,10,0.5),"K",sep = "")) # 没有实际意义,仅供测试
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

leg <- ggplot(df, aes(y, x, fill = x)) +
  geom_tile() +
  labs(x = NULL, y = NULL)

leg

fill的设置有很大作用

leg + scale_fill_continuous(breaks = c(2000, 4000))

leg + scale_fill_continuous(breaks = c(2000, 4000), labels = c("2k", "4k"))
scale_fill_continuousscale_fill_continuous

scale_fill_continuous

离散:如果你想在一个分类范围内重新标记breaks,你可以使用一个命名标签向量:

df2 <- data.frame(x = 1:3, y = c("a", "b", "c"))
ggplot(df2, aes(x, y)) +
  geom_point()

ggplot(df2, aes(x, y)) +
  geom_point() +
  scale_y_discrete(labels = c(a = "apple", b = "banana", c = "carrot")) 
scale_fill_continuousscale_fill_continuous

scale_fill_continuous

To suppress breaks (and for axes, grid lines) or labels, set them to NULL:

axs + scale_x_continuous(breaks = NULL)  # 连续
axs + scale_x_continuous(labels = NULL)

leg + scale_fill_continuous(breaks = NULL)
leg + scale_fill_continuous(labels = NULL)

The scales package provides a number of useful labelling functions:

  • scales::comma format() adds commas to make it easier to read large numbers.

  • scales::unit format(unit, scale) adds a unit suffix, optionally scaling

  • scales::dollar format(prefix, suffix) displays currency values, rounding to two decimal places and adding a prefix or suffix.

  • scales::wrap format() wraps long labels into multiple lines

例如:

library(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
axs
axs + scale_y_continuous(labels = scales::percent_format())
axs + scale_y_continuous(labels = scales::dollar_format(prefix = "$"))
leg + scale_fill_continuous(labels = scales::unit_format(suffix = "k",1e-3))

如何不使用scale完成该设置

axs +
  scale_y_continuous(breaks = c(0.950,0.975,1.000,1.025,1.050),labels = str_c(c(0.950,0.975,1.000,1.025,1.050) * 100,"%",sep = "")) +
  scale_x_continuous(breaks = seq(1000,5000,1000),labels = str_c(seq(1,5,1),"K",sep = ""))

axs + 
    scale_y_continuous(breaks = c(0.950,0.975,1.000,1.025,1.050),labels = str_c("$",c(0.950,0.975,1.000,1.025,1.050),sep = ""))

leg +
  scale_fill_continuous(breaks = seq(1000,5000,1000),labels = str_c(seq(1,5,1),"k"))  # 书中的代码有误

You can adjust the minor breaks (the faint grid lines that appear between the major grid lines) by supplying a numeric vector of positions to the minor breaks argument. This is particularly useful for log scales:

df <- data.frame(x = c(2, 3, 5, 10, 200, 3000), y = 1)
ggplot(df, aes(x, y)) +
  geom_point() +
  scale_x_log10()

mb <- as.numeric(1:10 %o% 10^(0:4))

ggplot(df, aes(x, y)) +
  geom_point() +
  scale_x_log10(minor_breaks = log10(mb))

注意使用%o%快速生成乘法表,并且必须在转换后的刻度上提供小的中断

6.3.3 练习

  1. 重新创建以下图形:
mpg %>% 
  ggplot() +
  geom_point(aes(displ,hwy)) +
  scale_x_continuous("Displacement") +
  scale_y_continuous(quote(Highway(miles/gallon))) +
  scale_x_continuous(breaks = seq(2,7,1),labels = str_c(seq(2,7,1),"L",sep = ""))
## Scale for 'x' is already present. Adding another scale for 'x', which will
## replace the existing scale.

  1. 列出您可以提供给breaks参数的三种不同类型的对象。breaks和labels有何不同?

字符型 数值型 时间

3.重新构造以下plot:

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point(aes(col = drv),size = 3) +
  scale_colour_discrete(breaks = c("4","f","r"),labels = str_c(c("4","f","r"),"wd",sep = ""))  # scale包括颜色,形状,大小...

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point(aes(col = drv),size = 3) +
  scale_y_continuous(breaks = seq(15,50,10),labels = str_c(seq(15,50,10),"k",sep = "")) +
  scale_x_continuous(breaks = seq(2,7,1),labels = str_c(seq(2,7,1),"L",sep = "")) +
  scale_colour_discrete(breaks = c("4","f","r"),labels = str_c(c("4","f","r"),"wd",seq = "")) +
  labs(x = "Displ",y = "Hwy",color = "Drv")

  1. What label function allows you to create mathematical expressions? What label function converts 1 to 1st, 2 to 2nd, and so on?

  2. What are the three most important arguments that apply to both axes and legends? What do they do? Compare and contrast their operation for axes vs. legends

6.4 图例

虽然最重要的参数在轴和图例之间共享,但是有些额外的选项仅适用于图例。图例比轴更复杂,因为:

  • 1.图例可以显示来自多个图层的多种美感(例如颜色和形状),并且图例中显示的符号根据图层中使用的几何图形而有所不同。

  • 2.轴始终出现在同一位置。图例可能出现在不同的位置,因此您需要一些全局的方式来控制它们。

  • 3.图例具有可调整的更多细节:应垂直显示还是水平显示?多少列?Key应该多大

以下各节描述了控制这些交互的选项。

6.4.1 层和图例

df <- tibble(x = 0,y = 1:3, z = letters[1:3])
df
## # A tibble: 3 x 3
##       x     y z    
##   <dbl> <int> <chr>
## 1     0     1 a    
## 2     0     2 b    
## 3     0     3 c
ggplot(df, aes(y, y)) +
  geom_point(size = 4, colour = "grey20") +
  geom_point(aes(colour = z), size = 2)

ggplot(df, aes(y, y)) +
  geom_point(size = 4, colour = "grey20", show.legend = TRUE) +
  geom_point(aes(colour = z), size = 2)
层和图例层和图例

层和图例

有时,您希望图例中的几何图形与图中的几何图形显示方式不同。当您使用透明度或大小来处理中等程度的过度绘图以及在绘图中使用颜色时,此功能特别有用。 您可以使用指南legend()override.aes参数进行此操作,稍后将对其进行详细了解。

norm <- data.frame(x = rnorm(1000), y = rnorm(1000))
norm$z <- cut(norm$x, 3, labels = c("a", "b", "c"))

ggplot(norm, aes(x, y)) +
  geom_point(aes(colour = z), alpha = 0.1)

ggplot(norm, aes(x, y)) +
  geom_point(aes(colour = z), alpha = 0.1) +
  guides(colour = guide_legend(override.aes = list(alpha = 1)))
层和图例层和图例

层和图例

ggplot2尝试使用最少的图例来准确传达情节中使用的美学。它通过组合图例来实现此目的,在图例中,相同的变量映射到不同的美学。下图显示了它如何处理点:如果颜色和形状都映射到相同的变量,则只需要一个图例。

ggplot(df, aes(x, y)) + geom_point(aes(colour = z))
ggplot(df, aes(x, y)) + geom_point(aes(shape = z))
ggplot(df, aes(x, y)) + geom_point(aes(shape = z, colour = z))
层和图例层和图例层和图例

层和图例

6.4.2 图层布局

The position and justification of legends are controlled by the theme setting legend.position, which takes values “right”,“left”,“top”,“bottom”,or “none” (no legend).

df <- data.frame(x = 1:3, y = 1:3, z = c("a", "b", "c"))

base <- ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z), size = 3) +
  xlab(NULL) +
  ylab(NULL)
base + theme(legend.position = "right") # the default
base + theme(legend.position = "bottom")
base + theme(legend.position = "none")
legend.position的位置legend.position的位置legend.position的位置

legend.position的位置

c(0, 1) is the top-left corner and c(1, 0) is the bottom-right corner. You control which corner of the legend the legend.position refers to with legend.justification, which is specified in a similar way.

base + theme(legend.position = c(0, 1), legend.justification = c(0, 1))
base + theme(legend.position = c(0.5, 0.5), legend.justification = c(0.5, 0.5))
base + theme(legend.position = c(1, 0), legend.justification = c(1, 0))
legend.position的位置legend.position的位置legend.position的位置

legend.position的位置

6.4.3 Guide函数

Most of the arguments to the guide function control the fine level details of the text colour, size, font etc. You’ll learn about those in the themes chapter. Here I’ll focus on the most important arguments.

6.4.3.1 guide_legend

df <- data.frame(x = 1, y = 1:4, z = letters[1:4])

# Base plot
p <- ggplot(df, aes(x, y)) + geom_raster(aes(fill = z))
p
p + guides(fill = guide_legend(ncol = 2))
p + guides(fill = guide_legend(ncol = 2, byrow = TRUE))
guide_legend()函数guide_legend()函数guide_legend()函数

guide_legend()函数

p <- ggplot(df, aes(1, y)) + geom_bar(stat = "identity", aes(fill = z))
p
p + guides(fill = guide_legend(reverse = TRUE))
guide_legend()函数guide_legend()函数

guide_legend()函数

6.4.3.2 guide colourbar

参数 barwidth and barheight; nbin controls the number of slices; reverse flips the colour bar to put the lowest values at the top.

df <- data.frame(x = 1, y = 1:4, z = 4:1)
p <- ggplot(df, aes(x, y)) + geom_tile(aes(fill = z))

p
p + guides(fill = guide_colorbar(reverse = TRUE))
p + guides(fill = guide_colorbar(barheight = unit(4, "cm")))
guide colourbar()函数guide colourbar()函数guide colourbar()函数

guide colourbar()函数

6.4.4 Exercises

  1. How do you make legends appear to the left of the plot?

legend.position

  1. What’s gone wrong with this plot? How could you fix it?
ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = drv, shape = drv)) +
  scale_colour_discrete("Drive train")

mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point(aes(color = drv,shape = drv)) +
  scale_color_discrete("Drive\ntrain")+
  scale_shape_discrete("Drive\ntrain")
scale_color_discrete()函数scale_color_discrete()函数

scale_color_discrete()函数

  1. Can you recreate the code for this plot?
mpg %>% 
  ggplot(aes(displ,hwy)) +
  geom_point(aes(color = class),size = 2,show.legend = FALSE) +
  geom_smooth(aes(color = class),method = "lm") +
  theme(legend.position = "bottom") +
  guides(color = guide_legend(nrow = 1, byrow = TRUE))

6.5 范围Limits

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: Removed 2 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_point).
base + scale_x_continuous(limits = c(0, 4))
limits()函数limits()函数limits()函数

limits()函数

base + xlim(0, 4)
base + xlim(4, 0)
base + lims(x = c(0, 4))
limits()函数limits()函数limits()函数

limits()函数

如果你有一双鹰眼,你会注意到坐标轴的范围实际上已经超出了你指定的限制。这确保了数据不会与坐标轴重叠。

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")
expand()函数expand()函数

expand()函数

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)
scale_fill_gradient()函数scale_fill_gradient()函数scale_fill_gradient()函数

scale_fill_gradient()函数

6.5.1 练习

  1. The following code creates two plots of the mpg dataset. Modify the code so that the legend and axes match, without using facetting!
fwd <- subset(mpg, drv == "f")

rwd <- subset(mpg, drv == "r")

ggplot(fwd, aes(displ, hwy, colour = class)) + geom_point()
ggplot(rwd, aes(displ, hwy, colour = class)) + geom_point()
练习练习

练习

  1. What does expand limits() do and how does it work? Read the source code.

  2. What happens if you add two xlim() calls to the same plot? Why?

  3. What does scale x continuous(limits = c(NA, NA)) do?

6.6 Scales工具箱

6.6.1 Continuous Position Scales

# 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") 
Continuous Position Scales函数Continuous Position Scales函数

Continuous Position Scales函数

scale x date() and scale x datetime() work similarly to scale x continuous() but have special date breaks and date labels arguments that work in date-friendly units:

下面的代码说明了其中一些参数。

base <- ggplot(economics, aes(date, psavert)) +
  geom_line(na.rm = TRUE) +
  labs(x = NULL, y = NULL)

base

base + scale_x_date(date_labels = "%Y",date_breaks = "5 years")
scale_x_date函数scale_x_date函数

scale_x_date函数

base + scale_x_date(
  limits = as.Date(c("2004-01-01", "2005-01-01")),
  date_labels = "%b %y",
  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"
)
scale_x_date函数scale_x_date函数

scale_x_date函数

6.6.2 颜色

位置后,最常用的美学映射是颜色。在ggplot2中,有许多种将值映射到颜色的方法:四种用于连续值的基于梯度的方法,以及两种用于映射离散值的方法。但是,在我们研究不同方法的细节之前,先学习一些色彩理论是很有用的。颜色理论很复杂,因为眼睛和大脑的基础生物学很复杂,因此本入门仅涉及一些更重要的问题。可以在http://tinyurl.com/clrdtls上在线获得一个出色且详细的展览

6.6.2.1 连续

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")

There are four continuous colour scales:

erupt

erupt + scale_fill_gradient(low = "white", high = "black")

erupt + scale_fill_gradient(
  low = munsell::mnsl("5G 9/2"),
  high = munsell::mnsl("5G 6/8")
)
scale_fill_gradient函数scale_fill_gradient函数scale_fill_gradient函数

scale_fill_gradient函数

mid <- median(faithfuld$density)
erupt + scale_fill_gradient2(midpoint = mid)

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))
scale_fill_gradientn函数scale_fill_gradientn函数scale_fill_gradientn函数

scale_fill_gradientn函数

erupt + scale_fill_distiller()
erupt + scale_fill_distiller(palette = "RdPu")
erupt + scale_fill_distiller(palette = "YlOrBr")
scale_fill_distiller函数scale_fill_distiller函数scale_fill_distiller函数

scale_fill_distiller函数

所有连续的色标都有一个na.value参数,用于控制缺失值(包括色标限制范围之外的值)所用的颜色。默认情况下,它设置为灰色,当您使用彩色刻度时,它将突出显示。如果使用黑白标度,则可能需要将其设置为其他值以使其更明显

df <- data.frame(x = 1, y = 1:5, z = c(1, 3, 2, NA, 5))
df
##   x y  z
## 1 1 1  1
## 2 1 2  3
## 3 1 3  2
## 4 1 4 NA
## 5 1 5  5
p <- ggplot(df, aes(x, y)) + geom_tile(aes(fill = z), size = 5)
p + 
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0))
  
# Make missing colours invisible
p + 
  scale_fill_gradient(na.value = NA) +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0))
# Customise on a black and white scale
p + 
  scale_fill_gradient(low = "black", high = "white", na.value = "red") +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0))
scale_fill_gradient函数scale_fill_gradient函数scale_fill_gradient函数

scale_fill_gradient函数

6.6.2.2 离散

离散数据有四个色标。我们用条形图来说明它们,该条形图将位置和填充都编码为相同的变量:

df <- data.frame(x = c("a", "b", "c", "d"), y = c(3, 4, 1, 2))
df
##   x y
## 1 a 3
## 2 b 4
## 3 c 1
## 4 d 2
bars <- df %>% 
  ggplot(aes(x,y,fill = x)) +
  geom_bar(stat = "identity") +
  theme(legend.position = "bottom") +
  scale_x_discrete(expand = c(0,0),NULL) +
  scale_y_continuous(NULL,expand = c(0,0))
bars
bars + scale_fill_hue(c = 40)
bars + scale_fill_hue(h = c(180, 300))
scale_fill_hue函数scale_fill_hue函数scale_fill_hue函数

scale_fill_hue函数

默认配色方案的一个缺点是,因为所有颜色都具有相同的亮度和色度,所以当您以黑白打印它们时,它们都显示为相同的灰色阴影

scale colour brewer() uses handpicked “ColorBrewer” colours, http://colorbrewer2.org/.

RColorBrewer::display.brewer.all()

bars + scale_fill_brewer(palette = "Set1")
bars + scale_fill_brewer(palette = "Set2")
bars + scale_fill_brewer(palette = "Accent")
scale_fill_brewer函数scale_fill_brewer函数scale_fill_brewer函数

scale_fill_brewer函数

bars + scale_fill_grey()
bars + scale_fill_grey(start = 0.5, end = 1)
bars + scale_fill_grey(start = 0, end = 0.5)
scale_fill_grey函数scale_fill_grey函数scale_fill_grey函数

scale_fill_grey函数

如果您有自己的离散调色板,则scale colour manual()很有用。以下示例显示了由Wes Anderson电影启发的调色板,如wesanderson包https://github.com/karthik/wesanderson 所提供的。这些并不是为感知均匀性而设计的,但是很有趣!

# install.packages("wesanderson")
library(wesanderson)
# wesanderson::wes_palettes
bars + scale_fill_manual(values = wes_palette("BottleRocket1"))
bars + scale_fill_manual(values = wes_palette("Rushmore"))
bars + scale_fill_manual(values = wes_palette("Rushmore"))
scale_fill_manual函数scale_fill_manual函数scale_fill_manual函数

scale_fill_manual函数

请注意,一组颜色不能同时满足所有用途的需要:明亮的颜色对于点很有效,但在条形上却是压倒性的。微妙的颜色对于条形效果很好,但在点上很难看到:

# Bright colours work best with points
df <- data.frame(x = 1:3 + runif(30), y = runif(30), z = c("a", "b", "c"))
point <- ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z)) +
  theme(legend.position = "none") +
  labs(x = NULL, y = NULL)

point + scale_colour_brewer(palette = "Set1")
point + scale_colour_brewer(palette = "Set2")
point + scale_colour_brewer(palette = "Pastel1")
scale_colour_brewer函数scale_colour_brewer函数scale_colour_brewer函数

scale_colour_brewer函数

# Subtler colours work better with areas
df <- data.frame(x = 1:3, y = 3:1, z = c("a", "b", "c"))
area <- ggplot(df, aes(x, y)) +
  geom_bar(aes(fill = z), stat = "identity") +
  theme(legend.position = "none") +
  labs(x = NULL, y = NULL)
area + scale_fill_brewer(palette = "Set1")
area + scale_fill_brewer(palette = "Set2")
area + scale_fill_brewer(palette = "Pastel1")
scale_fill_brewer函数scale_fill_brewer函数scale_fill_brewer函数

scale_fill_brewer函数

6.6.3 The Manual Discrete Scale

The following code demonstrates the use of scale colour manual():

plot <- ggplot(msleep, aes(brainwt, bodywt)) +
    scale_x_log10() +
    scale_y_log10()

plot +
  geom_point(aes(color = vore))
## Warning: Removed 27 rows containing missing values (geom_point).
plot +
    geom_point(aes(colour = vore)) +
    scale_colour_manual(
    values = c("red", "orange", "green", "blue"),
    na.value = "grey50"
)
## Warning: Removed 27 rows containing missing values (geom_point).
colours <- c(
    carni = "red",
    insecti = "orange",
    herbi = "green",
    omni = "blue"
)
plot +
    geom_point(aes(colour = vore)) +
    scale_colour_manual(values = colours)
## Warning: Removed 32 rows containing missing values (geom_point).
scale_colour_manual函数scale_colour_manual函数scale_colour_manual函数

scale_colour_manual函数

以下示例显示了创造性地在同一图上显示多个变量并显示有用的图例。在大多数其他绘图系统中,您需要为线条着色,然后添加图例:

huron <- data.frame(year = 1875:1972, level = as.numeric(LakeHuron))
ggplot(huron, aes(year)) +
    geom_line(aes(y = level + 5), colour = "red") +
    geom_line(aes(y = level - 5), colour = "blue")

ggplot(huron, aes(year)) +
    geom_line(aes(y = level + 5, colour = "above")) +
    geom_line(aes(y = level - 5, colour = "below"))

ggplot(huron,aes(year)) +
  geom_line(aes(y = level + 5,color = "above")) +
  geom_line(aes(y = level - 5,color = "below")) +
  labs(col = "Direction")

  ggplot(huron, aes(year)) +
    geom_line(aes(y = level + 5, colour = "above")) +
    geom_line(aes(y = level - 5, colour = "below")) +
    scale_colour_manual("Direction",
                        values = c("above" = "red", "below" = "blue")
)
scale_colour_manual函数scale_colour_manual函数

scale_colour_manual函数

6.6.4 The Identity Scale

head(luv_colours)
##          L             u         v           col
## 1 9341.570 -3.370649e-12    0.0000         white
## 2 9100.962 -4.749170e+02 -635.3502     aliceblue
## 3 8809.518  1.008865e+03 1668.0042  antiquewhite
## 4 8935.225  1.065698e+03 1674.5948 antiquewhite1
## 5 8452.499  1.014911e+03 1609.5923 antiquewhite2
## 6 7498.378  9.029892e+02 1401.7026 antiquewhite3
ggplot(luv_colours, aes(u, v)) +
    geom_point(aes(colour = col), size = 3) +
    scale_color_identity() +
    coord_equal()

6.6.5 练习

  1. Compare and contrast the four continuous colour scales with the four discrete scales.

  2. Explore the distribution of the built-in colors() using the luv colours dataset.