数据主题:Rap Artists
数据源:
https://github.com/gkaramanis/tidytuesday/tree/master/2020-week16/data
依然是第十六周的主题:
前置知识
- futurevisions包
https://github.com/JoeyStanley/futurevisions
devtools::install_github("JoeyStanley/futurevisions")
基于NASA航天局visions-of-the-future的调色包
个人感觉用色鲜明,饱和度高,还挺好看的
# 两个核心函数
futurevisions("mars")
# [1] "#DB3A2F" "#EAB33A" "#275D8E" "#902A57" "#F7EBD3" "#0B0C0B"
show_palette("mars")
ggplot(mpg, aes(cty, hwy, color = factor(cyl))) +
geom_jitter() +
scale_color_manual(values = futurevisions("mars"))
含梯度颜色5类,离散颜色4类,分类颜色11类
- rev函数
逆转向量顺序
x <- c(1,2,3)
rev(x)
# [1] 3 2 1
# 等同于
x[length(x):1]
futurevisions("cancri")[1:5]
# [1] "#343854" "#8C384D" "#CF2438" "#D95E31" "#F0C742"
rev(futurevisions("cancri")[1:5])
# [1] "#F0C742" "#D95E31" "#CF2438" "#8C384D" "#343854"
- 关于内置主题
参考:
https://dataxujing.github.io/ggplot2%E7%9A%84%E4%B8%BB%E9%A2%98%E6%A8%A1%E6%9D%BF/
# ggplot2内置主题主要包括
theme_gray() # 默认
theme_bw()
theme_linedraw()
theme_light()
theme_dark()
theme_minimal()
theme_classic()
theme_void()
library(ggplot2)
library(gridExtra)
p <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))
grid.arrange(p + theme_gray() + ggtitle("theme_gray"),
p + theme_bw() + ggtitle("theme_bw"),
p + theme_linedraw() + ggtitle("theme_linedraw"),
p + theme_light() + ggtitle("theme_light"),
p + theme_dark() + ggtitle("theme_dark"),
p + theme_minimal() + ggtitle("theme_minimal"),
p + theme_classic() + ggtitle("theme_classic"),
p + theme_void() + ggtitle("theme_void"),ncol=2)
本例用的是theme_void
,基本只保留数据图本身(点 线 条)
原图复现
- 导入包和数据
library(tidyverse)
library(magrittr)
library(janitor)
library(ggimage)
# remotes::install_github("wilkelab/ggtext")
# https://github.com/wilkelab/ggtext
library(ggtext)
# devtools::install_github("JoeyStanley/futurevisions")
library(futurevisions)
windowsFonts(HEL=windowsFont("Helvetica CE 55 Roman"),
RMN=windowsFont("Times New Roman"),
ARL=windowsFont("Arial"),
ARLB=windowsFont("Arial Bold"),
JBM=windowsFont("JetBrains Mono"))
# 使用rio包对于文件编码的处理不智能
?rio::import
rankings <- readr::read_csv('https://raw.githubusercontent.com/gkaramanis/tidytuesday/master/2020-week16/data/data-kOlSQ.csv')
- 数据处理
# 新学的操作,不过其实Hadley大佬不建议用%<>%
rankings %<>%
# 可以将重复列区分开
janitor::clean_names() %>%
# 选择列的时候可以直接rename
select(title_artist = song_artist, img = spotify_thumb_sm, 3:15) %>%
filter(n > 4) %>%
mutate(
rank = row_number(),
title_artist = str_remove(title_artist, "!\\[\\].+"),
title_artist = str_replace(title_artist, "\\*\\* ", "\\*\\*"),
img = ifelse(str_detect(img, "data"), NA, img),
img = ifelse(str_detect(title, "Shook"), "https://i.scdn.co/image/ab67616d00004851a2203fa0656cede30f879b97", img), # fix image for this track
# 因子重排序默认降序
title = fct_reorder(title, critic_rating),
n_1 = n1,
n_2_5 = n2 + n3 + n4 + n5
) %>%
pivot_longer(cols = n1:n4, names_to = "votes", values_to = "votes_n")
- 画图
p_img <- ggplot(rankings) +
geom_bar(data = subset(rankings, votes == "n1"),
aes(votes_n, title,fill = votes),
# 决定柱形图方向
orientation = "y",
# position = "stack", # 柱形纵向堆叠,dodge是横向并列
# 本质下一行代码一样,实际上这里不需要写
position = position_stack(reverse = FALSE),
stat = "identity",
width = 0.8) +
geom_bar(data = subset(rankings, votes != "n1"),
aes(-votes_n, title, fill = votes),
orientation = "y",
position = position_stack(reverse = TRUE),
stat = "identity",
width = 0.8) +
# n_1 + 1.5 明确了图片和右侧柱形的相对映射位置
# image通过url将图片载入图层
# asp是横纵比
geom_image(aes(n_1 + 1.5, title, image = img), size = 0.029, asp = 1.375)
p <- p_img +
geom_richtext(aes(n_1 + 2.5, title, label = title_artist),
size = 7, family = "ARL",
hjust = 0, vjust = 0.5, fill = NA, label.color = NA, lineheight = 1.1) +
annotate("text", x = 35, y = 13,
label = "BBC Music’s\ngreatest hip-hop\nsongs of all time",
hjust = 1, vjust = 0, colour = "black", family = "JBMB", size = 18, lineheight = 1) +
annotate("text", x = 35, y = 10.9,
label = "Distribution of critics' votes\nfor songs with 5 votes or more",
hjust = 1, vjust = 0, colour = "black", family = "ARL", size = 12, lineheight = 1) +
# 添加中白线,美化
geom_vline(xintercept = 0, color = "white") +
# 控制x轴范围
scale_x_continuous(limits = c(-12, 35)) +
scale_fill_manual(values = rev(futurevisions("cancri")[1:5]),
labels = c("1st fav.", "2nd fav.", "3rd fav.", "4th fav.", "5th fav."),
guide = guide_legend(direction = "horizontal",
title = "",
title.position = "top",
title.hjust = 0.5,
label.position = "bottom")) +
labs(
caption = "Source: BBC Music/Datawrapper | Graphic: Georgios Karamanis"
) +
theme_void() +
theme(
legend.position = c(0.815, 0.45),
legend.spacing.x = unit(0, "pt"),
legend.key.width = unit(90, "pt"),
legend.text = element_text(family = "IBM Plex Sans Bold", size = 10),
# legend.title = element_text(family = "IBM Plex Serif Bold", size = 15),
# 填充画面背景色
plot.background = element_rect(fill = "#a7a6ba", colour = NA),
plot.caption = element_text(family = "IBM Plex Sans", size = 12, hjust = 0.96),
# 调节画面边缘间距,很实用
plot.margin = margin(20, 20, 20, 20)
) +
ggsave(here::here(".",paste0("rap-artists-likert-", format(Sys.time(), "%Y%m%d"), ".png")),
dpi = 320, width = 22, height = 16)
网友评论