歡迎關注天善智慧, 我們是專注于商業智慧BI, 大資料, 資料分析領域的垂直社區, 學習, 問答、求職一站式搞定!
本文將給大家介紹一個ggplot2灰常牛X的視覺化擴展包, 我將該包主頁的包用法介紹整理成中文, 分享給大家。
包名叫geofacet, 有經驗的charter大概能猜出來個大概, 沒錯該包是關於視覺化資料中的地理資訊, 以及維度分面。
作者命名非常講究, 將該包的兩個主要核心功能進行組合命名。
地理資訊視覺化分面, 這麼吊的包你肯定是第一次看到吧(其實之前介紹過一些地圖上的mini 直條圖、圓形圖等都算這一類),
這樣瞭解ggplot2的用戶學習成本就低了很多, 因為只需瞭解這個分面參數的具體設定, 組織對應資料來源格式就OK了。
以下是本文的主要內容:
geofacet包擴展了ggplot2的分面函數, 進而提供了基於地理資訊的更加靈活的資料視覺化方案。 這個分面函數並無特別指出, 如同內置的分面函數(facet_grid、facet_wrap等)用法沒有太大差別。 唯一的區別是, 在最終的圖形版面呈現結果上, 允許單個圖表分面刻畫在對應的地理多邊形中心位置。
該包的核心功能可以概括為以下幾點:
每一個分面儲存格都可以呈現一個維度的資料(而非單個數值);
每一個分面儲存格可以容納任何一種ggplot2內置圖表物件(看清楚了, 是任何一種, 任何一種, 任何一種, 就問你這包屌不屌!);
分面系統支援任何的地理多邊形(可以是內建的, 也可以是用戶自訂的)。
該包的強大優勢絕不僅僅只有以下展示的這些內容, 很快我們將會建立一個該包的專屬博客(如果建好了會將其網站分享在本頁面)。
install.packages("geofacet")
# or from github:
# devtools::install_github("hafen/geofacet")
library(geofacet)
library(ggplot2)
library(ggthemes)
使用方法:
該包內的主要函數是facet_geo(), 它的用法可以類比ggplot2的內置分面函數facet_warp()acet_grid()(當然在輸出方式上略有不同)。 只要你已經熟練掌握了ggplot2語法, 那麼你就可以輕鬆搞定這個包。
接下來讓我們展示一個例子, 該包內置了一個資料集——state_ranks。
head(state_ranks)
這是一個包含美國各州不同社會指標優略程度的資料集(按照排名由低到高排序)。
然後讓我們使用geofacet來給每一個州都創造一個直條圖, 我僅需使用一個ggplot2內的geom_col()函數即可, 至於分面參數, 這裡我們摒棄使用傳統的facet_wrap()分面函數, 而是使用geofacet包提供的facet_geo()函數來替代。
定義一個主題:
mytheme
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(axis.line = element_blank(), axis.text.x=element_blank(),
axis.ticks=element_blank(), axis.title = element_blank(),
panel.background=element_blank(),panel.border =element_blank(),
panel.grid = element_blank(), plot.background = element_blank(),
strip.background=element_blank(),legend.position = c(0.9,0.15))
}
分面直條圖:
ggplot(state_ranks, aes(variable,rank,fill=variable)) +
geom_col() +
coord_flip() +
scale_fill_wsj()+
facet_geo(~state)+
mytheme()
geofacet內部重要參數:
grid參數:可以理解為網格id, 可以選擇內建的id名稱, 或者是提供一個自建的已經命名有網格名稱的資料框。
label參數:可以指定任何我們想要指定的變數作為網格顯示的標籤。
以下是兩一個自帶資料集的例子:
head(us_state_grid2)
這一次, 我們用來呈現美國季節調整後的失業率隨時間的變化。 使用對應州名作為對應網格標籤。
ggplot(state_unemp, aes(year, rate)) +
geom_line() +
facet_geo(~ state, grid = "us_state_grid2", label = "name") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
labs(title = "Seasonally Adjusted US Unemployment Rate 2000-2016",
caption = "Data Source: bls.gov",
x = "Year",
y = "Unemployment Rate (%)") +
theme(strip.text.x = element_text(size = 6))
指定網格非常容易,我們只需提供一個內含地區名稱和地區代碼的資料框即可。
ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
facet_geo(~ name, grid = "eu_grid1", scales = "free_y") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita in Relation to EU Index (100)") +
theme_bw()
以下是該包內已經內建好的,我們畫圖可利用的帶地區編碼的資料集。
get_grid_names()
[1] "us_state_grid1" "us_state_grid2" "eu_grid1" "aus_grid1" "sa_prov_grid1" "london_boroughs_grid"
[7] "nhs_scot_grid" "india_grid1" "india_grid2" "argentina_grid1" "br_grid1"
OMG,WAKM ,竟然沒有China,這不科學啊(等我弄明白了我親自給大家做一個)。
接下來是其他國家的幾個例子!
#歐盟成員國GDP增長情況:
ggplot(aus_pop, aes(age_group, pop / 1e6, fill = age_group)) +
geom_col() +
facet_geo(~ code, grid = "aus_grid1") +
coord_flip() +
labs(
title = "Australian Population Breakdown",
caption = "Data Source: ABS Labour Force Survey, 12 month average",
y = "Population [Millions]") +
theme_bw()
#澳大利亞人口分組視覺化:
ggplot(aus_pop, aes(age_group, pop /1e6, fill = age_group)) + geom_col() + facet_geo(~ code, grid ="aus_grid1") + coord_flip() + labs( title ="Australian Population Breakdown", caption ="Data Source: ABS Labour Force Survey, 12 month average", y ="Population [Millions]") + theme_bw()
#南非
ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ province, grid = "sa_prov_grid1") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
#關於倫敦房價
ggplot(london_afford, aes(x = year, y = starts, fill = year)) +
geom_col(position = position_dodge()) +
facet_geo(~ code, grid = "london_boroughs_grid", label = "name") +
labs(title = "Affordable Housing Starts in London",
subtitle = "Each Borough, 2015-16 to 2016-17",
caption = "Source: London Datastore", x = "", y = "")
#蘇格蘭居民牙齒健康程度
ggplot(nhs_scot_dental, aes(x = year, y = percent)) +
geom_line() +
facet_geo(~ name, grid = "nhs_scot_grid") +
scale_x_continuous(breaks = c(2004, 2007, 2010, 2013)) +
scale_y_continuous(breaks = c(40, 60, 80)) +
labs(title = "Child Dental Health in Scotland",
subtitle = "Percentage of P1 children in Scotland with no obvious decay experience.",
caption = "Source: statistics.gov.scot", x = "", y = "")
#印度人口分佈
ggplot(subset(india_pop, type == "state"),
aes(pop_type, value / 1e6, fill = pop_type)) +
geom_col() +
facet_geo(~ name, grid = "india_grid2", label = "code") +
labs(title = "Indian Population Breakdown",
caption = "Data Source: Wikipedia",
x = "",
y = "Population [Millions]") +
theme_bw() +
theme(axis.text.x = element_text(angle = 40, hjust = 1))
2016年美國總統大選:
ggplot(election, aes("", pct, fill = candidate)) +
geom_col(alpha = 0.8, width = 1) +
scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
facet_geo(~ state, grid = "us_state_grid2") +
scale_y_continuous(expand = c(0, 0)) +
labs(title = "2016 Election Results",
caption = "Data Source: http://bit.ly/2016votecount",
x = NULL,
y = "Percentage of Voters") +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.text.x = element_text(size = 6))
換成橫條圖:
ggplot(election, aes(candidate, pct, fill = candidate)) +
geom_col() +
scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
facet_geo(~ state, grid = "us_state_grid2") +
theme_bw() +
coord_flip() +
labs(title = "2016 Election Results",
caption = "Data Source: http://bit.ly/2016votecount",
x = NULL,
y = "Percentage of Voters") +
theme(strip.text.x = element_text(size = 6))
好了就寫這幾個吧,看完是不是覺得這個包很牛掰啊哈哈哈~_~
我也是被他給驚豔到才立馬寫出來分享給大家,不過可惜的是這些只能使用內建資料,如果你要呈現的地域包含在內建的地區裡面,應該是可以用的,但是內部沒有定義的地區編碼,需要自己使用JS編輯器定義、提交、審核,灰常麻煩,但是我有信心把源碼搞明白,然後寫一套可以自訂的地區分面系統。(不知道要猴年馬月才能出來哈哈哈~)
其他精彩薦讀:
學習R語言我都做了那些有趣的事情!
基於R軟體對qq消息視覺化給分析的實現
R語言視覺化實戰:上帝視角—給世界一個特寫~
R語言繪製漫畫圖表(代碼全)
天善學院svip正火爆報名中!包含Excel BI、Python3爬蟲案例、Python機器學習、Python資料科學家、大資料體系、資料分析報告、資料分析師體系、深度學習、R語言案例共10套課程,其他課程只需五折即可,歡迎大家關注報名!
指定網格非常容易,我們只需提供一個內含地區名稱和地區代碼的資料框即可。
ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
facet_geo(~ name, grid = "eu_grid1", scales = "free_y") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita in Relation to EU Index (100)") +
theme_bw()
以下是該包內已經內建好的,我們畫圖可利用的帶地區編碼的資料集。
get_grid_names()
[1] "us_state_grid1" "us_state_grid2" "eu_grid1" "aus_grid1" "sa_prov_grid1" "london_boroughs_grid"
[7] "nhs_scot_grid" "india_grid1" "india_grid2" "argentina_grid1" "br_grid1"
OMG,WAKM ,竟然沒有China,這不科學啊(等我弄明白了我親自給大家做一個)。
接下來是其他國家的幾個例子!
#歐盟成員國GDP增長情況:
ggplot(aus_pop, aes(age_group, pop / 1e6, fill = age_group)) +
geom_col() +
facet_geo(~ code, grid = "aus_grid1") +
coord_flip() +
labs(
title = "Australian Population Breakdown",
caption = "Data Source: ABS Labour Force Survey, 12 month average",
y = "Population [Millions]") +
theme_bw()
#澳大利亞人口分組視覺化:
ggplot(aus_pop, aes(age_group, pop /1e6, fill = age_group)) + geom_col() + facet_geo(~ code, grid ="aus_grid1") + coord_flip() + labs( title ="Australian Population Breakdown", caption ="Data Source: ABS Labour Force Survey, 12 month average", y ="Population [Millions]") + theme_bw()
#南非
ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ province, grid = "sa_prov_grid1") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
#關於倫敦房價
ggplot(london_afford, aes(x = year, y = starts, fill = year)) +
geom_col(position = position_dodge()) +
facet_geo(~ code, grid = "london_boroughs_grid", label = "name") +
labs(title = "Affordable Housing Starts in London",
subtitle = "Each Borough, 2015-16 to 2016-17",
caption = "Source: London Datastore", x = "", y = "")
#蘇格蘭居民牙齒健康程度
ggplot(nhs_scot_dental, aes(x = year, y = percent)) +
geom_line() +
facet_geo(~ name, grid = "nhs_scot_grid") +
scale_x_continuous(breaks = c(2004, 2007, 2010, 2013)) +
scale_y_continuous(breaks = c(40, 60, 80)) +
labs(title = "Child Dental Health in Scotland",
subtitle = "Percentage of P1 children in Scotland with no obvious decay experience.",
caption = "Source: statistics.gov.scot", x = "", y = "")
#印度人口分佈
ggplot(subset(india_pop, type == "state"),
aes(pop_type, value / 1e6, fill = pop_type)) +
geom_col() +
facet_geo(~ name, grid = "india_grid2", label = "code") +
labs(title = "Indian Population Breakdown",
caption = "Data Source: Wikipedia",
x = "",
y = "Population [Millions]") +
theme_bw() +
theme(axis.text.x = element_text(angle = 40, hjust = 1))
2016年美國總統大選:
ggplot(election, aes("", pct, fill = candidate)) +
geom_col(alpha = 0.8, width = 1) +
scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
facet_geo(~ state, grid = "us_state_grid2") +
scale_y_continuous(expand = c(0, 0)) +
labs(title = "2016 Election Results",
caption = "Data Source: http://bit.ly/2016votecount",
x = NULL,
y = "Percentage of Voters") +
theme(axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
strip.text.x = element_text(size = 6))
換成橫條圖:
ggplot(election, aes(candidate, pct, fill = candidate)) +
geom_col() +
scale_fill_manual(values = c("#4e79a7", "#e15759", "#59a14f")) +
facet_geo(~ state, grid = "us_state_grid2") +
theme_bw() +
coord_flip() +
labs(title = "2016 Election Results",
caption = "Data Source: http://bit.ly/2016votecount",
x = NULL,
y = "Percentage of Voters") +
theme(strip.text.x = element_text(size = 6))
好了就寫這幾個吧,看完是不是覺得這個包很牛掰啊哈哈哈~_~
我也是被他給驚豔到才立馬寫出來分享給大家,不過可惜的是這些只能使用內建資料,如果你要呈現的地域包含在內建的地區裡面,應該是可以用的,但是內部沒有定義的地區編碼,需要自己使用JS編輯器定義、提交、審核,灰常麻煩,但是我有信心把源碼搞明白,然後寫一套可以自訂的地區分面系統。(不知道要猴年馬月才能出來哈哈哈~)
其他精彩薦讀:
學習R語言我都做了那些有趣的事情!
基於R軟體對qq消息視覺化給分析的實現
R語言視覺化實戰:上帝視角—給世界一個特寫~
R語言繪製漫畫圖表(代碼全)
天善學院svip正火爆報名中!包含Excel BI、Python3爬蟲案例、Python機器學習、Python資料科學家、大資料體系、資料分析報告、資料分析師體系、深度學習、R語言案例共10套課程,其他課程只需五折即可,歡迎大家關注報名!