ggplot2 调色板
这两个数据集将用于生成下面的各种图。
# Two variables
df <- read.table(header=TRUE, text='
cond yval
A 2
B 2.5
C 1.6
')
# Three variables
df2 <- read.table(header=TRUE, text='
cond1 cond2 yval
A I 2
A J 2.5
A K 1.6
B I 2.2
B J 2.4
B K 1.2
C I 1.7
C J 2.3
C K 1.9
')
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. 离散性变量颜色
# 1.1 ggplot2默认的颜色选择。
对于离散型变量,ggplot2默认通过scale_fill_hue()
和 scale_colour_hue()
函数在HSL色环周围均匀选择颜色。
例如,如果有两种颜色,那么它们将从圆上的相对点中选择;如果有三种颜色,它们将在色环上相距 120°;等等。用于不同级别数量的颜色如下所示:
在绘图过程中,默认不需要显式地调用scale_fill_hue()
和 scale_colour_hue()
函数。
# These two are equivalent; by default scale_fill_hue() is used
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity")
# ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") + scale_fill_hue()
# These two are equivalent; by default scale_colour_hue() is used
ggplot(df, aes(x=cond, y=yval, colour=cond)) + geom_point(size=2)
# ggplot(df, aes(x=cond, y=yval, colour=cond)) + geom_point(size=2) + scale_colour_hue()
2
3
4
5
6
7
8
# 1.2. 设置颜色亮度和色彩饱和度
显式调用scale_fill_hue()
和 scale_colour_hue()
函数可以设置legend 标题,可以设置颜色亮度和色度(默认亮度65, 色度100)
# Use luminance=45, instead of default 65
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_hue(l=40)
# Reduce saturation (chromaticity) from 100 to 50, and increase luminance
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_hue(c=45, l=80)
# Note: use scale_colour_hue() for lines and points
2
3
4
5
6
7
8
9
# 1.3. RColorBrewer调色板
使用scale_fill_brewer()
和 scale_colour_brewer()
函数可以在ggplot2中调用RColorBrewer的调色板。
palette
可以指定具体的名字,e.g.palette = "Spectral
- 也可以配合
type
一起使用, e.g.palette = 3, type = "div"
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer()
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer(palette="Set1")
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer(palette="Spectral")
# Note: use scale_colour_brewer() for lines and points
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer(palette = 3, type = "div")
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer(palette = 2, type = "qua")
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_brewer(palette = 1, type = "seq")
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
RColorBrewer调色板如下图所示:
# 1.4. 手动指定颜色
使用scale_fill_manual()
和 scale_colour_manual()
函数可以手动的指定颜色代码。
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_manual(values=c("red", "blue", "green"))
ggplot(df, aes(x=cond, y=yval, fill=cond)) + geom_bar(stat="identity") +
scale_fill_manual(values=c("#CC6666", "#9999CC", "#66CC99"))
# Note: use scale_colour_manual() for lines and points
2
3
4
5
6
7
# 2. 连续性变量颜色
# Generate some data
set.seed(133)
df <- data.frame(xval=rnorm(50), yval=rnorm(50))
# Make color depend on yval
ggplot(df, aes(x=xval, y=yval, colour=yval)) + geom_point()
# Use a different gradient
ggplot(df, aes(x=xval, y=yval, colour=yval)) + geom_point() +
scale_colour_gradientn(colours=rainbow(4))
2
3
4
5
6
7
8
9
10
# 3. 色盲友好型调色板
ggplot2 中的默认颜色可能很难相互区分,因为它们具有相同的亮度。它们对色盲观众也不友好。一个好的通用解决方案是使用色盲友好型
调色板。
该色板来自 http://jfly.iam.u-tokyo.ac.jp/color/ (opens new window)
# The palette with grey:
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# The palette with black:
cbbPalette <- c("#000000", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
# To use for fills, add
scale_fill_manual(values=cbPalette)
# To use for line and point colors, add
scale_colour_manual(values=cbPalette)
2
3
4
5
6
7
8
9
10
11
# 4. ggsci调色板
ggsci
提供了一系列高质量的调色板,其灵感来自科学期刊、数据可视化库、科幻电影和电视节目中使用的颜色。
ggsci
中的调色板可用作ggplot2
图层的scale,所有调色板,相应的命名为:
- scale_color_palname()
- scale_color_palname()
下表总结了所有可用的调色板:
Name | Scales | Palette Types | Palette Generator |
---|---|---|---|
NPG | scale_color_npg() scale_fill_npg() | "nrc" | pal_npg() |
AAAS | scale_color_aaas() scale_fill_aaas() | "default" | pal_aaas() |
NEJM | scale_color_nejm() scale_fill_nejm() | "default" | pal_nejm() |
Lancet | scale_color_lancet() scale_fill_lancet() | "lanonc" | pal_lancet() |
JAMA | scale_color_jama() scale_fill_jama() | "default" | pal_jama() |
JCO | scale_color_jco() scale_fill_jco() | "default" | pal_jco() |
UCSCGB | scale_color_ucscgb() scale_fill_ucscgb() | "default" | pal_ucscgb() |
D3 | scale_color_d3() scale_fill_d3() | "category10" "..." | pal_d3() |
LocusZoom | scale_color_locuszoom() scale_fill_locuszoom() | "default" | pal_locuszoom() |
IGV | scale_color_igv() scale_fill_igv() | "default" "alternating" | pal_igv() |
UChicago | scale_color_uchicago() scale_fill_uchicago() | "default" "light" "dark" | pal_uchicago() |
Star Trek | scale_color_startrek() scale_fill_startrek() | "uniform" | pal_startrek() |
Tron Legacy | scale_color_tron() scale_fill_tron() | "legacy" | pal_tron() |
Futurama | scale_color_futurama() scale_fill_futurama() | "planetexpress" | pal_futurama() |
Rick and Morty | scale_color_rickandmorty() scale_fill_rickandmorty() | "schwifty" | pal_rickandmorty() |
The Simpsons | scale_color_simpsons() scale_fill_simpsons() | "springfield" | pal_simpsons() |
GSEA | scale_color_gsea() scale_fill_gsea() | "default" | pal_gsea() |
Material Design | scale_color_material() scale_fill_material() | "red" "pink" "..." | pal_material() |
下面展示ggsci调色板的使用
library("ggplot2")
library("ggsci")
library(patchwork)
data("diamonds")
p1 = ggplot(subset(diamonds, carat >= 2.2),
aes(x = table, y = price, colour = cut)) +
geom_point(alpha = 0.7) +
geom_smooth(method = "loess", alpha = 0.05, size = 1, span = 1) +
theme_bw()
p2 = ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),
aes(x = depth, fill = cut)) +
geom_histogram(colour = "black", binwidth = 1, position = "dodge") +
theme_bw()
p1 + p2
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 4.1 NPG调色板
NPG调色板灵感来源于nature 出版集团
p1_npg = p1 + scale_color_npg()
p2_npg = p2 + scale_fill_npg()
p1_npg + p2_npg
2
3
4
# 4.2 JCO调色板
JCO调色板灵感来源于JCO杂志
p1_jco = p1 + scale_color_jco()
p2_jco = p2 + scale_fill_jco()
p1_jco + p2_jco
2
3
4
# 5. Viridis调色板
原本出自viridis package (opens new window), 在最新版本的ggplot2已经可以直接使用。
# 6. scico调色板
scico是一款基于科学色图的R调色板,scico为ggplot2提供了颜色标尺,用法与Viridis调色板类似
library(scico)
# 展示所有可用色板
scico_palette_show()
# 取色
scico(30, palette = 'lapaz')
#[1] "#190C65" "#1D196C" "#1E2575" "#202F7D" "#223A85" "#25448B" "#274E92" "#2A5898" "#2E629D"
#[10] "#336CA1" "#3774A3" "#3F7DA5" "#4886A6" "#528EA6" "#5F95A5" "#6C9AA3" "#7A9E9F" "#87A19A"
#[19] "#95A494" "#A2A58F" "#ADA78B" "#BBA989" "#CAAD8A" "#DBB592" "#EBC0A0" "#F6CCB0" "#FBD7C2"
#[28] "#FDE0D2" "#FFEAE2" "#FFF2F2"
2
3
4
5
6
7
8
9
10
11
12
library(ggplot2)
volcano <- data.frame(
x = rep(seq_len(ncol(volcano)), each = nrow(volcano)),
y = rep(seq_len(nrow(volcano)), ncol(volcano)),
height = as.vector(volcano)
)
ggplot(volcano, aes(x = x, y = y, fill = height)) +
geom_raster() +
scale_fill_scico(palette = 'davos')
2
3
4
5
6
7
8
9
10
# 7. cols4all调色板(终极解决方案)
cols4all
是一个用于选择调色板的终极解决方案。colors for all
是这个R包的使命,即颜色不仅应用于具有正常色觉的人,
而且还适用于具有色觉缺陷的人。目前,该R包整合了几个流行和鲜为人知的系列调色板: ColorBrewer,Viridis,Kovesi,Paul Tol,
Scico,Carto,Tableau,Wes Anderson和Seaborn。此外,cols4all
还可以添加自定义的调色板。
# 调色板名称及其属性
c4a_palettes
获取调色板名称c4a_series
获取色板系列名称c4a_info
获取色板详细信息
c4a_series()
c4a_palettes(series = "brewer")
# find names of hcl palettes that are diverging
c4a_palettes(type = "div", series = "hcl")
# select purple green palette from the hcl series:
c4a("hcl.purple_green", 11)
c4a("br_bg", n = 7)
2
3
4
5
6
7
8
9
10
11
# ggplot2标尺
scale_