ggthemes包(汉化)——ggplot2绘图主题包

ggthemes包(汉化)——ggplot2绘图主题包

这个包的原始地址为:jrnold/ggthemes。为了让这个包支持中文,我在这篇文章的结尾汉化了这个包,Github地址为:czxa/ggthemes。除了主题外,这个包还包含了很多颜色标度,具体可以参考该包的文档网站:ggthemes-docs

安装

1
devtools::install_github("czxa/ggthemes")

主题

theme_base()

1
2
3
4
5
library(ggplot2)
library(ggthemes)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg, colour = factor(gear))) + facet_wrap(.~am)
p + theme_base()
ggsave("20181006b1.png")

theme_calc()

1
2
p + theme_calc()
ggsave("20181006b2.png")

scale_color_calc():改变标度的颜色

1
2
p + theme_calc() + scale_color_calc()
ggsave("20181006b3.png")

scale_shape_calc():改变标度的形状

1
2
3
4
q <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, shape = factor(gear))) +
facet_wrap(~am)
q + theme_calc() + scale_shape_calc()

theme_economist()

1
2
p + theme_economist()
ggsave("20181006b4.png")

scale_colour_economist():改变标度的颜色

1
2
p + theme_economist() + scale_colour_economist()
ggsave("20181006b6.png")

对调坐标轴

1
2
3
4
p + theme_economist(horizontal = FALSE) +
scale_colour_economist() +
coord_flip()
ggsave("20181006b7.png")

theme_economist_white()

1
2
p + theme_economist_white()
ggsave("20181006b5.png")

scale_colour_economist():改变标度的颜色

1
2
p + theme_economist_white() + scale_colour_economist()
ggsave("20181006b8.png")

gray_bg = FALSE:使用全白背景

1
2
3
p + theme_economist_white(gray_bg = FALSE) +
scale_colour_economist()
ggsave("20181006b9.png")

base_family=”Verdana”:使用其它字体

1
2
3
4
library("extrafont")
p + theme_economist(base_family="Verdana") +
scale_colour_economist()
ggsave("20181006b10.png")

theme_excel()

1
2
p + theme_excel() + scale_colour_excel()
ggsave("20181006b11.png")

1
2
3
4
5
ggplot(mpg, aes(x = class, fill = drv)) +
geom_bar() +
scale_fill_excel() +
theme_excel()
ggsave("20181006b12.png")

theme_few()

1
2
p + theme_few() + scale_colour_few()
ggsave("20181006b13.png")

1
2
p + theme_few() + scale_colour_few("Light")
ggsave("20181006b14.png")

1
2
p + theme_few() + scale_colour_few("Dark")
ggsave("20181006b15.png")

1
2
3
4
5
ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, shape = factor(gear))) +
theme_few() +
scale_shape_few()
ggsave("20181006b16.png")

theme_fivethirtyeight()

1
2
p + theme_fivethirtyeight()
ggsave("20181006b17.png")

theme_foundation()

1
2
p + theme_foundation(base_size = 12, base_family = "")
ggsave("20181006b18.png")

theme_gdocs()

1
2
p + theme_gdocs() + scale_color_gdocs()
ggsave("20181006b19.png")

theme_hc()

1
2
p + theme_hc() + scale_colour_hc()
ggsave("20181006b20.png")

theme_hc()

1
2
3
p + theme_hc(style = "darkunica", base_family = "") +
scale_colour_hc("darkunica")
ggsave("20181006b21.png")

theme_igray()

1
2
p + theme_igray()
ggsave("20181006b22.png")

theme_map()

这个主题主要应用于地图:

1
2
3
4
5
6
7
8
9
10
library("maps")
library("ggplot2")
us <- fortify(map_data("state"), region = "region")
(gg <- 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())
ggsave("20181006b23.png")

theme_pander()

1
2
p + theme_pander()
ggsave("20181006b24.png")

1
2
3
4
require("pander")
panderOptions("graph.grid.color", "red")
p + theme_pander(base_family = "")
ggsave("20181006b25.png")

1
2
3
4
5
ggplot(mpg, aes(x = class, fill = drv)) +
geom_bar() +
scale_fill_pander() +
theme_pander(base_family = "")
ggsave("20181006b26.png")

theme_par()

1
2
p + theme_par()
ggsave("20181006b27.png")

1
2
3
par(font = 2, col.lab = "red", fg = "white", bg = "black")
p + theme_par()
ggsave("20181006b28.png")

theme_stata()

1
2
3
p + theme_stata() +
scale_colour_stata("s2color")
ggsave("20181006b29.png")

1
2
3
p + theme_stata(scheme = "s1mono") +
scale_colour_stata("mono")
ggsave("20181006b30.png")

theme_tufte()

1
2
3
4
5
6
7
8
p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
scale_x_continuous(breaks = extended_range_breaks()(mtcars$wt)) +
scale_y_continuous(breaks = extended_range_breaks()(mtcars$mpg)) +
ggtitle("Cars")
p + geom_rangeframe() +
theme_tufte()
ggsave("20181006b31.png")

1
2
3
p + geom_rug() +
theme_tufte(ticks = FALSE)
ggsave("20181006b32.png")

theme_wsj()

1
2
3
4
5
6
p <- ggplot(mtcars) +
geom_point(aes(x = wt, y = mpg, colour = factor(gear))) +
facet_wrap(~am) +
ggtitle("Diamond Prices")
p + scale_colour_wsj("colors6", "") + theme_wsj()
ggsave("20181006b33.png")

1
2
p + scale_colour_wsj("colors6", "") + theme_wsj(color = "gray")
ggsave("20181006b34.png")

# R

评论

程振兴

程振兴 @czxa.top
截止今天,我已经在本博客上写了604.4k个字了!

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×