快捷搜索:  汽车  科技

r语言绘图配色方案(R语言配色包推荐)

r语言绘图配色方案(R语言配色包推荐)library(ggplot2) library(palmerpenguins) df1 <- data.frame(x = c("a" "b" "c" "d") y = c(3 5 7 6)) df2 <- penguinsggplot(df1 aes(x y fill=x)) geom_col() theme_bw() scale_fill_discrete_qualitative(palette = "warm")ggplot(df1 aes(x y fill=x)) geom_col() theme_bw() scale_fill_discrete_diverging(palette = "Vik")ggplot(df2 a

安装并加载colorspace包

使用install.packages()命令安装即可:

rm(list=ls()) #安装和加载colorspace包 install.packages("colorspace") library(colorspace)配色模板查看

colorspace包中共包括三种类型的配色模板——QualitativeSequentialDiverging,可以使用hcl_palettes()查看全部配色或某一类型配色:

hcl_palettes(plot = TRUE)

r语言绘图配色方案(R语言配色包推荐)(1)

hcl_palettes("qualitative" plot = TRUE)

r语言绘图配色方案(R语言配色包推荐)(2)

hcl_palettes("sequential (single-hue)" n = 7 plot = TRUE)

r语言绘图配色方案(R语言配色包推荐)(3)

hcl_palettes("sequential (multi-hue)" n = 7 plot = TRUE)

r语言绘图配色方案(R语言配色包推荐)(4)

hcl_palettes("diverging" n = 7 plot = TRUE)

r语言绘图配色方案(R语言配色包推荐)(5)

r语言绘图配色方案(R语言配色包推荐)(6)

获取特定模板中颜色的十六进制代码

通过diverging_hcl()、sequential_hcl()、qualitative_hcl()三个函数进行查看相应类型中的颜色十六进制代码,也可以通过swatchplot()函数进行可视化:

diverging_hcl(n=7 "Broc") ##"#002B4B" "#2F6489" "#92A6BA" "#F9F9F9" "#A6A587" "#636212" "#292800" swatchplot(diverging_hcl(n = 7 "Broc"))

r语言绘图配色方案(R语言配色包推荐)(7)

sequential_hcl(n = 7 palette = "Heat") ##"#8E063B" "#B9534C" "#DA8459" "#EEAB65" "#F6C971" "#F1DE81" "#E2E6BD" swatchplot(sequential_hcl(n = 7 palette = "Heat"))

r语言绘图配色方案(R语言配色包推荐)(8)

qualitative_hcl(7 palette = "Dark") ##"#C87A8A" "#B28955" "#82994C" "#30A37C" "#00A0AE" "#7E8FC7" "#BA7BB8" swatchplot(qualitative_hcl(7 palette = "Dark"))

r语言绘图配色方案(R语言配色包推荐)(9)

在ggplot2绘图过程中进行应用

主要通过scale_<aesthetic>_<datatype>_<colorscale>()函数实现,其中<aesthetic> 是指定映射 (fill color colour),<datatype> 是表明数据变量的类型 (discrete continuous binned),colorscale 是声明颜色标度类型 (qualitative sequential diverging divergingx),在绘图过程中使用 号将其跟在绘图函数后就行:

library(ggplot2) library(palmerpenguins) df1 <- data.frame(x = c("a" "b" "c" "d") y = c(3 5 7 6)) df2 <- penguins

ggplot(df1 aes(x y fill=x)) geom_col() theme_bw() scale_fill_discrete_qualitative(palette = "warm")

r语言绘图配色方案(R语言配色包推荐)(10)

ggplot(df1 aes(x y fill=x)) geom_col() theme_bw() scale_fill_discrete_diverging(palette = "Vik")

r语言绘图配色方案(R语言配色包推荐)(11)

ggplot(df2 aes(bill_length_mm fill = species)) geom_density(alpha = 0.6) scale_fill_discrete_sequential(palette = "Heat" nmax = 6 rev = FALSE order = 2:4)

r语言绘图配色方案(R语言配色包推荐)(12)

ggplot(df2 aes(bill_length_mm fill = species)) geom_density(alpha = 0.6) scale_fill_discrete_sequential(palette = "Reds 3" nmax = 6 rev = FALSE order = 2:4)

r语言绘图配色方案(R语言配色包推荐)(13)

参考:https://bookdown.org/wangminjie/R4DS/tidyverse-ggplot2-colors.html#tidyverse-ggplot2-colors

猜您喜欢: