简介
英国广播公司(British Broadcasting Corporation;BBC)是全球最大的新闻媒体,其中各类新闻稿件采用的统计图表能很好地传达信息。有时因为数据量庞大,手动整理太过于浪费时间,BBC也早早就使用编程语言来统计数据。此外,为了方便清洗可重复数据和绘制图表,BBC数据团队用R对数据进行处理和可视化,经年累月下于去年整理绘图经验并开发了R包-bbplot,那么今天小编就来介绍一下如何使用这个R包来做出高逼格的统计图。
代码演示
在开始讲bbplot包之前呢,先介绍一个神奇的R包pacman。我们知道,在linux 中我们有conda,python 中有pip,而不同的系统中提供了多种多样的包管理工具。R base包虽然提供了library 相关的函数以支持相关的管理操作,但有些时候依旧不是很方便。这时候pacman包就出现了。比起library来说,会方便很多。
if(!require(pacman))install.packages("pacman")
pacman::p_load('dplyr', 'tidyr', 'gapminder',
'ggplot2', 'ggalt',
'forcats', 'R.utils', 'png',
'grid', 'ggpubr', 'scales',
'bbplot')
# install.packages('devtools')
devtools::install_github('bbc/bbplot')
#Data for chart from gapminder package
line_df <- gapminder %>%
filter(country == "Malawi")
#Make plot
ggplot(line_df, aes(x = year, y = lifeExp)) +
geom_line(colour = "#1380A1", size = 1) +
geom_hline(yintercept = 0, size = 1, colour="#333333") +
bbc_style() +
labs(title="Living longer",
subtitle = "Life expectancy in Malawi 1952-2007")
这线条,这构造,是不是看起来就很高端!当然也可以画出多条折线。。。
#Prepare data
multiple_line_df <- gapminder %>%
filter(country == "China" | country == "United States")
#Make plot
ggplot(multiple_line_df, aes(x = year, y = lifeExp, colour = country)) +
geom_line(size = 1) +
geom_hline(yintercept = 0, size = 1, colour="#333333") +
scale_colour_manual(values = c("#FAAB18", "#1380A1")) +
bbc_style() +
labs(title="Living longer",
subtitle = "Life expectancy in China and the US")
Immugent最喜欢的是这个包做出的哑铃图。
library("ggalt")
library("tidyr")
#Prepare data
dumbbell_df <- gapminder %>%
filter(year == 1967 | year == 2007) %>%
select(country, year, lifeExp) %>%
spread(year, lifeExp) %>%
mutate(gap = `2007` - `1967`) %>%
arrange(desc(gap)) %>%
head(10)
#Make plot
ggplot(dumbbell_df, aes(x = `1967`, xend = `2007`, y = reorder(country, gap), group = country)) +
geom_dumbbell(colour = "#dddddd",
size = 3,
colour_x = "#FAAB18",
colour_xend = "#1380A1") +
bbc_style() +
labs(title="We're living longer",
subtitle="Biggest life expectancy rise, 1967-2007")
还有这种曲面图:
#Prepare data
facet <- gapminder %>%
filter(continent != "Americas") %>%
group_by(continent, year) %>%
summarise(pop = sum(as.numeric(pop)))
#Make plot
facet_plot <- ggplot() +
geom_area(data = facet, aes(x = year, y = pop, fill = continent)) +
scale_fill_manual(values = c("#FAAB18", "#1380A1","#990000", "#588300")) +
facet_wrap( ~ continent, ncol = 5) +
scale_y_continuous(breaks = c(0, 2000000000, 4000000000),
labels = c(0, "2bn", "4bn")) +
bbc_style() +
geom_hline(yintercept = 0, size = 1, colour = "#333333") +
theme(legend.position = "none",
axis.text.x = element_blank()) +
labs(title = "Asia's rapid growth",
subtitle = "Population growth by continent, 1952-2007")
大家可能已经注意到上面的图表,人口相对较少的大洋洲已经完全消失了,那么就需要分别展示了。
#Make plot
facet_plot_free <- ggplot() +
geom_area(data = facet, aes(x = year, y = pop, fill = continent)) +
facet_wrap(~ continent, scales = "free") +
bbc_style() +
scale_fill_manual(values = c("#FAAB18", "#1380A1","#990000", "#588300")) +
geom_hline(yintercept = 0, size = 1, colour = "#333333") +
theme(legend.position = "none",
axis.text.x = element_blank(),
axis.text.y = element_blank()) +
labs(title = "It's all relative",
subtitle = "Relative population growth by continent,1952-2007")
说在最后
这个包的功能还有很多,有需要的小伙伴可以专门去研究一下。主要是由两个函数实现:1.bbc_style():没有参数,通常是将文本大小、字体和颜色,轴线,轴线文本,边距和许多其他标准图表组件转换为BBC样式;2.finalise_plot()是bbplot程序包的第二个函数。它能按照BBC图形的标准将标题和副标题左对齐,在绘图的右下角添加页脚,也可以在左下角添加来源。
好啦,本期到这就结束啦!
网友评论