文章目录
- 颜色代码大全
- R语言中的调色板
- RColorBrewer提供的调色盘
- R语言中配色介绍
- R语言中自带的调色板
- RColorBrewer包提供更多的调色板
- ggplot2中配色系统的介绍
- 数值型配色
- 类别型配色
- scale_colour/fill_brewer()
- 用法
- scale_colour/fill_manual()
- ggsci为ggplot2提供期刊配色
颜色代码大全
R语言中的调色板
palette(value) # obtain the curren palette or set the palette
palette.pals() # give the names of predefined palettes
palette.colors() # return a vector of R colors
RColorBrewer提供的调色盘
其他颜色集可以从R包RColorBrewer中获取,利用命令display.brewer.all()可以显示出该包中全部的颜色集:
library(RColorBrewer)
display.brewer.all()
palete()#设置全局调色盘
brewer.pal(n, name)#调用相应调色盘的颜色
R语言中配色介绍
R语言作图中颜色的调整往往是通过给参数col
赋值来实现的,其接受的类型有:数字、颜色名、rgb()、十六进制。
R语言中通过很多调色板,帮助提供配色方案。
R语言中自带的调色板
R语言中自带的调色板有hcl, rainbow, heat, terrain, topo, cm
等, 用法如下:
hcl.colors(n, palette = "viridis", alpha = NULL, rev = FALSE, fixup = TRUE)
hcl.pals(type = NULL)
rainbow(n, s = 1, v = 1, start = 0, end = max(1, n - 1)/n, alpha, rev = FALSE)
heat.colors(n, alpha, rev = FALSE)
terrain.colors(n, alpha, rev = FALSE)
topo.colors(n, alpha, rev = FALSE)
cm.colors(n, alpha, rev = FALSE)
RColorBrewer包提供更多的调色板
RColorBrewer包提供了按照 序列型(sequential)、离散型(diverging)、分类型(qualitative) 提供的三种类型的调色盘。利用display.brewer.all()
可以展示出全部的调色板样式:
随后我们通过brewer.pal(n, name)
函数就可以调用相应name的调色板了,例如:
ggplot2中配色系统的介绍
ggplot2包中对配色系统主要通过以下几个函数实现:
- 数值型
- scale_colour/fill_coninuous()
- scale_fill_distiller()
- scale_colour/fill_gradient()
- scale_colour/fill_gradient2()
- scale_colour/fill_gradientn()
- 类别型
- scale_colour/fill_discrete()
- scale_colour/fill_brewer()
- scale_colour/fill_manual()
其中,colour表示轮廓色度量,fill表示填充色度量
数值型配色
类别型配色
scale_colour/fill_brewer()
用法
scale_colour_brewer(
...,
type = "seq",
palette = 1,
direction = 1,
aesthetics = "colour"
)
scale_fill_brewer(
...,
type = "seq",
palette = 1,
direction = 1,
aesthetics = "fill"
)
参数介绍:
… Other arguments passed on to discrete_scale(),
continuous_scale(), or binned_scale(), for brewer, distiller, and
fermenter variants respectively, to control name, limits, breaks,
labels and so forth.type One of seq (sequential), div (diverging) or qual (qualitative)
palette If a string, will use that named palette. If a number, will
index into the list of palettes of appropriate type. The list of
available palettes can found in the Palettes section.direction Sets the order of colours in the scale. If 1, the default,
colours are as output by RColorBrewer::brewer.pal(). If -1, the order
of colours is reversed.aesthetics Character string or vector of character strings listing
the name(s) of the aesthetic(s) that this scale works with. This can
be useful, for example, to apply colour settings to the colour and
fill aesthetics at the same time, via aesthetics = c(“colour”,
“fill”).values if colours should not be evenly positioned along the gradient
this vector gives the position (between 0 and 1) for each colour in
the colours vector. See rescale() for a convenience function to map an
arbitrary range to between 0 and 1.space colour space in which to calculate gradient. Must be “Lab” -
other values are deprecated.na.value Colour to use for missing values
guide Type of legend. Use “colourbar” for continuous colour bar, or
“legend” for discrete colour legend.
scale_colour/fill_brewer()是利用已有的颜色集进行配色的,ggplot2中自带的颜色集有:
其他颜色集可以从R包RColorBrewer中获取,利用命令display.brewer.all()可以显示出该包中全部的颜色集:
library(RColorBrewer)
display.brewer.all()
scale_colour/fill_manual()
https://zhuanlan.zhihu.com/p/80849532
ggsci为ggplot2提供期刊配色
ggsci是一款优秀的ggplot2的配色包,包含了常见杂志的配色,比如lancet,jco,jama,nejm等,主要通过以下2句代码进行配色:
scale_folor_palname
scale_fill_palname
可参考:https://zhuanlan.zhihu.com/p/454703155