颜色偏好

颜色偏好

本文是学习zonination/colors的笔记,是一个关于颜色偏好调查的可视化。

准备

绘图所需的数据集:
colors.csv
responses-curated.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
32
33
34
35
36
37
38
39
40
41
setwd("~/Desktop/颜色偏好")
library(ggplot2)
library(ggpomological)
library(reshape2)
library(scales)
library(lubridate)
library(readr)
theme_set(theme_pomological(base_size = 20, base_family = "MLingWaiMedium-SC") + theme(plot.title = element_text(hjust = 0.5)) + theme(plot.subtitle = element_text(hjust = 0.5)))
# 首先绘制本次调查使用的颜色图谱
colors <- read_csv("colors.csv")
colors$hex <- paste("#",
as.hexmode(colors$R),
as.hexmode(colors$G),
as.hexmode(colors$B),
sep = "")
# 中文化
colors$Family[which(colors$Family == "Reds")] = "红色"
colors$Family[which(colors$Family == "Oranges")] = "橘色"
colors$Family[which(colors$Family == "Yellows")] = "黄色"
colors$Family[which(colors$Family == "Greens")] = "绿色"
colors$Family[which(colors$Family == "Cyans")] = "青色"
colors$Family[which(colors$Family == "Blues")] = "蓝色"
colors$Family[which(colors$Family == "Violets")] = "紫罗兰"
colors$Family[which(colors$Family == "Magentas")] = "洋红"
colors$Family[which(colors$Family == "Blacks")] = "黑色"
colors$Family[which(colors$Family == "Greys")] = "灰色"
colors$Family[which(colors$Family == "Whites")] = "白色"
colors$Family <- factor(colors$Family,
c(
"红色", "橘色", "黄色", "绿色",
"青色", "蓝色", "紫罗兰", "洋红",
"黑色", "灰色", "白色"
))
ggplot(colors, aes(x = 1, y = Category)) +
geom_tile(aes(fill = Category), colour = "black") +
scale_fill_manual("", values = colors$hex) +
guides(fill = "none") +
labs(title = "颜色定义",
x = "", y = "") +
facet_grid(Family~., scales = "free_y", space = "free_y") +
theme(axis.text.x = element_blank())

颜色偏好的性别差异

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
56
57
58
59
60
61
62
63
64
65
66
67
68
# 读入调查数据
resp <- read.csv("responses-curated.csv", stringsAsFactors = F)
names(resp) <- c("时间", "颜色", "性别", "年龄", "职业性格")
resp$时间 <- strptime(resp$时间, "%m/%d/%Y %H:%M:%S")

# 过滤掉不合格的回答
resp$状态 <- "回答"
for(n in 1:nrow(resp)){
if(resp$颜色[n] == "Troll"){
resp$状态[n] <- "拒绝"
}
if(resp$颜色[n] == ""){
resp$状态[n] <- "拒绝"
}
if(resp$性别[n] == "Troll"){
resp$状态[n] <- "拒绝"
}
if(resp$性别[n] == ""){
resp$状态[n] <- "拒绝"
}
}
rm(n)
resp2 <- subset(resp, 状态 == "回答")
# 颜色中文化
resp2$颜色[which(resp2$颜色 == "Red")] = "红色"
resp2$颜色[which(resp2$颜色 == "Orange")] = "橘色"
resp2$颜色[which(resp2$颜色 == "Yellow")] = "黄色"
resp2$颜色[which(resp2$颜色 == "Green")] = "绿色"
resp2$颜色[which(resp2$颜色 == "Cyan")] = "青色"
resp2$颜色[which(resp2$颜色 == "Blue")] = "蓝色"
resp2$颜色[which(resp2$颜色 == "Violet")] = "紫罗兰"
resp2$颜色[which(resp2$颜色 == "Magenta")] = "洋红"
resp2$颜色[which(resp2$颜色 == "Pink")] = "紫色"
resp2$颜色[which(resp2$颜色 == "Black")] = "黑色"
resp2$颜色[which(resp2$颜色 == "Brown")] = "棕色"
resp2$颜色[which(resp2$颜色 == "Grey")] = "灰色"
resp2$颜色[which(resp2$颜色 == "White")] = "白色"
resp2$颜色[which(resp2$颜色 == "Other")] = "其它"

resp2$性别[which(resp2$性别 == "Male")] = "男性"
resp2$性别[which(resp2$性别 == "Female")] = "女性"
resp2$性别[which(resp2$性别 == "Other")] = "其它"

# 因子化
resp2$颜色 <- factor(resp2$颜色,
c("红色", "橘色", "黄色", "绿色",
"青色", "蓝色", "紫罗兰", "洋红",
"紫色", "棕色", "白色", "黑色",
"灰色", "其它"))
resp2$性别 <- factor(resp2$性别,
rev(c("男性", "女性", "其它")))

# 按性别分类的图
ggplot(resp2, aes(性别)) +
geom_bar(stat = "count",
position = "fill",
aes(fill = 颜色),
alpha = 0.8,
colour = "black") +
scale_fill_manual("颜色", values = c(
"#FF0000","#FF8000","#FFFF00","#00C000","#00FF80",
"#0000FF","#8000FF","#FF00FF","#FF80FF","#804000",
"#FFFFFF","#000000","#808080","#F0F0F0"
)) +
scale_y_continuous(labels = scales::percent_format()) +
labs(title = "图:颜色偏好的性别差异",
x = "", y = "") +
coord_flip()

颜色偏好的年龄差异

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 按年龄分类
ggplot(resp2, aes(年龄)) +
geom_histogram(position = "fill",
binwidth = 5,
aes(fill = 颜色),
alpha = 0.8,
colour = "black") +
scale_fill_manual("颜色", values = c(
"#FF0000","#FF8000","#FFFF00","#00C000","#00FF80",
"#0000FF","#8000FF","#FF00FF","#FF80FF","#804000",
"#FFFFFF","#000000","#808080","#F0F0F0"
)) +
scale_y_continuous(labels = scales::percent_format()) +
scale_x_continuous(limits = c(12.5, 42.5)) +
labs(title = "图:颜色偏好的年龄差异",
x = "年龄", y = "") +
facet_grid(.~性别) +
theme(strip.text.x = element_text(size = 12, colour = "#525252")) +
theme(strip.text.y = element_text(size = 12, colour = "#525252"))

颜色偏好的职业性格差异

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
# 按职业性格类型分类
resp2$temp <- NA
for(n in 1:nrow(resp2)){
if(resp2$职业性格[n]=="INTJ" | resp2$职业性格[n]=="INTP" | resp2$职业性格[n]=="ENTJ" | resp2$职业性格[n]=="ENTP" ){resp2$temp[n]<-"理性人"}
if(resp2$职业性格[n]=="INFJ" | resp2$职业性格[n]=="INFP" | resp2$职业性格[n]=="ENFJ" | resp2$职业性格[n]=="ENFP" ){resp2$temp[n]<-"理想主义者"}
if(resp2$职业性格[n]=="ISTP" | resp2$职业性格[n]=="ISFP" | resp2$职业性格[n]=="ESTP" | resp2$职业性格[n]=="ESFP" ){resp2$temp[n]<-"工匠"}
if(resp2$职业性格[n]=="ISTJ" | resp2$职业性格[n]=="ISFJ" | resp2$职业性格[n]=="ESTJ" | resp2$职业性格[n]=="ESFJ" ){resp2$temp[n]<-"守护者"}
}

ggplot(resp2, aes(x = 职业性格)) +
geom_bar(position = "fill",
stat = "count",
aes(fill = 颜色),
alpha = 0.8,
colour = "black") +
scale_fill_manual("颜色", values = c(
"#FF0000","#FF8000","#FFFF00","#00C000","#00FF80",
"#0000FF","#8000FF","#FF00FF","#FF80FF","#804000",
"#FFFFFF","#000000","#808080","#F0F0F0"
)) +
scale_y_continuous(labels = scales::percent_format()) +
labs(title = "图:颜色偏好的职业性格差异", x = "", y = "") +
facet_grid(temp~., scale = "free_y", space = "free_y") +
coord_flip() +
theme(strip.text.x = element_text(size = 12, colour = "#525252")) +
theme(strip.text.y = element_text(size = 12, colour = "#525252"))

# R

评论

Your browser is out-of-date!

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

×