ggplo2经典可视化案例(1)

通过Tidytuesday Week 52 - Big Mac Index数据绘制高端可视化图,数据可视化的经典案例,各位看官老爷细细品味,喜欢请关注个人公众号R语言数据分析指南持续分享更多优质资源

加载所需R包

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

数据清洗

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"))

自定义函数绘制各个国家的散点图

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))

绘制各个国家的散点图

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")

设置图片布局

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)

数据链接:https://mp.weixin.qq.com/s/lyXPBs-B-HYdaUfsF7Fbdg

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 213,014评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,796评论 3 386
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,484评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,830评论 1 285
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,946评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,114评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,182评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,927评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,369评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,678评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,832评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,533评论 4 335
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,166评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,885评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,128评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,659评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,738评论 2 351