蝙蝠侠电视剧中的反派

蝙蝠侠电视剧中的反派

本文是学习Holy ifelse() statements Batman!的笔记,绘制了蝙蝠侠电视剧中37个反派出现的季数和集数的网络图。

首先需要从A Visual Guide to All 37 Villains in the Batman TV Series爬取每个反派出现的季数和集数的数据。

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
library(rvest)
library(ggraph)
library(igraph)
library(tidyverse)

chars <- read_html('http://mentalfloss.com/article/60213/visual-guide-all-37-villains-batman-tv-series') %>%
html_nodes('#article-1 > div:nth-child(1) > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(2) > h4') %>%
html_text() %>%
data.frame(stringsAsFactors = F)
chars$name = sub(".+?. ", "", chars$.)
chars$id <- as.integer(lapply(strsplit(
chars$., '. '), '[', 1))

apps <- read_html('http://mentalfloss.com/article/60213/visual-guide-all-37-villains-batman-tv-series') %>%
html_nodes('strong i') %>%
html_text() %>%
data.frame(stringsAsFactors = F)

apps$id <- seq(1:37)
villians <- inner_join(apps, chars,
by = c('id'))

# 清理和组织数据
raw.seasons <- separate_rows(villians,
..x, sep = "SEASON ")
raw.seasons$..y <- as.integer(
unlist(lapply(strsplit(
raw.seasons$..x, ' *'
), '[', 1)))

raw.seasons <- separate_rows(
raw.seasons,
..x,
sep = "([^0-9])")

raw.seasons$..x <- as.numeric(raw.seasons$..x)

batman <- subset(raw.seasons, !is.na(..x))
names(batman)[1:4] <- c(
'to', 'from', 'season', 'char'
)

# to变量的第一个数字表示季,后面的数字表示集,是网络图的尾端
batman$to <- paste0(batman$season,
batman$to)
# from变量是反派角色的名字,由于名字很长,中间加个换行符,是网络图的节点
batman$from <- batman$char
batman$from <- gsub(' \\(', '\n\\(',
batman$char)
graph <- graph_from_data_frame(batman)
V(graph)$degree <- degree(graph)

n.names <- grep("[[:digit:]]",
V(graph)$name,
value = T)

绘图:

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ggraph(graph, layout = 'fr') +
geom_edge_link(aes(
colour = factor(season)
)) +
geom_node_point(aes(
size = ifelse(V(graph)$name %in% n.names, 1, degree)),
colour = ifelse(V(graph)$name %in% n.names, '#363636', '#ffffff'),
show.legend = F) +
theme_graph(background = 'grey20',
text_colour = 'white',
base_family = 'STSongti-SC-Bold',
base_size = 10,
subtitle_size = 10,
title_size = 22) +
theme(legend.position = 'bottom') +
scale_edge_color_brewer('Season',
palette = 'Dark2') +
geom_node_text(aes(
label = name),
color = ifelse(V(graph)$name %in% n.names, 'grey', 'white'),
size = ifelse(V(graph)$name %in% n.names,1.75 , 2.5),
repel = T,
check_overlap = T) +
labs(
title = '蝙蝠侠中的恶棍',
subtitle = '节点表示蝙蝠侠电视剧的1——3季中的37个恶棍,尾端的数字表示出现的季和集数。',
caption = 'ggraph的作者:http://www.data-imaginist.com/\n 数据来源: http://mentalfloss.com/article/60213/visual-guide-all-37-villains-batman-tv-series')

# R

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×