文章目录

  • theme()函数的用法
  • 1. 使用ggplot2包中内置主题
  • 2. 使用拓展包中的主题
  • ggthemes
  • ggthemr
  • 3.ggThemeAssist包



本文分为两个部分

套用ggplot2包中自带的主题模板

套用扩展包中的主题模板

主要介绍ggthemes ggthemr两个包

另外两个ggsci ggtech简要提及

theme()函数的用法

theme()在ggplot2中是一个非常强大的函数,几乎图形显示中的所有细节(包括:各种字体,各种颜色,各种边框)都可以用它来进行最后的调整。该函数的形式如下:

theme(
  line,
  rect,
  text,
  title,
  aspect.ratio,
  axis.title,
  axis.title.x,
  axis.title.x.top,
  axis.title.x.bottom,
  axis.title.y,
  axis.title.y.left,
  axis.title.y.right,
  axis.text,
  axis.text.x,
  axis.text.x.top,
  axis.text.x.bottom,
  axis.text.y,
  axis.text.y.left,
  axis.text.y.right,
  axis.ticks,
  axis.ticks.x,
  axis.ticks.x.top,
  axis.ticks.x.bottom,
  axis.ticks.y,
  axis.ticks.y.left,
  axis.ticks.y.right,
  axis.ticks.length,
  axis.ticks.length.x,
  axis.ticks.length.x.top,
  axis.ticks.length.x.bottom,
  axis.ticks.length.y,
  axis.ticks.length.y.left,
  axis.ticks.length.y.right,
  axis.line,
  axis.line.x,
  axis.line.x.top,
  axis.line.x.bottom,
  axis.line.y,
  axis.line.y.left,
  axis.line.y.right,
  legend.background,
  legend.margin,
  legend.spacing,
  legend.spacing.x,
  legend.spacing.y,
  legend.key,
  legend.key.size,
  legend.key.height,
  legend.key.width,
  legend.text,
  legend.text.align,
  legend.title,
  legend.title.align,
  legend.position,
  legend.direction,
  legend.justification,
  legend.box,
  legend.box.just,
  legend.box.margin,
  legend.box.background,
  legend.box.spacing,
  panel.background,
  panel.border,
  panel.spacing,
  panel.spacing.x,
  panel.spacing.y,
  panel.grid,
  panel.grid.major,
  panel.grid.minor,
  panel.grid.major.x,
  panel.grid.major.y,
  panel.grid.minor.x,
  panel.grid.minor.y,
  panel.ontop,
  plot.background,
  plot.title,
  plot.title.position,
  plot.subtitle,
  plot.caption,
  plot.caption.position,
  plot.tag,
  plot.tag.position,
  plot.margin,
  strip.background,
  strip.background.x,
  strip.background.y,
  strip.placement,
  strip.text,
  strip.text.x,
  strip.text.y,
  strip.switch.pad.grid,
  strip.switch.pad.wrap,
  ...,
  complete = FALSE,
  validate = TRUE
)

从上面的参数可以看到这里分以下几类:

  • axis开头, 主要对坐标轴的名称,刻度等细节进行控制
  • legend开头,主要对图例进行控制
  • panel开头,主要对平行图进行控制
  • plot开头,主要对plot中的一些细节进行控制
  • strip开头,主要对刻面图进行控制,例如刻面标签,刻面图标签的背景等
    在使用这些参数时,主要它们接受的一般是一个element_XX()对象,这可以在R中help得到用法,这里就不在赘述。

1. 使用ggplot2包中内置主题

主要有如下几种

theme_gray() # 默认
    theme_bw()
    theme_linedraw()
    theme_light()
    theme_dark()
    theme_minimal()
    theme_void()
    theme_test() #常用于学术期刊
    theme_classic()#常用于学术期刊

使用如下

library(ggplot2)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))
p + theme_gray() # 默认
p + theme_bw()
p + theme_linedraw()
p + theme_light()
p + theme_dark()
p + theme_minimal()
p + theme_classic()
p + theme_void()
p + theme_test()

r语言GMM面板 r语言theme模板_r语言GMM面板


r语言GMM面板 r语言theme模板_github_02

2. 使用拓展包中的主题

主要有4个包(主要介绍前两个)

ggthemes包

ggthemr包

ggtech包

ggsci包 #专门为学术图标开发的包
可以参考: https://nanx.me/ggsci/articles/ggsci.html

ggthemes

这个包内置了许多类似ggplot2中theme的主题,具体如下

代码示例

library(ggthemes)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))
p + theme_base()#和base包作图的效果一样

主题与图形对应展示如下

r语言GMM面板 r语言theme模板_github_03


除此之外,这个包还内置了一些可以直接套用的颜色

p2 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) + geom_point()
p2
p2 + theme_economist()
p2 + theme_economist() + scale_color_economist()
p2 + theme_economist() + scale_color_economist(stata = T)
p2 + theme_calc() + scale_color_calc()

r语言GMM面板 r语言theme模板_ci_04


将相同的theme和scale匹配起来展示如下

除此之外的其他颜色读者自试

r语言GMM面板 r语言theme模板_theme_05

ggthemr

在安装了scales包的前提下使用下面命令安装此包

devtools::install_github('cttobin/ggthemr') # 因为目前此包还未在cran上发布,所以从github上直接安装

(1) ggthemr也是套用已有模板,但是使用方法和前面有所不同。它是预先设置主题,之后正常作图,并且所有图形都会自动套用这个主题

library(ggthemr)

# 首先创建一个点图,一个柱状图
p0 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) + geom_point()
p1 <- ggplot(mtcars, aes(cyl)) + geom_bar(aes(fill = factor(gear)))
p0 # 展示ggplot2中默认的图形
p1

ggthemr('dust') # 设置其中一种样式
p0 # 展示这个主题下得到的图形
p1
ggthemr_reset() # 清楚现有主题
p0 # 图形又和ggplot2默认的情况相同
p1
ggthemr('flat') # 设置另外一种主题
p0
ggthemr('pale') # 用新的主题覆盖上一次设置的主题
p0
ggthemr_reset() # 如果之后不想用此包中的主题,注意要清空之前的设置

r语言GMM面板 r语言theme模板_r语言GMM面板_06


r语言GMM面板 r语言theme模板_ggplot2_07


r语言GMM面板 r语言theme模板_theme_08


r语言GMM面板 r语言theme模板_ci_09


r语言GMM面板 r语言theme模板_github_10

类似的主题有很多,截取官网上的一张图片,这张图片展示了内置主题的样式

r语言GMM面板 r语言theme模板_ci_11


(2) 接下来,我们来讲一下此包的一些细节

首先是ggthemr函数的其他参数解释

ggthemr函数帮助文档定义如下
ggthemr(palette = "dust", layout = "clear", spacing = 1.6,
text_size = 12, type = "inner", line_weight = 0.5, pos = 1,
envir = as.environment(pos), set_theme = TRUE)

palette 参数就是我们指定的样式

其他参数区别见下面例子

ggthemr("dust", layout = "clear", spacing = 1.6,
        text_size = 12, type = "inner", line_weight = 0.5, pos = 1) # 全部使用默认设置
p0

r语言GMM面板 r语言theme模板_ci_12

ggthemr("dust", layout = "clean", spacing = 3.2)
p0 # 网格线没有了(clean),外部空间变大了(spacing)

r语言GMM面板 r语言theme模板_r语言GMM面板_13

ggthemr("dust", layout = "minimal", text_size = 24)
p0 # 网格线和坐标轴线都没有了(minimal),字体变大(text_size)

r语言GMM面板 r语言theme模板_theme_14

ggthemr("dust", layout = "plain", type = "outer")
p0 # 网格虚线变成实线(plain), 样式扩充到了整张图(主图之外的部分也着色了)(outer)

r语言GMM面板 r语言theme模板_theme_15

ggthemr("dust", layout = "scientific", line_weight = 1)
p0 # 网格线间隔增加了一些虚线(scientific),线都变粗了(line_weight)

r语言GMM面板 r语言theme模板_theme_16

ggthemr("dust", set_theme = F) # 列出参数,之后做的图的样式却不改变
p0

(3) 下面讲一下包中的其他函数

# 一个展示颜色的函数
colour_plot(c('#14B294', 'coral'))
colour_plot(colors()[1:10])
colour_plot(ggthemr('sea')) # 展示这个主题的颜色
ggthemr("dust")
swatch() # 列出当前主题dust下的颜色
colour_plot(swatch()) # 展示当前主题的颜色

r语言GMM面板 r语言theme模板_theme_17


作图时总是从这个主题的颜色的第二个开始往下选取

ggthemr("pale")
colour_plot(swatch())
p0

r语言GMM面板 r语言theme模板_ci_18


每个主题的颜色深浅是可以调整的

# lighten_swatch和darken_swatch对颜色是离散形式的图形有效
# lighten_gradient和darken_gradient对颜色是连续形式的图形有效
# lighten_palette和darken_palette对二者皆有效

# 下面代码读者自己运行
ggthemr_reset()
ggthemr("solarized")
p0
colour_plot(swatch())
darken_swatch(amount = 0.3)
p0
colour_plot(swatch())
lighten_swatch(amount = 0.3)
p0
colour_plot(swatch())

ggthemr_reset()

df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100),
  z2 = abs(rnorm(100))
)
pp0 <- ggplot(df, aes(x, y)) + geom_point(aes(colour = z1))

pp0
darken_gradient(0.3)
pp0
lighten_gradient(0.3)
pp0

(4) 这个包中像sea dust 这样的样式也可以自定义

# 使用官网上的例子
set.seed(12345)
random_colours <- sample(colors()[-c(1, 253, 361)], 10L) # 创建一个颜色向量,有10个颜色

ugly <- define_palette( # 定义样式,除此之外还有 background text line gridline等参数
  swatch = random_colours,
  gradient = c(lower = random_colours[1L], upper = random_colours[2L]) # 选出两个颜色做渐变
)

ggthemr(ugly)
p0

(5) 一个主题包括背景的颜色,和点、文字等的颜色,一般都是搭配一起出现,但是我也可以只使用其中的一部分。

ggthemr_reset()

# 将ggthemr运行后得到的结果提取出来,方便之后进一步提取部分信息来使用
dust_theme <- ggthemr('dust', set_theme = FALSE) # set_theme参数让这个函数运行后不对之后作图产生影响
p0
p0 + dust_theme$theme # 只对背景等进行设置,不改变点的颜色,相当于使用ggplot2中的theme函数得到的结果
# 从前面的学习中我们知道,要改变点的颜色,需要用scale_color_系列函数,这里也是一样,提取出dust_theme中的scale_color_

p0 + dust_theme$theme + dust_theme$scales$scale_colour_discrete()

ggtech

这个包从许多科技公司取色,用于图表绘制之中

这个包需要从github上下载安装,而且要想正常使用需要加载一些字体(见官网文章末尾),具体使用也参考这个网站

ggsci

这个包主要用于学术,感兴趣的读者可以到[官方教程[(Scientific Journal and Sci-Fi Themed Color Palettes for ggplot2:https://ggsci.net/articles/ggsci.html)中学习。

3.ggThemeAssist包

新手还可以利用外界程序交互式地调整,并自动生成代码。不过该工具的使用需要先安装shiny包。
示例如下:

linstall.packages("shiny")
install.packages("ggThemeAssist")
ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))

鼠标选中ggplot()这一行,然后在Rstudio界面选择Tools->Addins->ggplot Theme Assistant选项,就可以手动调整,点击Done后自动生成代码。

r语言GMM面板 r语言theme模板_r语言GMM面板_19