其他
ggplot2绘制围棋棋局
欢迎关注R语言数据分析指南
❝春节假期结束了又开始愉快的更新文档了,本节来介绍如何通过「ggplot2」来绘制一盘围棋,其实质就是散点图的修饰而已主要体会后面的绘图设计不必计较前期的数据处理过程
❞
加载R包
library(tidyverse)
导入数据
ratings <- read_csv("ratings.csv")
details <- read_csv("details.csv")
数据清洗
ratings %>% inner_join(details, by='id') %>%
mutate(category:=lapply(lapply(str_split(boardgamecategory, ','),
str_trim),str_replace_all, '[[:punct:]]','')) %>%
unnest(category) %>%
mutate(mechanic:=lapply(lapply(str_split(boardgamemechanic, ','),
str_trim),str_replace_all, '[[:punct:]]','')) %>%
unnest(mechanic) -> df_full_unnested
details %>%
mutate(category:=lapply(lapply(str_split(boardgamecategory,','),
str_trim), str_replace_all,'[[:punct:]]','')) %>%
unnest(category) -> df_category
df_category %>%
count(category) %>%
arrange(desc(n)) %>%
top_n(19) %>%
pull(category) -> top_categories
details %>%
mutate(mechanic:=lapply(lapply(str_split(boardgamemechanic,','),
str_trim),str_replace_all,'[[:punct:]]','')) %>%
unnest(mechanic) -> df_mechanic
df_mechanic %>%
count(mechanic) %>%
arrange(desc(n)) %>%
filter(!is.na(mechanic)) %>%
top_n(19) %>%
pull(mechanic) -> top_mechanics
数据可视化
df_full_unnested %>%
filter(category %in% top_categories, mechanic %in% top_mechanics) %>%
group_by(category, mechanic) %>%
summarise(avg_rating := mean(average)) %>%
mutate(global_mean:=mean(avg_rating)) %>%
mutate(rating_level:=ifelse(avg_rating>=global_mean,'over', 'under')) %>%
ggplot() +
geom_point(aes(category, reorder(mechanic, avg_rating), color=rating_level), size=5) +
scale_color_manual(values=c('black','white')) +
coord_equal() +
theme_minimal() +
theme(text=element_text(family='Cinzel', color='#e5e5e5'),
panel.grid=element_line(color='black', size=0.5),
panel.background=element_rect(fill='#b58225', color='#b58225'),
legend.position = 'none',
plot.margin=margin(1,2,1,0,'cm'),
axis.text.x = element_text(angle=45, hjust=1),
axis.title=element_blank(),
axis.text=element_text(color='black', size=8))
❝好了今天的介绍到此结束,转发此文到朋友圈或者对此文打赏任意金额即可获取数据及代码,感谢各位的支持
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
「关注下方公众号下回更新不迷路」添加作者微信请备注单位+方向+姓名
2022-02-06
2022-01-28
2022-01-26
2022-01-25
2022-01-24
2022-01-18