使用install.packages()命令安装即可:
rm(list=ls())
#安装和加载colorspace包
install.packages("colorspace")
library(colorspace)
colorspace包中共包括三种类型的配色模板——Qualitative、Sequential、Diverging,可以使用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"))
主要通过scale__
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
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号