九柱游戏得分可视化

九柱游戏得分可视化

本文是学习zonination/skittles的笔记,关于skittles这个游戏我不是很理解,但是他使用的数据集大概可以这么理解,有草莓、苹果、橙子、柠檬和葡萄五个人,玩了36轮游戏,然后每个人每轮游戏的得分。

首先准备好本次绘图需要的数据:
skittles.csv

准备工作:

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
setwd("~/Desktop")
skittles <- read.csv("skittles.csv")
library(ggplot2)
library(reshape2)

# 设定色板
skipalette <- c("#c0043f","#e64808","#f1be02","#048207","#441349")

# 设定表头和计算总体均值
names(skittles) <- c("轮", "草莓", "橙子", "柠檬", "苹果", "葡萄")
netmean <- mean(c(skittles$草莓, skittles$橙子, skittles$柠檬, skittles$苹果, skittles$葡萄))

# 归总数据
skitsum <- data.frame("flavor" = names(skittles)[2:6],
"mean" = sapply(skittles[, 2:6], mean),
"sd" = sapply(skittles[, 2:6], sd))
skitsum$flavor <- factor(skitsum$flavor,
c("草莓", "橙子", "柠檬", "苹果", "葡萄"))

skitmelt <- melt(skittles, id = "轮")
skitbin <- data.frame("轮" = NA, "variable" = NA, "value" = NA)
# 下面这个循环可以用来创建重叠点然后等下再用jitter分散开
for(n in 1:nrow(skitmelt)){
skitbin <- rbind(skitbin, skitmelt[rep(n, skitmelt$value[n]),])
}
skitbin <- skitbin[2:nrow(skitbin),]
skitbin$variable <- factor(skitbin$variable,
c("草莓", "橙子", "柠檬", "苹果", "葡萄"))
rm(n);rm(skitmelt)

theme_set(theme_bw(base_size = 20, base_family = "STSong") + theme(plot.title = element_text(hjust = 0.5)))

绘制散点图

1
2
3
4
5
6
7
8
9
10
ggplot(skitbin, aes(x = 轮, y = variable)) +
geom_jitter(aes(fill = variable), height = 5/16, width = 5/16, shape = 21, size = 3) +
geom_tile(colour = "grey75", alpha = 0) +
scale_fill_manual("Flavor", values = skipalette) +
guides(fill = "none") +
scale_x_continuous(breaks = 1:36, minor_breaks = seq(0.5, 36.5, 1)) +
scale_y_discrete(limits = rev(levels(melt(skittles, id = "轮")$variable))) +
labs(title = "图:九柱游戏计数",
x = "轮数") +
theme(panel.grid = element_blank(), axis.title.y = element_blank())

绘制热力图

1
2
3
4
5
6
7
8
9
10
11
12
13
ggplot(melt(skittles, id = "轮"), aes(x = 轮, y = variable)) +
geom_tile(aes(fill = variable, alpha = value), colour = "white") +
geom_text(aes(label = value), size = 3) +
scale_fill_manual("Flavor", values = skipalette) +
guides(alpha = "none", fill = "none") +
scale_x_continuous(breaks = 1:36, minor_breaks = NULL) +
scale_y_discrete(limits = rev(levels(melt(skittles, id = "轮")$variable))) +
labs(title = "图:九柱游戏计数",
subtitle = "原始数据转换成热力图",
x = "轮数") +
theme(plot.subtitle = element_text(hjust = 0.5)) +
theme(panel.grid = element_blank()) +
theme(axis.title.y = element_blank())

堆叠柱形图

1
2
3
4
5
6
7
8
9
10
ggplot(melt(skittles, id = "轮"), aes(轮, value)) +
geom_bar(stat = "identity", aes(fill = variable), alpha = 0.8) +
scale_fill_manual("Flavor", values = skipalette) +
labs(title = "图:九柱游戏计数",
subtitle = "原始数据转换成堆叠柱形图",
x = "轮数", y = "九柱戏计数") +
scale_x_continuous(breaks = 1:36, minor_breaks = NULL) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
theme(panel.grid = element_blank()) +
theme(axis.title.y = element_blank())

小提琴图+点图

1
2
3
4
5
6
7
8
9
10
11
12
ggplot(melt(skittles, id = "轮"), aes(variable,  value)) +
geom_violin(aes(fill = variable), alpha = 0.8) +
geom_dotplot(binaxis = "y", stackdir = "center", aes(fill = variable)) +
scale_fill_manual("Flavor", values = skipalette) +
guides(fill = "none") +
labs(title = "Skittles Flavor Distribution",
subtitle = "Violinplot with added Dotplot",
x = "Flavor", y = "Skittle Count") +
scale_y_continuous() +
geom_hline(yintercept = netmean, linetype = 4) +
theme(plot.subtitle = element_text(hjust = 0.5)) +
theme(panel.grid = element_blank())

误差棒图

1
2
3
4
5
6
7
8
9
10
11
ggplot(skitsum, aes(x = flavor, y = mean)) +
geom_bar(stat = "identity", aes(fill = flavor), alpha = 0.8, color = "black") +
geom_errorbar(aes(ymax = mean + 1.96*sd, ymin = mean - 1.96*sd), width = 0.2, size = 1, alpha = 0.5) +
scale_fill_manual("Flavor", values = skipalette) +
guides(fill = "none") +
labs(title = "Skittles Flavor Distribution",
subtitle = "Statistical Summary of Skittles Flavors",
x = "Flavor", y = "Mean Skittle Count\nError bars are 95% Confidence Intervals")+
geom_hline(yintercept = netmean, linetype = 4)+
theme(plot.subtitle = element_text(hjust = 0.5)) +
theme(panel.grid = element_blank())

# R

评论

Your browser is out-of-date!

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

×