R语言配色包推荐-colorspace

安装并加载colorspace包

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

rm(list=ls())


#安装和加载colorspace包
install.packages("colorspace")
library(colorspace)

配色模板查看

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

hcl_palettes(plot = TRUE)

hcl_palettes("qualitative", plot = TRUE)

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

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

hcl_palettes("perging", n = 7, plot = TRUE)

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

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

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


swatchplot(perging_hcl(n = 7, "Broc"))

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


swatchplot(sequential_hcl(n = 7, palette = "Heat"))

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


swatchplot(qualitative_hcl(7, palette = "Dark"))

在ggplot2绘图过程中进行应用

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

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

ggplot(df1, aes(x,y,fill=x))+
  geom_col()+
  theme_bw()+
  scale_fill_discrete_perging(palette = "Vik")

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)

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)

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

展开阅读全文

页面更新:2024-03-20

标签:标度   变量   函数   中共   加载   命令   颜色   模板   类型   语言   代码

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号

Top