數(shù)據(jù)主題:Rap Artists
數(shù)據(jù)源:
https://github.com/gkaramanis/tidytuesday/tree/master/2020-week16/data
依然是第十六周的主題:
前置知識(shí)
- futurevisions包
https://github.com/JoeyStanley/futurevisions
devtools::install_github("JoeyStanley/futurevisions")
基于NASA航天局visions-of-the-future的調(diào)色包
個(gè)人感覺用色鮮明田晚,飽和度高,還挺好看的
# 兩個(gè)核心函數(shù)
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函數(shù)
逆轉(zhuǎn)向量順序
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"
- 關(guān)于內(nèi)置主題
參考:
https://dataxujing.github.io/ggplot2%E7%9A%84%E4%B8%BB%E9%A2%98%E6%A8%A1%E6%9D%BF/
# ggplot2內(nèi)置主題主要包括
theme_gray() # 默認(rèn)
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
喇喉,基本只保留數(shù)據(jù)圖本身(點(diǎn) 線 條)
原圖復(fù)現(xiàn)
- 導(dǎo)入包和數(shù)據(jù)
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包對(duì)于文件編碼的處理不智能
?rio::import
rankings <- readr::read_csv('https://raw.githubusercontent.com/gkaramanis/tidytuesday/master/2020-week16/data/data-kOlSQ.csv')
- 數(shù)據(jù)處理
# 新學(xué)的操作,不過其實(shí)Hadley大佬不建議用%<>%
rankings %<>%
# 可以將重復(fù)列區(qū)分開
janitor::clean_names() %>%
# 選擇列的時(shí)候可以直接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
# 因子重排序默認(rèn)降序
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是橫向并列
# 本質(zhì)下一行代碼一樣痘括,實(shí)際上這里不需要寫
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 明確了圖片和右側(cè)柱形的相對(duì)映射位置
# 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),
# 調(diào)節(jié)畫面邊緣間距挥唠,很實(shí)用
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)