1. ggsci介紹
ggsci提供了一系列配置好的顏色給ggplot2繪制出的圖片調(diào)色赦政,包括一些知名雜志期刊或者軟件甚至是知名科幻電影、動畫等的經(jīng)典配色風(fēng)格泳叠。將這些配色風(fēng)格直接應(yīng)用于我們繪制出的圖作瞄,方便又好看。
# 下載
# download ggsci package
install.packages("ggsci")
#install.packages("devtools")
devtools::install_github("nanxstats/ggsci")
對所有的調(diào)色板危纫,使用方法:p+scale_color_xx()
和p+scale_fill_xx()
宗挥。
p是繪制好的ggplot圖,xx指調(diào)色板的名字
所有調(diào)色板的名字:npg, aaas, nejm, lancet, jama, jco, ucscgb, d3, locuszoom, igv, cosmic, uchicago, startrek, tron, futurama, rickandmorty, simpsons, gsea, material
2. ggsci使用
2.1 離散型調(diào)色板种蝶,應(yīng)用于離散型數(shù)值
使用diamond數(shù)據(jù)集做演示契耿,先使用diamond數(shù)據(jù)集繪制兩張圖
library("ggsci")
library("ggplot2")
library("gridExtra")
library("patchwork")
data("diamonds")
p1 <- ggplot(subset(diamonds, carat >= 2.2),
aes(x = table, y = price, colour = cut)
) + geom_point(alpha = 0.7) +
geom_smooth(method = "loess", alpha = 0.05, size = 1, span = 1) +
theme_bw()
p2 <- ggplot(subset(diamonds, carat > 2.2 & depth > 55 & depth < 70),aes(x = depth, fill = cut)
) +geom_histogram(colour = "black", binwidth = 1, position = "dodge") +theme_bw()
p1|p2
- NPG(將NPG雜志的配色風(fēng)格應(yīng)用到剛剛繪制的兩張圖)
p1_npg <- p1 + scale_color_npg()
p2_npg <- p2 + scale_fill_npg()
grid.arrange(p1_npg, p2_npg, ncol = 2)
顏色提取
show_col(pal_npg("nrc", alpha = 0.6)(10))
##上面palette=c('nrc),所以取顏色時pal_npg()第一個參數(shù)就是‘nrc’螃征。如果是‘Default’就填‘Default’搪桂,其它的函數(shù)都是一樣的取法。
- AAAS(使用AAAS雜志的配色風(fēng)格)
p1_aaas <- p1 + scale_color_aaas()
p2_aaas <- p2 + scale_fill_aaas()
grid.arrange(p1_aaas, p2_aaas, ncol = 2)
- NEJM(新英格蘭配色風(fēng)格)
p1_nejm <- p1 + scale_color_nejm()
p2_nejm <- p2 + scale_fill_nejm()
grid.arrange(p1_nejm, p2_nejm, ncol = 2)
- Lancet
p1_lancet <- p1 + scale_color_lancet()
p2_lancet <- p2 + scale_fill_lancet()
grid.arrange(p1_lancet, p2_lancet, ncol = 2)
- JAMA
p1_jama <- p1 + scale_color_jama()
p2_jama <- p2 + scale_fill_jama()
grid.arrange(p1_jama, p2_jama, ncol = 2)
- JCO
p1_jco <- p1 + scale_color_jco()
p2_jco <- p2 + scale_fill_jco()
grid.arrange(p1_jco, p2_jco, ncol = 2)
- UCSCGB
p1_ucscgb <- p1 + scale_color_ucscgb()
p2_ucscgb <- p2 + scale_fill_ucscgb()
grid.arrange(p1_ucscgb, p2_ucscgb, ncol = 2)
- D3
p1_d3 <- p1 + scale_color_d3()
p2_d3 <- p2 + scale_fill_d3()
grid.arrange(p1_d3, p2_d3, ncol = 2)
- LocusZoom
p1_locuszoom <- p1 + scale_color_locuszoom()
p2_locuszoom <- p2 + scale_fill_locuszoom()
grid.arrange(p1_locuszoom, p2_locuszoom, ncol = 2)
- IGV
p1_igv_default <- p1 + scale_color_igv()
p2_igv_default <- p2 + scale_fill_igv()
grid.arrange(p1_igv_default, p2_igv_default, ncol = 2)
- UChicago
p1_uchicago <- p1 + scale_color_uchicago()
p2_uchicago <- p2 + scale_fill_uchicago()
grid.arrange(p1_uchicago, p2_uchicago, ncol = 2)
- Star Trek
p1_startrek <- p1 + scale_color_startrek()
p2_startrek <- p2 + scale_fill_startrek()
grid.arrange(p1_startrek, p2_startrek, ncol = 2)
- Tron Legend
p1_tron <- p1 + theme_dark() + theme(
panel.background = element_rect(fill = "#2D2D2D"),
legend.key = element_rect(fill = "#2D2D2D")
) +
scale_color_tron()
p2_tron <- p2 + theme_dark() + theme(
panel.background = element_rect(fill = "#2D2D2D")
) +
scale_fill_tron()
grid.arrange(p1_tron, p2_tron, ncol = 2)
- Futurama
p1_futurama <- p1 + scale_color_futurama()
p2_futurama <- p2 + scale_fill_futurama()
grid.arrange(p1_futurama, p2_futurama, ncol = 2)
- Rick and Morty(我非常喜歡的動漫)
p1_rickandmorty <- p1 + scale_color_rickandmorty()
p2_rickandmorty <- p2 + scale_fill_rickandmorty()
grid.arrange(p1_rickandmorty, p2_rickandmorty, ncol = 2)
- The Simpsons
p1_simpsons <- p1 + scale_color_simpsons()
p2_simpsons <- p2 + scale_fill_simpsons()
grid.arrange(p1_simpsons, p2_simpsons, ncol = 2)
2.2 連續(xù)型調(diào)色板
使用mtcars數(shù)據(jù)集做演示
library("reshape2")
data("mtcars")
cor <- cor(unname(cbind(mtcars, mtcars, mtcars, mtcars)))
cor_melt <- melt(cor)
p3 <- ggplot(
cor_melt,
aes(x = Var1, y = Var2, fill = value)
) +
geom_tile(colour = "black", size = 0.3) +
theme_bw() +
theme(
axis.title.x = element_blank(),
axis.title.y = element_blank()
)
- GSEA
將GSEA的配色風(fēng)格應(yīng)用到剛剛繪制的圖
p3_gsea <- p3 + scale_fill_gsea()
p3_gsea_inv <- p3 + scale_fill_gsea(reverse = TRUE)
grid.arrange(p3_gsea, p3_gsea_inv, ncol = 2)