查看原文
其他

R|ggplot2(六)|套用主题模板

2017-11-18 dwzb R语言中文社区

作者:dwzb,R语言中文社区专栏作者。知乎专栏:https://zhuanlan.zhihu.com/Data-AnalysisR


第一篇戳:R|ggplot2(一)|一个完整的绘图流程

第二篇戳:R|ggplot2(二)|覆盖柱状图各种需求

第三篇戳:R|ggplot2(三)|coord 系列函数坐标轴转换

第四篇戳:R|ggplot2(四)|stat_ geom_ 和position

第五篇戳:R|ggplot2(五)|scale 修改默认设置


本文分为两个部分

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

  • 套用扩展包中的主题模板

    • 主要介绍ggthemes ggthemr两个包

    • 另外两个ggsci ggtech简要提及

1. 使用ggplot2包中内置主题

主要有如下几种

theme_gray() # 默认 theme_bw() theme_linedraw() theme_light() theme_dark() theme_minimal() theme_classic() theme_void()

使用如下

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()

2. 使用拓展包中的主题

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

  • ggthemes包

  • ggthemr包

  • ggtech包

  • ggsci包

ggthemes

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

# 代码示例 library(ggthemes) p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg)) p + theme_base()

主题与图形对应展示如下

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

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()

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

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

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() # 如果之后不想用此包中的主题,注意要清空之前的设置

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

(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) # 全部使用默认设置 p0ggthemr("dust", layout = "clean", spacing = 3.2) p0 # 网格线没有了(clean),外部空间变大了(spacing)ggthemr("dust", layout = "minimal", text_size = 24) p0 # 网格线和坐标轴线都没有了(minimal),字体变大(text_size)ggthemr("dust", layout = "plain", type = "outer") p0 # 网格虚线变成实线(plain), 样式扩充到了整张图(主图之外的部分也着色了)(outer)ggthemr("dust", layout = "scientific", line_weight = 1) p0 # 网格线间隔增加了一些虚线(scientific),线都变粗了(line_weight)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()) # 展示当前主题的颜色

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

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

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

# 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上下载安装,而且要想正常使用需要加载一些字体(见[官网文章](https://github.com/ricardo-bion/ggtech)末尾),具体使用也参考这个网站

ggsci

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


公众号后台回复关键字即可学习

回复 R              R语言快速入门免费视频 
回复 统计          统计方法及其在R中的实现
回复 用户画像   民生银行客户画像搭建与应用 
回复 大数据      大数据系列免费视频教程
回复 可视化      利用R语言做数据可视化
回复 数据挖掘   数据挖掘算法原理解释与应用
回复 机器学习   R&Python机器学习入门 

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存