绘制渐变图时,有一些调色板可以直接使用,这里介绍Wes Anderson调色盘,以及在R中的实现
devtools::install_github("karthik/wesanderson")
library(wesanderson)
names(wes_palettes)
## [1] "BottleRocket1" "BottleRocket2" "Rushmore1" "Royal1"
## [5] "Royal2" "Zissou1" "Darjeeling1" "Darjeeling2"
## [9] "Chevalier1" "FantasticFox1" "Moonrise1" "Moonrise2"
## [13] "Moonrise3" "Cavalcanti1" "GrandBudapest1" "GrandBudapest2"
产生颜色向量的主要函数为
wes_palette(name, n, type = c("discrete", "continuous"))
name
: 调色名称n
: 颜色数据,大部分只有4-5个颜色type
: “continuous” 或者 “discrete”.
library("ggplot2")
# Box plot
bp <- ggplot(iris, aes(Species, Sepal.Length)) +
geom_boxplot(aes(fill = Species)) +
theme_minimal() +
theme(legend.position = "top")
bp
# Scatter plot
sp <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) +
geom_point(aes(color = Species)) +
theme_minimal()+
theme(legend.position = "top")
library(wesanderson)
# Discrete color
bp + scale_fill_manual(values = wes_palette("GrandBudapest1", n = 3))
# Gradient color
pal <- wes_palette("Zissou1", 100, type = "continuous")
ggplot(heatmap, aes(x = X2, y = X1, fill = value)) +
geom_tile() +
scale_fill_gradientn(colours = pal) +
scale_x_discrete(expand = c(0, 0)) +
scale_y_discrete(expand = c(0, 0)) +
coord_equal()
单独绘制colorbar图,以我自己的数据为例
library(wesanderson)
pal <- wes_palette("Zissou1", 100, type = "continuous")
ggplot(new_id,aes(x=Group,y=1,fill=HR))+
geom_bar(position="stack", stat="identity")+
#scale_fill_grey(start = 0.8, end = 0.2)
scale_fill_gradientn(colours = pal) +
theme_classic()+coord_flip()
可单独进行编辑使用
更多资料详见:
https://www.datanovia.com/en/blog/top-r-color-palettes-to-know-for-great-data-visualization/