使用ggthemes,让你画出杂志配图般的图

Overview

ggplot2R界大神Hadley Wickham写的众多改变R的package中的一个,也是使用最普遍的package之一,ggplot2通过图层的方法极大的拓展了R的绘图功能,能够自由选择描绘数据的图形,配色,坐标轴,标识等等。同时ggplot2也给除了模板的功能,本身自带几个简单的theme_grey(Default),theme_bw,theme_light等主题,使用者也可以自定义自己的主题,包括配色,legend位置,实用的字体等等。

但是对大部分初级使用者来说自定义主题是比较困难的,并且很多人也不知道自己想要什么样的主题,Jeffrey Arnold写的这个ggthemes这个包或许可以帮助你。ggthemes提供了很多现成的(同时也是精美的)主题,能够免了纠结配色调整等细节的担忧,下面的内容多翻译自ggthemes的README:

ggthemes 包括了ggplot2几个扩展的geoms, scales, and themes

Geoms

  • geom_rangeframe : Tufte 风格的距离框
  • geom_tufteboxplot: Tufte风格的箱型图()

Themes (主题)

  • theme_calc: 一套LibreOffice Calc(对应office Excel)风格的主题
  • theme_economist: 《经济学人》(The Economist)杂志配图风格主题
  • theme_excel: a theme replicating the classic ugly gray charts in Excel 复制Excel(pre-2003)灰色丑陋风格的主题🙄
  • theme_few: Stephen Few“Practical Rules for Using Color in Charts”.这本书的一个主题
  • theme_fivethirtyeight: fivethirtyeight.com网站配图风格主题
  • theme_gdocs: Google Docs风格主题.
  • theme_hc:Highcharts JS风格主题.
  • theme_pander: pander包风格主题
  • theme_solarized: 使用 solarized 调色板的主题
  • theme_stata: Stata绘图主题
  • theme_tufte: Tufte’s The Visual Display of Quantitative Information.这本书的最简洁的主题

Scales

  • scale_colour_calc, scale_shape_calc: LibreOffice Calc的颜色和形状的调色板.
  • scale_colour_colorblind: 色盲无障碍的调色板(from http://jfly.iam.u-tokyo.ac.jp/color/).
  • scale_colour_economist: The Economist 配图颜色调色板.
  • scale_colour_excel: 新旧Office Excel调色板
  • scale_colour_few: Stephen Few“Practical Rules for Using Color in Charts”这本书绘图配色调色板。
  • scale_colour_gdocs: Google Docs调色板.
  • scale_colour_hc: Highcharts JS主题调色板.
  • scale_colour_solarized: Solarized 颜色
  • scale_colour_stata, scale_shapes_stata, scale_linetype_stata: Stata绘图的颜色形状和线条类型的调色板。
  • scale_colour_tableau, scale_shape_tableau: Tableau软件的形状颜色.
  • scale_colour_pander, scale_fill_pander: pander包的颜色和填充调色板.
  • scale_shape_cleveland, scale_shape_tremmel, scale_shape_circlefill: Cleveland, Tremmel (1995), and Lewandowsky and Spence (1989)三篇文章可视化展现的经典形状.
  • scale_x_tufte, scale_y_tufte: 带包含最小值和最大值漂亮标签的x和y的scale.

其他

  • bank_slopes: Find the optimal aspect ratio to bank slopes to 45 degrees

安装

CRAN安装,

1
install.packages('ggthemes')

通过devtoolsgithub安装最新的包,

1
2
library("devtools")
install_github(c("hadley/ggplot2", "jrnold/ggthemes"))

注意: 目前开发版(从github上安装的) ggthemes 只能和开发版的ggplot2一起工作, 和CRAN上的 ggplot2 不兼容

例子

1
2
3
library("ggplot2")
library("ggthemes")
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]

Tufte theme 和 geoms

根据The Visual Display of
Quantitative Information
这本书最简绘图原则绘制

1
2
3
4
(ggplot(mtcars, aes(wt, mpg))
+ geom_point()
+ geom_rangeframe()
+ theme_tufte())

tufte1)

The Tufte minimal boxplot

1
2
3
(ggplot(mtcars, aes(factor(cyl), mpg))
+ theme_tufte(ticks=FALSE)
+ geom_tufteboxplot())

Economist 主题

近似《经济学人》的主题

1
2
3
4
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_economist()
+ scale_colour_economist()
+ ggtitle("Diamonds Are Forever"))

plot of chunk economist

Solarized 主题

A theme and color and fill scales based on the Solarized palette.

浅色主题

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_solarized()
+ scale_colour_solarized("blue"))

plot of chunk solarized-light

深色主题

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_solarized(light=FALSE)
+ scale_colour_solarized("red"))

plot of chunk solarized-dark

备选主题

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_solarized_2()
+ scale_colour_solarized("blue"))

plot of chunk solarized-alt

Stata主题

stata绘图的(color, fill, linetype, shapes)

1
2
3
4
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_stata()
+ scale_colour_stata()
+ ggtitle("Plot Title"))

plot of chunk stata

Excel 2003 主题

对于这个经典的丑陋的主题,仅仅用于讽刺就好了,千万不要使用它,另外不包括3-D柱形图和饼状图

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_excel()
+ scale_colour_excel())

plot of chunk excel1

1
2
3
4
(ggplot(diamonds, aes(clarity, fill=cut))
+ geom_bar()
+ scale_fill_excel()
+ theme_excel())

plot of chunk excel2

Inverse Gray Theme

ggplot2默认主题theme_gray相反的主题,即背景是灰色的,画图区域是白色的

1
2
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_igray())

plot of chunk igray

Fivethirtyeight 主题

基于fivethirtyeight.com.
的主题和调色板

1
2
3
4
(qplot(hp, mpg, data= subset(mtcars, cyl != 5), geom="point", color = factor(cyl))
+ geom_smooth(method = "lm", se = FALSE)
+ scale_color_fivethirtyeight()
+ theme_fivethirtyeight())

plot of chunk fivethirtyeight

Tableau Scales

Color, fill, and shape scales based on those used in the Tableau software.
Tableau软件使用的的颜色(Tableau是一款快速可视化分析工具)

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_igray()
+ scale_colour_tableau())

plot of chunk tableau

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_igray()
+ scale_colour_tableau("colorblind10"))

plot of chunk tableau-colorbind10

Stephen Few’s 用色规则

、Stephen Few’s “Practical Rules for Using Color in Charts”这部作品主张的用色规则

1
2
3
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_few()
+ scale_colour_few())

plot of chunk few

Wall Street Journal

The Wall Street Journal配图的颜色和主题

1
2
3
4
(qplot(carat, price, data=dsamp, colour=cut)
+ theme_wsj()
+ scale_colour_wsj("colors6", "")
+ ggtitle("Diamond Prices"))

plot of chunk wsj

GDocs Theme

Google Docs 图表的颜色和主题

1
2
3
4
(qplot(carat, price, data=dsamp, colour=clarity)
+ theme_gdocs()
+ ggtitle("Diamonds")
+ scale_color_gdocs())

plot of chunk gdocs

Calc Theme

LibreOffice(一款开源的办公套件) Calc(参照Excel)的主题形状和颜色.

1
2
3
4
(qplot(carat, price, data=dsamp, colour=clarity)
+ theme_calc()
+ ggtitle("Diamonds")
+ scale_color_calc())

plot of chunk calc

Pander Theme

pander 包的主题和颜色

1
2
3
(qplot(carat, price, data = dsamp, colour = clarity)
+ theme_pander()
+ scale_colour_pander())

plot of chunk pander-scatterplot

1
2
3
(ggplot(dsamp, aes(clarity, fill = cut)) + geom_bar()
+ theme_pander()
+ scale_fill_pander())

plot of chunk pander-barplot

Highcharts theme

Highcharts JS(一套javascript图形实现)的颜色和主题.

1
2
3
4
(qplot(carat, price, data = dsamp, colour = cut)
+ theme_hc()
+ scale_colour_hc()
+ ggtitle("Diamonds Are Forever"))

plot of chunk hc-default

1
2
3
4
(qplot(carat, price, data = dsamp, colour = cut)
+ theme_hc(bgcolor = "darkunica")
+ scale_colour_hc("darkunica")
+ ggtitle("Diamonds Are Forever"))

plot of chunk hc-darkunica

1
2
3
4
5
6
dtemp <- data.frame(months = factor(rep(substr(month.name,1,3), 4), levels = substr(month.name,1,3)),
city = rep(c("Tokyo", "New York", "Berlin", "London"), each = 12),
temp = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6,
-0.2, 0.8, 5.7, 11.3, 17.0, 22.0, 24.8, 24.1, 20.1, 14.1, 8.6, 2.5,
-0.9, 0.6, 3.5, 8.4, 13.5, 17.0, 18.6, 17.9, 14.3, 9.0, 3.9, 1.0,
3.9, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8))
1
2
3
4
5
qplot(months, temp, data=dtemp, group=city, color=city, geom="line") +
geom_point(size=1.1) +
ggtitle("Monthly Average Temperature") +
theme_hc() +
scale_colour_hc()

plot of chunk hc-default-line

1
2
3
4
5
qplot(months, temp, data=dtemp, group=city, color=city, geom="line") +
geom_point(size=1.1) +
ggtitle("Monthly Average Temperature") +
theme_hc(bgcolor = "darkunica") +
scale_fill_hc("darkunica")

plot of chunk hc-darkunica-line

Maps 主题

展示地图的一个主题

1
library("maps")
1
2
3
4
##
## # ATTENTION: maps v3.0 has an updated 'world' map. #
## # Many country borders and names have changed since 1990. #
## # Type '?world' or 'news(package="maps")'. See README_v3. #
1
2
3
4
5
6
7
8
us <- fortify(map_data("state"), region = "region")
(ggplot()
+ geom_map(data = us, map = us,
aes(x = long, y = lat, map_id = region, group = group),
fill = "white", color = "black", size = 0.25)
+ coord_map("albers", lat0 = 39, lat1 = 45)
+ theme_map()
)

plot of chunk map

参考和推荐

  1. ggthemes包:丰富ggplot2的表现力
  2. ggthemes repo

    知识共享许可协议
    本作品采用知识共享署名 4.0 国际许可协议进行许可。