ggplot2 中的配色方案 facet_wrap
Color Scheme In ggplot2 facet_wrap
我正在制作一个多重散点图来显示交互。我使用 reshape2 包中的 melt 函数使我的数据看起来像这样:
head(wage)
money educ exper tenure nonwhite female married numdep smsa Region Industry
1 3.10 11 2 0 White Female Notmarried 2 1 west other
2 3.24 12 22 2 White Female Married 3 1 west services
3 3.00 11 2 0 White Male Notmarried 2 0 west trade
4 6.00 8 44 28 White Male Married 0 1 west clerocc
5 5.30 12 7 2 White Male Married 1 0 west other
6 8.75 16 9 8 White Male Married 0 1 west profserv
test1 = wage %>% select(money, educ, female, nonwhite, married, smsa, Region, Industry)
test1a = melt(test1, id.vars= c('money', 'educ'))
head(test1a)
money educ variable value
1 3.10 11 female Female
2 3.24 12 female Female
3 3.00 11 female Male
4 6.00 8 female Male
5 5.30 12 female Male
6 8.75 16 female Male
tail(test1a)
money educ variable value
3151 5.65 12 Industry construc
3152 15.00 16 Industry profserv
3153 2.27 10 Industry trade
3154 4.67 15 Industry construc
3155 11.56 16 Industry nondur
3156 3.50 14 Industry profserv
我使用的ggplot函数是:
ggplot(test1a, aes(educ,money, col = value )) + geom_point()+
facet_wrap(~ variable) + geom_smooth(method = 'lm', se = FALSE) +
theme(legend.position="none")
这给了我以下情节:
这正是我要找的,只是我希望所有 6 个图都具有相同的配色方案。换句话说,我希望所有 6 个图都具有与左上角完全相同的 green/yellow 图。
有什么建议吗?
我生成了一些数据来说明这个答案
test1a <- data.frame(money = rnorm(10), educ = rnorm(10),
variable = c("female","female","female","female","female","Industry","Industry","Industry","Industry","Industry"),
value = c("Female", "Female", "Male", "Male", "Female", "construc", "construc", "trade", "trade", "trade"))
money educ variable value
1 0.6509500 0.822198786 female Female
2 -0.7038793 0.257554982 female Female
3 -0.9110664 -1.048976078 female Male
4 0.1313963 -1.398813412 female Male
5 -0.6050824 0.818251963 female Female
6 1.2937046 -0.289675281 Industry construc
7 1.1670726 -0.004767622 Industry construc
8 0.3489473 -0.633061650 Industry trade
9 -0.1536924 -0.567433569 Industry trade
10 1.3932668 -0.010446676 Industry trade
使用的库
library(ggplot2)
library(dplyr)
首先获取 table 个正在使用的变量值
uniqueVarVal <- unique(test1a[,3:4])
variable value
1 female Female
3 female Male
6 Industry construc
8 Industry trade
目的是为女性变量获取手动色标,并为 Industry 变量使用相同方案。
要使用的颜色。我只指定了 2 个,你需要更多的颜色,因为你的一些变量有超过 2 个值。
colors <- c("red", "green")
添加要用于我们的 table 变量值的颜色
colValues <- uniqueVarVal %>%
group_by(variable) %>%
mutate(color = colors[row_number()]) %>%
ungroup()
# A tibble: 4 × 3
variable value color
<fctr> <fctr> <chr>
1 female Female red
2 female Male green
3 Industry construc red
4 Industry trade green
接下来我们需要设置值变量的水平,否则ggplot会按字母顺序排列它们。
test1a$value <- factor(test1a$value, levels = colValues$value)
最后使用重复模式红-绿指定手动色标。
ggplot(test1a, aes(educ,money, col = value )) +
geom_point(alpha = 0.3) +
geom_smooth(method = 'lm', se = FALSE) +
scale_color_manual(values = colValues$color) +
facet_wrap(~ variable)
我没有显示图例,所以你可以看到发生了什么。
考虑到点的密度,我建议使用 alpha 设置透明度。
我正在制作一个多重散点图来显示交互。我使用 reshape2 包中的 melt 函数使我的数据看起来像这样:
head(wage)
money educ exper tenure nonwhite female married numdep smsa Region Industry
1 3.10 11 2 0 White Female Notmarried 2 1 west other
2 3.24 12 22 2 White Female Married 3 1 west services
3 3.00 11 2 0 White Male Notmarried 2 0 west trade
4 6.00 8 44 28 White Male Married 0 1 west clerocc
5 5.30 12 7 2 White Male Married 1 0 west other
6 8.75 16 9 8 White Male Married 0 1 west profserv
test1 = wage %>% select(money, educ, female, nonwhite, married, smsa, Region, Industry)
test1a = melt(test1, id.vars= c('money', 'educ'))
head(test1a)
money educ variable value
1 3.10 11 female Female
2 3.24 12 female Female
3 3.00 11 female Male
4 6.00 8 female Male
5 5.30 12 female Male
6 8.75 16 female Male
tail(test1a)
money educ variable value
3151 5.65 12 Industry construc
3152 15.00 16 Industry profserv
3153 2.27 10 Industry trade
3154 4.67 15 Industry construc
3155 11.56 16 Industry nondur
3156 3.50 14 Industry profserv
我使用的ggplot函数是:
ggplot(test1a, aes(educ,money, col = value )) + geom_point()+
facet_wrap(~ variable) + geom_smooth(method = 'lm', se = FALSE) +
theme(legend.position="none")
这给了我以下情节:
这正是我要找的,只是我希望所有 6 个图都具有相同的配色方案。换句话说,我希望所有 6 个图都具有与左上角完全相同的 green/yellow 图。
有什么建议吗?
我生成了一些数据来说明这个答案
test1a <- data.frame(money = rnorm(10), educ = rnorm(10),
variable = c("female","female","female","female","female","Industry","Industry","Industry","Industry","Industry"),
value = c("Female", "Female", "Male", "Male", "Female", "construc", "construc", "trade", "trade", "trade"))
money educ variable value
1 0.6509500 0.822198786 female Female
2 -0.7038793 0.257554982 female Female
3 -0.9110664 -1.048976078 female Male
4 0.1313963 -1.398813412 female Male
5 -0.6050824 0.818251963 female Female
6 1.2937046 -0.289675281 Industry construc
7 1.1670726 -0.004767622 Industry construc
8 0.3489473 -0.633061650 Industry trade
9 -0.1536924 -0.567433569 Industry trade
10 1.3932668 -0.010446676 Industry trade
使用的库
library(ggplot2)
library(dplyr)
首先获取 table 个正在使用的变量值
uniqueVarVal <- unique(test1a[,3:4])
variable value
1 female Female
3 female Male
6 Industry construc
8 Industry trade
目的是为女性变量获取手动色标,并为 Industry 变量使用相同方案。
要使用的颜色。我只指定了 2 个,你需要更多的颜色,因为你的一些变量有超过 2 个值。
colors <- c("red", "green")
添加要用于我们的 table 变量值的颜色
colValues <- uniqueVarVal %>%
group_by(variable) %>%
mutate(color = colors[row_number()]) %>%
ungroup()
# A tibble: 4 × 3
variable value color
<fctr> <fctr> <chr>
1 female Female red
2 female Male green
3 Industry construc red
4 Industry trade green
接下来我们需要设置值变量的水平,否则ggplot会按字母顺序排列它们。
test1a$value <- factor(test1a$value, levels = colValues$value)
最后使用重复模式红-绿指定手动色标。
ggplot(test1a, aes(educ,money, col = value )) +
geom_point(alpha = 0.3) +
geom_smooth(method = 'lm', se = FALSE) +
scale_color_manual(values = colValues$color) +
facet_wrap(~ variable)
我没有显示图例,所以你可以看到发生了什么。
考虑到点的密度,我建议使用 alpha 设置透明度。