学会这个BBC,你的图也可以上新闻啦!
英國廣播公司(British Broadcasting Corporation;BBC)是全球最大的新聞媒體,其中各類新聞稿件采用的統計圖表能很好地傳達信息。為了方便清洗可重復數據和繪制圖表,BBC數據團隊用R對數據進行處理和可視化,經年累月下于去年整理繪圖經驗并開發了R包-bbplot,幫助我們畫出和BBC新聞中一樣好看的圖形。
加載需要的R包
使用pacman[1]軟件包中的p_load函數通過以下代碼一次性加載。
#安裝pcaman軟件包并對其他R包進行加載 if(!require(pacman))install.packages("pacman")pacman::p_load('dplyr', 'tidyr', 'gapminder','ggplot2', 'ggalt','forcats', 'R.utils', 'png','grid', 'ggpubr', 'scales','bbplot')安裝bbplot軟件包
bbplot不在CRAN上,因此必須使用devtools直接從Github安裝它(編程模板-R語言腳本寫作:最簡單的統計與繪圖,包安裝、命令行參數解析、文件讀取、表格和矢量圖輸出)。
# install.packages('devtools') devtools::install_github('bbc/bbplot')下載軟件包并成功安裝后,就可以創建圖表了( Science組合圖表解讀)。
bbplot軟件包如何工作?
該軟件包具有兩個函數功能,bbc_style()和finalise_plot()。
bbc_style():沒有參數,通常是將文本大小、字體和顏色,軸線,軸線文本,邊距和許多其他標準圖表組件轉換為BBC樣式。
對于折線圖而言,折線的顏色或條形圖的顏色,并不是從bbc_style()函數中直接實現的,而是需要在其他標準ggplot(ggplot2高效實用指南 (可視化腳本、工具、套路、配色))圖表函數中明確設置。
下面的代碼顯示了如何在標準圖表制作工作流程中使用bbc_style()。這是一個非常簡單的折線圖的示例,使用了gapminder程序包中的數據。
#Data for chart from gapminder package line_df <- gapminder %>%filter(country == "Malawi")#Make plot line <- 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")這是bbc_style()函數在后臺實際執行的操作。它實質上修改了ggplot2主題功能(ggplot2學習筆記之圖形排列)中的某些參數。
例如,第一個參數是設置圖標題元素的字體、大小、和字體顏色。
## function () ## { ## font <- "Helvetica" ## ggplot2::theme(plot.title = ggplot2::element_text(family = font, ## size = 28, face = "bold", color = "#222222"), plot.subtitle = ggplot2::element_text(family = font, ## size = 22, margin = ggplot2::margin(9, 0, 9, 0)), plot.caption = ggplot2::element_blank(), ## legend.position = "top", legend.text.align = 0, legend.background = ggplot2::element_blank(), ## legend.title = ggplot2::element_blank(), legend.key = ggplot2::element_blank(), ## legend.text = ggplot2::element_text(family = font, size = 18, ## color = "#222222"), axis.title = ggplot2::element_blank(), ## axis.text = ggplot2::element_text(family = font, size = 18, ## color = "#222222"), axis.text.x = ggplot2::element_text(margin = ggplot2::margin(5, ## b = 10)), axis.ticks = ggplot2::element_blank(), ## axis.line = ggplot2::element_blank(), panel.grid.minor = ggplot2::element_blank(), ## panel.grid.major.y = ggplot2::element_line(color = "#cbcbcb"), ## panel.grid.major.x = ggplot2::element_blank(), panel.background = ggplot2::element_blank(), ## strip.background = ggplot2::element_rect(fill = "white"), ## strip.text = ggplot2::element_text(size = 22, hjust = 0)) ## } ## <environment: namespace:bbplot>通過向bbc_style()函數中包含的主題添加額外的主題參數,例如添加一些網格線。
theme(panel.grid.major.x = element_line(color="#cbcbcb"),panel.grid.major.y=element_blank())保存完成的圖表
finalise_plot()是bbplot程序包的第二個函數。它能按照BBC圖形的標準將標題和副標題左對齊,在繪圖的右下角添加頁腳,也可以在左下角添加來源。它還可以將圖表保存到指定的位置。該函數有五個參數:
plot_name: the variable name that you have called your plot, for example for the chart example above plot_name would be "line"
source: the source text that you want to appear at the bottom left corner of your plot. You will need to type the word "Source:" before it, so for example source = "Source: ONS" would be the right way to do that.
save_filepath: the precise filepath that you want your graphic to save to, including the .png extension at the end. This does depend on your working directory and if you are in a specific R project. An example filepath would be: Desktop/R_projects/charts/line_chart.png.
width_pixels: this is set to 640px by default, so only call this argument if you want the chart to have a different width, and specify what you want it to be.
height_pixels: this is set to 450px by default, so only call this argument if you want the chart to have a different height, and specify what you want it to be.
logo_image_path: this argument specifies the path for the image/logo in the bottom right corner of the plot. The default is for a placeholder PNG file with a background that matches the background colour of the plot, so do not specify the argument if you want it to appear without a logo. If you want to add your own logo, just specify the path to your PNG file. The package has been prepared with a wide and thin image in mind.
在標準工作流程中使用finalise_plot()的示例:
finalise_plot(plot_name = my_line_plot,source = "Source: Gapminder",save_filepath = "filename_that_my_plot_should_be_saved_to.png",width_pixels = 640,height_pixels = 450,logo_image_path = "placeholder.png")那么如何保存上面創建的示例圖?
finalise_plot(plot_name = line,source = "Source: Gapminder",save_filepath = "images/line_plot_finalised_test.png",width_pixels = 640,height_pixels = 550)!!前方高能!!一大波圖即將“來襲”……
制作折線圖
#準備數據 line_df <- gapminder %>%filter(country == "China")#作圖 line <- 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 China 1952-2007")制作多條折線的圖
#準備數據 multiple_line_df <- gapminder %>%filter(country == "China" | country == "United States")#作圖 multiple_line <- 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")R語言 - 線圖繪制
制作條形圖
#準備數據 bar_df <- gapminder %>%filter(year == 2007 & continent == "Africa") %>%arrange(desc(lifeExp)) %>%head(5)#作圖 bars <- ggplot(bar_df, aes(x = country, y = lifeExp)) +geom_bar(stat="identity",position="identity",fill="#1380A1") +geom_hline(yintercept = 0, size = 1, colour="#333333") +bbc_style() +labs(title="Reunion is highest",subtitle = "Highest African life expectancy, 2007")R語言 - 柱狀圖
制作堆疊條形圖
#準備數據 stacked_df <- gapminder %>%filter(year == 2007) %>%mutate(lifeExpGrouped = cut(lifeExp,breaks = c(0, 50, 65, 80, 90),labels = c("Under 50", "50-65", "65-80", "80+"))) %>%group_by(continent, lifeExpGrouped) %>%summarise(continentPop = sum(as.numeric(pop)))#set order of stacks by changing factor levels stacked_df$lifeExpGrouped = factor(stacked_df$lifeExpGrouped, levels = rev(levels(stacked_df$lifeExpGrouped)))#作圖 stacked_bars <- ggplot(data = stacked_df,aes(x = continent,y = continentPop,fill = lifeExpGrouped)) +geom_bar(stat = "identity",position = "fill") +bbc_style() +scale_y_continuous(labels = scales::percent) +scale_fill_viridis_d(direction = -1) +geom_hline(yintercept = 0, size = 1, colour = "#333333") +labs(title = "How life expectancy varies",subtitle = "% of population by life expectancy band, 2007") +theme(legend.position = "top",legend.justification = "left") +guides(fill = guide_legend(reverse = TRUE))堆疊柱狀圖各成分連線畫法:突出組間變化
制作分組條形圖
只需要將position =“identity”更改為position =“dodge”:
#準備數據 grouped_bar_df <- gapminder %>%filter(year == 1967 | year == 2007) %>%select(country, year, lifeExp) %>%spread(year, lifeExp) %>%mutate(gap = `2007` - `1967`) %>%arrange(desc(gap)) %>%head(5) %>%gather(key = year,value = lifeExp,-country,-gap)#畫圖 grouped_bars <- ggplot(grouped_bar_df,aes(x = country,y = lifeExp,fill = as.factor(year))) +geom_bar(stat="identity", position="dodge") +geom_hline(yintercept = 0, size = 1, colour="#333333") +bbc_style() +scale_fill_manual(values = c("#1380A1", "#FAAB18")) +labs(title="We're living longer",subtitle = "Biggest life expectancy rise, 1967-2007")是Excel的圖,不!是R的圖
制作啞鈴圖
library("ggalt") library("tidyr")#準備數據 dumbbell_df <- gapminder %>%filter(year == 1967 | year == 2007) %>%select(country, year, lifeExp) %>%spread(year, lifeExp) %>%mutate(gap = `2007` - `1967`) %>%arrange(desc(gap)) %>%head(10)#作圖 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")制作直方圖
hist_df <- gapminder %>%filter(year == 2007)ggplot(hist_df, aes(lifeExp)) +geom_histogram(binwidth = 5, colour = "white", fill = "#1380A1") +geom_hline(yintercept = 0, size = 1, colour="#333333") +bbc_style() +scale_x_continuous(limits = c(35, 95),breaks = seq(40, 90, by = 10),labels = c("40", "50", "60", "70", "80", "90 years")) +labs(title = "How life expectancy varies",subtitle = "Distribution of life expectancy in 2007")對圖例進行更改
去掉圖例:
multiple_line + guides(colour=FALSE) #or multiple_line + theme(legend.position = "none")改變圖例位置:
multiple_line + theme(legend.position = "right")改變坐標軸
翻轉坐標軸:
bars <- bars + coord_flip()#垂直變成水平添加/刪除網格線:
bars <- bars + coord_flip() +theme(panel.grid.major.x = element_line(color="#cbcbcb"),panel.grid.major.y=element_blank())#默認主題只有y軸的網格線。使用panel.grid.major.x = element_line添加x軸上的網格線。(使用panel.grid.major.y = element_blank()刪除y軸上的網格線)人工更改軸間距:
使用scale_y_continuous或scale_x_continuous更改軸文本標簽:
bars <- bars + scale_y_continuous(limits=c(0,85),breaks = seq(0, 80, by = 20),labels = c("0","20", "40", "60", "80 years"))bars在軸標簽上添加千位分隔符
+ scale_y_continuous(labels = function(x) format(x, big.mark = ",",scientific = FALSE))將百分比符號添加到軸標簽:
+ scale_y_continuous(labels = function(x) paste0(x, "%"))構面
ggplot可以輕松創建多個小圖表,這被稱為構面。如果將需要可視化的數據按某個變量劃分,則需要使用函數facet_wrap或facet_grid。
#準備數據 facet <- gapminder %>%filter(continent != "Americas") %>%group_by(continent, year) %>%summarise(pop = sum(as.numeric(pop)))#作圖 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")可以嘗試的參數實在是太多啦!大家可以試一試呀!
[1]:https://bbc.github.io/rcookbook/#how_to_create_bbc_style_graphics
來源:https://bbc.github.io/rcookbook/
撰文:May
編輯:生信寶典
推薦閱讀
史上最全的圖表色彩運用原理
學術圖表的基本配色方法
數據可視化基本套路總結
萬能轉換:R圖和統計表轉成發表級的Word、PPT、Excel、HTML、Latex、矢量圖等
2019年諾貝爾生理醫學獎揭曉 |動圖展示歷年生理學獎
往期精品(點擊圖片直達文字對應教程)
后臺回復“生信寶典福利第一波”或點擊閱讀原文獲取教程合集
總結
以上是生活随笔為你收集整理的学会这个BBC,你的图也可以上新闻啦!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 引号吃掉了我的数据~~~
- 下一篇: 送书 | 获得诺贝尔奖之后影响力会下降?