其他
R可视化07|ggplot2图层-标度图层(scale layer)-颜色盘篇
"pythonic生物人"的第105篇分享
国庆第7篇,祝大家工作学习生活顺利!!
本文详细介绍ggplot2中颜色标度(Colour scales and legends)。
续前篇:
本文目录
3、颜色标度和图例(Colour scales and legends)
ggplot2颜色理论简介
连续型颜色标度(Continuous colour scales)
离散型颜色标度(Discrete colour scales)
箱型颜色标度(Binned colour scales)
3、颜色标度和图例(Colour scales and legends)
ggplot2颜色理论简介
ggplot2使用了一种HCL色彩空间(Hue-Chroma-Luminance),由三个部分构成:
色相(hue):(0~360)的角度值,将一种色彩赋予颜色属性(如红橙黄蓝等等); 彩度(chroma):颜色的明暗程度,即看其接近黑色或白色的程度,明度0是黑色,1是白色; 明度(luminance):色彩的纯度。0是灰色,彩度的最大值随明度变化而不同。
下图为HCL色彩空间形状,色相被映射成一个角度,彩度被映射成半径,每个分面展示一种明度,每个分面中心展示为灰色,离边缘越近颜色越浓烈,明度为0和100分别为一个黑点和白点:
连续型颜色标度(Continuous colour scales)
介绍ggplot2中可用的连续型调色盘。该部分使用数据集faithful,该数据集记录黄石公园一眼古泉每次喷发时间(eruptions)及等待喷发时间(waiting),使用2d核密度估计图展示效果(2d density estimate)。
> head(faithful,10)
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
7 4.700 88
8 3.600 85
9 1.950 51
10 4.350 85
options(repr.plot.width = 6, repr.plot.height = 28, repr.plot.res = 300)
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")
p1 <- erupt
#scale_fill_viridis_c()和scale_fill_distiller()
p2 <- erupt + scale_fill_viridis_c()
p3 <- erupt + scale_fill_viridis_c(option = "magma")
#
p4 <- erupt + scale_fill_distiller()
p5 <- erupt + scale_fill_distiller(palette = "RdPu")
p6 <- erupt + scale_fill_distiller(palette = "YlOrBr")
#scico颜色盘
library(scico)
p7 <- erupt + scico::scale_fill_scico(palette = "bilbao") # the default
p8 <- erupt + scico::scale_fill_scico(palette = "vik")
p9 <- erupt + scico::scale_fill_scico(palette = "lajolla")
#paletteer、gameofthrones颜色盘
library(paletteer)
library(gameofthrones)
p10 <- erupt + paletteer::scale_fill_paletteer_c("viridis::plasma")
p11 <- erupt + paletteer::scale_fill_paletteer_c("scico::tokyo")
p12 <- erupt + paletteer::scale_fill_paletteer_c("gameofthrones::targaryen")
#scale_fill_gradient() 双色梯度,low和high分别控制两端颜色
#scale_fill_gradient2() 三色梯度,low、mid和high分别控制两端颜色及中间色,midpoint设置中间值,默认为0,可使用其他任意色。
#scale_fill_gradientn() 自定义n色梯度,colours和values参数控制颜色分布
p13 <- erupt + scale_fill_gradient(low = "grey", high = "brown")
p14 <- erupt + scale_fill_gradient2(low = "grey", mid = "white", high = "brown", midpoint = .02)
p15 <- erupt + scale_fill_gradientn(colours = terrain.colors(7))
#使用munsell调色盘
library('munsell')
#查看munsell可用色带
p16 <- munsell::hue_slice("5P") +
annotate(
geom = "segment",
x = c(7, 7),
y = c(1, 10),
xend = c(7, 7),
yend = c(2, 9),
arrow = arrow(length = unit(2, "mm"))
)
p17 <- erupt + scale_fill_gradient(
low = munsell::mnsl("5P 2/12"),
high = munsell::mnsl("5P 7/12")
)
p18 <- erupt + scale_fill_gradient2(
low = munsell::mnsl("5B 7/8"),
high = munsell::mnsl("5Y 7/8"),
mid = munsell::mnsl("N 7/0"),
midpoint = .02
)
#使用colorspace调色盘
library('colorspace')
p19 <- erupt + scale_fill_gradientn(colours = colorspace::heat_hcl(7))
p20 <- erupt + scale_fill_gradientn(colours = colorspace::diverge_hcl(7))
#缺省值处理
df <- data.frame(x = 1, y = 1:5, z = c(1, 3, 2, NA, 5))
base <- ggplot(df, aes(x, y)) +
geom_tile(aes(fill = z), size = 5) +
labs(x = NULL, y = NULL)
p21 <- base
p22 <- base + scale_fill_gradient(na.value = NA)
p23 <- base + scale_fill_gradient(na.value = "yellow")#缺失值设置为黄色
toy <- data.frame(
const = 1,
up = 1:4,
txt = letters[1:4],
big = (1:4)*1000,
log = c(2, 5, 10, 2000)
)
leg <- ggplot(toy, aes(up, up, fill = big)) +
geom_tile() +
labs(x = NULL, y = NULL)
p24 <- leg + scale_fill_continuous(breaks = NULL)
p25 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18,p19,p20,p21,p22,p23,p24,nrow = 12)
ggsave("scale5.png", p25, width = 8, height = 28)
离散型颜色标度(Discrete colour scales)
ggplot2绘图时,如何指定单颜色,如grey,red等等。
options(repr.plot.width = 6, repr.plot.height = 10, repr.plot.res = 300)
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")
# scale_fill_discrete()默认颜色
p1 <- bars
p2 <- bars + scale_fill_discrete()
#scale_fill_hue()默认颜色
p3 <- bars + scale_fill_hue()
#scale_colour_brewer() ,类似于Matplotlib中的默认色带
p4 <- bars + scale_fill_brewer(palette = "Set1")
p5 <- bars + scale_fill_brewer(palette = "Set2")
p6 <- bars + scale_fill_brewer(palette = "Accent")
#Hue颜色,h, c和l三参数控制颜色
p7 <- bars
p8 <- bars + scale_fill_hue(c = 40)
p9 <- bars + scale_fill_hue(h = c(180, 300))
#grey颜色,0为黑色,1为白色
p10 <- bars + scale_fill_grey()
p11 <- bars + scale_fill_grey(start = 0.5, end = 1)
p12 <- bars + scale_fill_grey(start = 0, end = 0.5)
#自己传入颜色名称
p13 <- bars + scale_fill_manual(values = c("sienna1", "sienna4", "hotpink1", "hotpink4"))
p14 <- bars + scale_fill_manual(values = c("tomato1", "tomato2", "tomato3", "tomato4"))
p15 <- bars + scale_fill_manual(values = c("grey", "black", "grey", "grey"))
p16 <- grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,nrow = 5)
ggsave("scale6.png", p16, width = 8, height = 10)
RColorBrewer 中可用的所有离散型色度条。
RColorBrewer::display.brewer.all()
箱型颜色标度(Binned colour scales)
options(repr.plot.width = 6, repr.plot.height = 10, repr.plot.res = 300)
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")
p0 <- erupt
#scale_fill_binned
p1 <- erupt + scale_fill_binned()
#scale_fill_steps
p2 <- erupt + scale_fill_steps()
p3 <- erupt + scale_fill_steps(n.breaks = 8)
p4 <- erupt + scale_fill_steps(low = "grey", high = "brown")
#scale_fill_steps2
p5 <- erupt + scale_fill_steps2(low = "grey", mid = "white", high = "brown", midpoint = .02)
#scale_fill_stepsn
p6 <- erupt + scale_fill_stepsn(n.breaks = 12, colours = terrain.colors(12))
#使用scale_fill_fermenter
p7 <- erupt + scale_fill_fermenter(n.breaks = 9)
p8 <- erupt + scale_fill_fermenter(n.breaks = 9, palette = "Oranges")
p9 <- erupt + scale_fill_fermenter(n.breaks = 9, palette = "PuOr")
p10 <- grid.arrange(p0,p1,p2,p3,p4,p5,p6,p7,p8,p9,nrow = 5)
ggsave("scale7.png", p16, width = 8, height = 10)
本文结束,更多好文:
Python绘图颜色系列:
其它:
有意见请移步到QQ群629562529反馈,一起进步哈!