科研绘图的主题与颜色搭配

主题

除了 theme_bw() 以外,下面主题也适合科研绘图。
ggplot2 自带主题:https://ggplot2.tidyverse.org/reference/ggtheme.html
cowplot 包主题:https://wilkelab.org/cowplot/articles/themes.html
ggthemes 包主题:https://yutannihilation.github.io/
allYourFigureAreBelongToUs/ggthemes/
ggthemr 包主题:https://github.com/cttobin/ggthemr
其中后两个包中的主题可用于日常数据展示,不适合科研发表文章。
ggplot2 自带主题我常用的是 theme_bw() 、theme_few() 、theme_cowplot() 和 theme_minimal()

library(tidyverse)
library(ggsci)
library(cowplot)
data("diamonds")
set.seed(1000)
small_diamonds <- sample_n(diamonds, size = 500)
head(small_diamonds)
p <- ggplot(data = small_diamonds, aes(x = carat, y = price)) +
  geom_point(shape = 21, size = 4, 
             color = 'black', aes(fill = cut)) +
  scale_fill_npg() +
  labs(title = 'point plot', 
       x = 'weight of the diamond ', 
       y = 'price in US dollars',
       fill = 'quality of the cut') +
  scale_x_continuous(breaks = seq(0,3,0.5)) +
  scale_y_continuous(breaks = seq(0, 15000, 5000), 
                     labels = c('0', '5K', '10K', '15K'))
  
p1 <- p +theme_bw() + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.22, 0.7)) +
  ggtitle('theme_bw')

p2 <- p + theme_classic() + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.22, 0.7)) +
  ggtitle('theme_classic')

p3 <- p + theme_test() + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.22, 0.7)) +
  ggtitle('theme_test')

p4 <- p + theme_minimal() + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.22, 0.7)) +
  ggtitle('theme_minimal')

p5 <- p + theme_void() + 
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.22, 0.7)) +
  ggtitle('theme_void')

cowplot 包中常用的主题如下

p6 <- p +theme_half_open() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.04, 0.7)) +
  ggtitle('theme_half_open')

p7 <- p +theme_minimal_grid() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.04, 0.7)) +
  ggtitle('theme_minimal_grid')

p8 <- p +theme_minimal_hgrid() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.04, 0.7)) +
  ggtitle('theme_minimal_hgrid')

p9 <- p +theme_minimal_vgrid() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.04, 0.7)) +
  ggtitle('theme_minimal_vgrid')

plot_grid(p1, p2, p3, p4, p5, p6, p7, p8, p9, labels = LETTERS)
Rplot.png

自定义主题

自定义主题代码学习起来稍显复杂,建议通过 ggThemeAssist包来搞定。将待修改的图存在变量中,选中变量,在 Addins 中选择 ggThemeAssist 进行调整,会自动生成代码。

p + theme(axis.title = element_text(size = 15), 
    plot.title = element_text(hjust = 0.5), 
    legend.key = element_rect(fill = NA), 
    legend.background = element_rect(fill = NA), 
    legend.position = c(0.085, 0.7)) +
  labs(title = "I am title", subtitle = "I am subtitle", 
    caption = "I am caption")
Rplot.png

颜色搭配

小技巧:可以使用 scales 包中的 show_col() 显示颜色

library(scales)
my_cols <- c('#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00')
show_col(my_cols)
Rplot.png

离散型变量

如果自定义颜色,可以到使用 Colour Picker 来选择颜色;或者到 http://colorbrewer2.org/ 挑选颜色;除此之外,我们可以使用 RColorBrewer 和 ggsci 包中定义的调色板。

library(ggplot2)
library(cowplot)
p <- ggplot(data = small_diamonds, aes(x = carat, y = price)) +
  geom_point(shape = 21, size = 4, 
             color = 'black', aes(fill = cut)) +
  labs(x = 'weight of the diamond ', 
       y = 'price in US dollars',
       fill = 'quality of the cut') +
  scale_x_continuous(breaks = seq(0,3,0.5)) +
  scale_y_continuous(breaks = seq(0, 15000, 5000), 
                     labels = c('0', '5K', '10K', '15K')) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.1, 0.8)) +
  ggtitle('')
p
Rplot.png

自定义颜色

library(scales)
my_cols <- c('#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00')
show_col(my_cols)
p + scale_fill_manual(values = my_cols)
Rplot.png

RColorBrewer 调色板

library(RColorBrewer)
par(mar=c(3,4,2,2))
display.brewer.all()
Rplot.png
show_col(brewer.pal(9, 'Set1'))
Rplot.png
p <- p + theme(legend.position = c(0.23, 0.7))
p1 <- p + scale_fill_brewer(palette = 'Set1') + ggtitle('Set1')
p2 <- p + scale_fill_brewer(palette = 'Set2') + ggtitle('Set2')
p3 <- p + scale_fill_brewer(palette = 'Set3') + ggtitle('Set3')
p4 <- p + scale_fill_brewer(palette = 'Pastel1') + ggtitle('Pastel1')
p5 <- p + scale_fill_brewer(palette = 'Pastel2') + ggtitle('Pastel2')
p6 <- p + scale_fill_brewer(palette = 'Paired') + ggtitle('Paired')
p7 <- p + scale_fill_brewer(palette = 'Dark2') + ggtitle('Dark2')
p8 <- p + scale_fill_brewer(palette = 'Accent') + ggtitle('Accent')
p9 <- p + scale_fill_brewer(palette = 'Paired') + ggtitle('Paired')

plot_grid(p1, p2, p3, p4, p5, p6, p7, p8, p9, labels = LETTERS)
Rplot.png

ggsci 调色板

参考:https://cran.r-project.org/web/packages/ggsci/vignettes/ggsci.html

library(ggsci)
my_cols <- pal_npg("nrc")(9)
show_col(my_cols)
Rplot.png

paletteer 调色板

参考:

https://github.com/EmilHvitfeldt/paletteer

https://github.com/EmilHvitfeldt/r-color-palettes

library(paletteer)

p + scale_fill_paletteer_d("awtools::mpalette") +
  ggtitle('awtools::mpalette')
Rplot.png

连续型变量

library(ggplot2)
library(cowplot)
p <- ggplot(data = small_diamonds, aes(x = carat, y = price)) +
  geom_point(shape = 21, size = 4, 
             color = 'black', aes(fill = depth)) +
  labs(x = 'weight of the diamond ', 
       y = 'price in US dollars') +
  scale_x_continuous(breaks = seq(0,3,0.5)) +
  scale_y_continuous(breaks = seq(0, 15000, 5000), 
                     labels = c('0', '5K', '10K', '15K')) +
  theme_bw() +
  theme(plot.title = element_text(hjust = 0.5),
        legend.background = element_blank(),
        legend.position = c(0.1, 0.8)) 

自定义颜色

单色

p + scale_fill_gradient(low = '#FCDAC9', high = '#7C0D0D')

Rplot.png

双色:红绿

p + scale_fill_gradient2(low = 'green', high = 'red', 
                         mid = 'white', midpoint = 60)
Rplot.png

双色:红蓝

p + scale_fill_gradient2(low = 'blue', high = 'red', 
                         mid = 'white', midpoint = 60)
Rplot.png

ggsci 调色板

p + scale_fill_gsea() 
Rplot.png

paletteer 调色板

library(paletteer)
library(pals)
p + scale_fill_paletteer_c("pals::coolwarm")
Rplot.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。