hrbrthemes:我的当前ggplot2主题

hrbrthemes:我的当前ggplot2主题

hrbrthemes包中有一个非常学术范儿的主题——theme_ipsum(),最近非常喜欢。本文是学习hrbrmstr/hrbrthemes的笔记。

安装

R
1
devtools::install_github("hrbrmstr/hrbrthemes")

使用

theme_ipsum()

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
library(hrbrthemes)
library(gcookbook) # 提供示例数据
library(tidyverse)
# 查看当前版本
packageVersion("hrbrthemes")

# theme_ipsum_rc()似乎不好用(字体问题,该主题的字体是IBM Plex Sans,不支持中文),总是出错。
ggplot(mtcars,
aes(mpg, wt)) +
geom_point() +
labs(x="里程数 (mpg)", y="重量 (顿)",
title="散点图示例",
subtitle="A plot that is only useful for demonstration purposes",
caption="Brought to you by the letter 'g'") +
theme_ipsum(base_family = 'PingFangSC-Regular')

发现函数苹方简体字体比较适合这个主题。

标度

scale_color_ipsum()scale_fill_ipsum()的配饰方案似乎就是highcharts的配色方案。

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
ggplot(mpg, aes(displ, hwy)) +
geom_jitter(aes(color=class, fill=class), size=3, shape=21, alpha=1/2) +
scale_x_continuous(expand=c(0,0), limits=c(1, 8), breaks=1:8) +
scale_y_continuous(expand=c(0,0), limits=c(10, 50)) +
scale_color_ipsum() +
scale_fill_ipsum() +
facet_wrap(~class, scales="free") +
labs(
title="IBM Plex Sans Test",
subtitle="This is a subtitle to see the how it looks in IBM Plex Sans",
caption="Source: hrbrthemes & IBM"
) +
theme_ipsum(grid="XY", axis="xy", base_family = 'PingFangSC-Regular') +
theme(legend.position="none")

坐标轴格式

R
1
2
3
4
5
6
7
8
9
10
11
12
count(mpg, class) %>% 
mutate(pct=n/sum(n)) %>%
ggplot(aes(class, pct, fill = class)) +
scale_fill_ipsum() +
guides(fill = 'none') +
geom_col() +
scale_y_percent() +
labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
title="Seminal ggplot2 column chart example with percents",
subtitle="A plot that is only useful for demonstration purposes",
caption="Brought to you by the letter 'g'") +
theme_ipsum(grid="Y", base_family = 'PingFangSC-Regular')

R
1
2
3
4
5
6
7
8
9
10
11
ggplot(uspopage, aes(x=Year, y=Thousands, fill=AgeGroup)) + 
geom_area() +
scale_fill_ipsum() +
scale_x_continuous(expand=c(0,0)) +
scale_y_comma() +
labs(title="Age distribution of population in the U.S., 1900-2002",
subtitle="Example data from the R Graphics Cookbook",
caption="Source: R Graphics Cookbook") +
theme_ipsum(grid="XY", base_family = 'PingFangSC-Regular') +
theme(axis.text.x=element_text(hjust=c(0, 0.5, 0.5, 0.5, 1))) +
theme(legend.position="bottom")

# R

评论

Your browser is out-of-date!

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

×