ggplot2的主题模板总结

ggplot2的主题模板总结

ggplot2的主题模板

1.使用ggplot2包中内置主题

主要有如下几种

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

使用如下

library(ggplot2)
library(gridExtra)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))

grid.arrange(p + theme_gray() + ggtitle("theme_gray"),
             p + theme_bw() + ggtitle("theme_bw"),
             p + theme_linedraw() + ggtitle("theme_linedraw"), 
             p + theme_light() + ggtitle("theme_light"),
             p + theme_dark() + ggtitle("theme_dark"), 
             p + theme_minimal() + ggtitle("theme_minimal"),
             p + theme_classic() + ggtitle("theme_classic"),
             p + theme_void() + ggtitle("theme_void"),ncol=2)

2.使用拓展包中的主题

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

  • ggthemes包
  • ggthemr包
  • ggtech包
  • ggsci包

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

  • theme_base: a theme resembling the default base graphics in R. See also theme_par.

  • theme_calc: a theme based on LibreOffice Calc.

  • theme_economist: a theme based on the plots in the The Economist magazine.

  • theme_excel: a theme replicating the classic ugly gray charts in Excel

  • theme_few: theme from Stephen Few’s “Practical Rules for Using Color in Charts”.

  • theme_fivethirtyeight: a theme based on the plots at fivethirtyeight.com.

  • theme_gdocs: a theme based on Google Docs.

  • theme_hc: a theme based on Highcharts JS.

  • theme_par: a theme that uses the current values of the base graphics parameters in par.

  • theme_pander: a theme to use with the pander package.

  • theme_solarized: a theme using the solarized color palette.

  • theme_stata: themes based on Stata graph schemes.

  • theme_tufte: a minimal ink theme based on Tufte’s The Visual Display of Quantitative Information.

  • theme_wsj: a theme based on the plots in the The Wall Street Journal.

# 代码示例
library(ggthemes)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))
p1 <- p + theme_base() + ggtitle("theme_base")
p2 <- p + theme_calc() + ggtitle('theme_calc')
p3 <- p + theme_economist() + ggtitle("theme_economist")
p4 <- p + theme_excel() + ggtitle("theme_excel")
p5 <- p + theme_few() + ggtitle("theme_few")
p6 <- p + theme_fivethirtyeight() + ggtitle("theme_fivethirtyeight")
p7 <- p + theme_gdocs() + ggtitle("theme_gdocs")
p8 <- p + theme_hc() + ggtitle("theme_hc")
p9 <- p + theme_par() + ggtitle("theme_par")
p10 <- p + theme_pander() + ggtitle("theme_pander")
p11 <- p + theme_solarized() + ggtitle("theme_solarized")
p12 <- p + theme_stata() + ggtitle("theme_stata")
p13 <- p + theme_tufte() + ggtitle("theme_tufte")
p14 <- p + theme_wsj() + ggtitle("theme_wsj")

grid.arrange(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,
             ncol=2)

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

  • scale_colour_calc, scale_shape_calc: color and shape palettes from LibreOffice Calc.

  • scale_colour_colorblind: Colorblind safe palette from http://jfly.iam.u-tokyo.ac.jp/color/.

  • scale_colour_economist: colors used in plots in plots in The Economist.

  • scale_colour_excel: colors from new and old Excel.

  • scale_colour_few: color palettes from Stephen Few’s “Practical Rules for Using Color in Charts”.

  • scale_colour_gdocs: color palette from Google Docs.

  • scale_colour_hc: a theme based on Highcharts JS.

  • scale_colour_solarized: Solarized colors

  • scale_colour_stata, scale_shapes_stata, scale_linetype_stata: color, shape, and linetype palettes from Stata graph schemes.

  • scale_colour_tableau, scale_shape_tableau: color and shape palettes from Tableau.

  • scale_colour_pander, scale_fill_pander: scales to use with the pander package.

  • scale_colour_ptol, scale_fill_ptol: color palettes from Paul Tol’s Colour Schemes

  • scale_shape_cleveland, scale_shape_tremmel, scale_shape_circlefill: shape scales from classic works in visual perception: Cleveland, Tremmel (1995), and Lewandowsky and Spence (1989).

p2 <- ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(gear))) + geom_point()

p3 <- p2 + theme_economist()
p4 <- p2 + theme_economist() + scale_color_economist()
p5 <- p2 + theme_economist() + scale_color_economist(stata = T)
p6 <- p2 + theme_calc() + scale_color_calc()

grid.arrange(p2,p3,p4,p5,p6,ncol = 2)

业务中可以将相同的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)))
# 展示ggplot2中默认的图形
grid.arrange(p0,p1,ncol = 2)

ggthemr('dust') # 设置其中一种样式
# 展示这个主题下得到的图形
grid.arrange(p0,p1,ncol = 2)

ggthemr_reset() # 清除现有主题

# 图形又和ggplot2默认的情况相同
grid.arrange(p0,p1,ncol = 2)

ggthemr('flat') # 设置另外一种主题
p0

ggthemr('pale') # 用新的主题覆盖上一次设置的主题
p0

ggthemr_reset() # 如果之后不想用此包中的主题,注意要清空之前的设置

类似的主题有很多,去官网浏览不同的更多主题。

  1. 接下来,我们来讲一下此包的一些细节

首先是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

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