ggplo2經(jīng)典可視化案例(1)

通過Tidytuesday Week 52 - Big Mac Index數(shù)據(jù)繪制高端可視化圖,數(shù)據(jù)可視化的經(jīng)典案例项鬼,各位看官老爺細(xì)細(xì)品味耗帕,喜歡請關(guān)注個人公眾號R語言數(shù)據(jù)分析指南持續(xù)分享更多優(yōu)質(zhì)資源

加載所需R包

rm(list=ls())
library(tidyverse)
library(lubridate)
library(patchwork)
# install.packages("fuzzyjoin")
library(fuzzyjoin)

數(shù)據(jù)清洗

bigmac <- read.delim("big-mac.csv",header = T,sep=",",
                     check.names = F)
                     
eurozone_countries <- c("Austria", "Belgium", "Cyprus",
"Estonia", "Finland", "France", "Germany", "Greece",
"Ireland", "Italy", "Latvia", "Lithuania", "Luxembourg",
"Malta", "Netherlands", "Portugal", "Slovakia",
"Slovenia", "Spain")

eurozone <- tibble(iso_a3 = rep("EUZ", length(eurozone_countries)), 
                   currency = rep("EUR", length(eurozone_countries)),
                   name = eurozone_countries)

eurozone <- eurozone %>%
  fuzzyjoin::regex_left_join(select(maps::iso3166, mapname, a3), c(name = "mapname")) %>%
  select(-mapname)

date_list <- bigmac %>%
  filter(iso_a3 == "EUZ") %>%
  select(date, iso_a3) %>%
  distinct(date, iso_a3) 

eurobigmac <- date_list %>%
  inner_join(eurozone, by = c("iso_a3")) %>% 
  left_join(select(bigmac,-name), by = c("iso_a3", "date")) %>%
  mutate(iso_a3 = ifelse(!is.na(a3), a3, iso_a3)) %>%
  select(-a3)

bigmac <- bigmac %>%
  mutate(name = ifelse(name == "Euro area", "Eurozone", name)) %>%
  bind_rows(eurobigmac)

world_map <- map_data("world") %>%
  filter(region != "Antarctica") %>%
  as_tibble() %>%
  fuzzyjoin::regex_left_join(maps::iso3166, c(region = "mapname")) %>%
  left_join(filter(bigmac,date == ymd("2020-07-01")), by = c(a3 = "iso_a3"))

自定義函數(shù)繪制各個國家的散點(diǎn)圖

blue = "#0870A5"
red = "#DB444B"

chart <- function(country){
  
  data <- bigmac %>%
    filter(name == country) %>%
    mutate(valuation = ifelse(usd_raw >= 0, "Overvalued", "Undervalued"))
  min_axis <- ifelse(min(data$usd_raw) > 0, 0, min(data$usd_raw)) - 0.15
  max_axis <- ifelse(max(data$usd_raw) < 0, 0, max(data$usd_raw)) + 0.15
  min_date <- min(data$date)
  
  data %>%
    ggplot(aes(date, usd_raw)) +
    geom_point(aes(color = valuation), size = 2) +
    geom_hline(yintercept = 0, color = "grey50", linetype = "dashed") +
    geom_text(x = min_date, y = 0.1,
    label = "Overvalued", hjust = 0, color = blue) +
    geom_text(x = min_date, y = -0.1,
    label = "Undervalued",
    hjust = 0, color = red) +
    scale_color_manual(values = c("Overvalued" = blue,
    "Undervalued" = red)) +
    scale_y_continuous(limits = c(min_axis, max_axis),
    labels = scales::percent) +
    guides(color = FALSE)+
    labs(title = country) +
    theme(plot.background = element_rect(fill = NA,
    color = NA), panel.background = element_rect(fill = NA,color = NA),
    axis.title = element_blank(),
    axis.text = element_text(family = "heebo",size = 10),
    panel.grid.minor = element_blank(),
          axis.text.x=element_blank(),
          panel.grid.major = element_line(color = "grey80",
          linetype = "dotted"),
          plot.title = element_text(family = "heebo",
          size = 12))
}

用折線連接地圖上的國家

centre <- map_data("world") %>% tbl_df %>% 
  filter(region %in% c("Norway","Switzerland",
  "South Africa", "Argentina", "China", "Russia",
                       "Canada", "Mexico", "France", "New Zealand")) %>%
  group_by(region) %>%
  summarise(centx = mean(long),
            centy = mean(lat))

countries_lines <- tibble(x = c(-65.5,25.3,-110,-104,
99.2,107, 170, 8.31,16.2,3.23),
xend = c(-85, 65, -200, -200, 210,
210, 210, 170, 47, -60),
y = c(-37.7, -28.8, 60, 24.2, 63.5,
35, -40.9, 46.7, 60, 46.2),
yend = c(-75, -75, 70, 10, 75,
25, -50, 115, 115, 115))

繪制世界地圖

map <- ggplot() +
  geom_polygon(data = world_map, aes(long, lat, group = group, fill = usd_raw),
               color = "grey50", size = 0.3) +
  scale_fill_gradient2(low = "#F21A00", mid = "#E9C825", high = "#3B9AB2", midpoint = -0.3,
                       labels = scales::percent, na.value="grey80") +
  geom_segment(data = countries_lines, aes(x = x, xend = xend, y = y , yend = yend), color = "grey50", inherit.aes = FALSE) +
  scale_x_continuous(limits = c(-350, 350), expand = c(0,0)) +
  scale_y_continuous(limits = c(-130, 170)) +
  labs(fill = "Big Mac Index relative to USD") +
  guides(fill = guide_colorbar(title.position = "top",
                               label.position = "bottom",
                               title.hjust = 0.5,
                               barwidth = 20)) +
  theme_void() +
  theme(legend.position = c(0.12, 0.12),
        legend.direction = "horizontal",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10))

繪制各個國家的散點(diǎn)圖

swiss <- chart("Switzerland")
swiss
norway <- chart("Norway")
euro_area <- chart("Eurozone")
south_africa <- chart("South Africa")
russia <- chart("Russia")
china <- chart("China")
new_zealand <- chart("New Zealand")
argentina <- chart("Argentina")
mexico <- chart("Mexico")
canada <- chart("Canada")

設(shè)置圖片布局

final <- map + 
  inset_element(swiss,0.7,0.8,0.9,0.95) +
  inset_element(norway,0.5,0.8,0.7,0.95) +
  inset_element(euro_area,0.3,0.8,0.5,0.95) +
  inset_element(south_africa,0.55,0.05,0.75,0.20) +
  inset_element(russia,0.8,0.6,1,0.75) +
  inset_element(china,0.8,0.4,1,0.55) +
  inset_element(new_zealand,0.8,0.15,1,0.30) +
  inset_element(argentina,0.3,0.05,0.5,0.20) +
  inset_element(mexico,0,0.4,0.2,0.55) +
  inset_element(canada,0,0.6,0.2,0.75) +
  plot_annotation(
    title = "The Big Mac Index",
    caption = "Visualization: Christophe Nicault | Data: The Economist",
    theme = theme(plot.caption = element_text(family = "heebo", 
    size = 10, color = "#183170"),
plot.title = element_text(family = "oswald",
hjust = 0.5, size = 28, face = "bold",
color = "#183170", margin = margin(5,0,0,0))))

保存圖片

ggsave(final,file="big-index.png",device = NULL,
path = NULL,width = 25,height = 15,units = c("in"),dpi = 300)

數(shù)據(jù)鏈接:https://mp.weixin.qq.com/s/lyXPBs-B-HYdaUfsF7Fbdg

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末佣赖,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子表伦,更是在濱河造成了極大的恐慌谦去,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹦哼,死亡現(xiàn)場離奇詭異鳄哭,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)翔怎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門窃诉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人赤套,你說我怎么就攤上這事飘痛。” “怎么了容握?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵宣脉,是天一觀的道長。 經(jīng)常有香客問我剔氏,道長塑猖,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任谈跛,我火速辦了婚禮羊苟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘感憾。我一直安慰自己蜡励,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著凉倚,像睡著了一般兼都。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上稽寒,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天扮碧,我揣著相機(jī)與錄音,去河邊找鬼杏糙。 笑死慎王,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的宏侍。 我是一名探鬼主播柬祠,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼负芋!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起嗜愈,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤旧蛾,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后蠕嫁,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體锨天,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年剃毒,在試婚紗的時候發(fā)現(xiàn)自己被綠了病袄。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡赘阀,死狀恐怖益缠,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情基公,我是刑警寧澤幅慌,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站轰豆,受9級特大地震影響胰伍,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜酸休,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一骂租、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧斑司,春花似錦渗饮、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽特占。三九已至,卻和暖如春云茸,著一層夾襖步出監(jiān)牢的瞬間是目,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工标捺, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留懊纳,地道東北人。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓亡容,卻偏偏與公主長得像,于是被迫代替她去往敵國和親闺兢。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355