美文网首页
科研绘图的主题与颜色搭配

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

作者: Bioinfor生信云 | 来源:发表于2023-12-11 15:50 被阅读0次

主题

除了 theme_bw()以外,下面主题也适合科研绘图

ggplot2 自带主题: https://ggplot2.tidyverse.org/reference/ggtheme .html

cowplot 包主题: https://wilkelab.org/cowplot/articles/themes.html

ggthemes 包主题: https://yutannihilation.github.io/

ggthemr 包主题: https://github.com/cttobin/ggthemr

ggplot2 自带主题我常用的是 theme_bw()、theme_few()、 theme_cowplot()、 theme_minimal()

library(ggplot2)
library(ggsci)
library(cowplot)
library(tidyverse)
data("diamonds")
set.seed(1000)
small_diamonds <- sample_n(diamonds, size = 500)

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)

自定义主题

通过 ggThemeAssist 包来自定义主题,选中变量,在 Addins 中选择 ggThemeAssist进行调整,会自动生成代码。

颜色搭配

  • 小技巧
library(scales)
my_cols <- c('#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00')
show_col(my_cols)

离散型变量

如果自定义颜色,可以到使用 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
  • 自定义颜色
library(scales)
my_cols <- c('#e41a1c', '#377eb8', '#4daf4a', '#984ea3', '#ff7f00')
show_col(my_cols)
p + scale_fill_manual(values = my_cols)

还可以使用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 = 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 = '#5c7ada', high = '#df9dc0')
p + scale_fill_gradient2(low = 'green', high = 'red', 
                         mid = 'white', midpoint = 60)

欢迎加入学习群

相关文章

网友评论

      本文标题:科研绘图的主题与颜色搭配

      本文链接:https://www.haomeiwen.com/subject/oxpsgdtx.html