如何调整ggplot中geom_point按Full Bath变量着色的清晰度?
解决ggplot中
Full.Bath变量着色区分度不足的问题 你现在按Full.Bath变量给散点着色时颜色色调太相近,没法清晰区分,主要是因为默认的色阶/色板对比度不够。下面分两种情况给你调整方案:
一、如果Full.Bath是分类变量(比如1卫、2卫这类离散类别)
直接换用区分度更高的离散色板:
- 用ggplot内置的色盲友好型色板:
data %>% ggplot(aes(Sale.Price, Total.Bsmt.SF)) + geom_point(aes(colour = Full.Bath)) + geom_smooth() + scale_color_viridis_d(option = "plasma")
- 或者用
RColorBrewer包的高对比度色板:
library(RColorBrewer) data %>% ggplot(aes(Sale.Price, Total.Bsmt.SF)) + geom_point(aes(colour = Full.Bath)) + geom_smooth() + scale_color_brewer(palette = "Set1")
二、如果Full.Bath是连续数值型变量
调整连续色阶的对比度,让颜色差异更明显:
- 用色盲友好的连续色板:
data %>% ggplot(aes(Sale.Price, Total.Bsmt.SF)) + geom_point(aes(colour = Full.Bath)) + geom_smooth() + scale_color_viridis_c(option = "inferno")
- 也可以自定义渐变颜色:
data %>% ggplot(aes(Sale.Price, Total.Bsmt.SF)) + geom_point(aes(colour = Full.Bath)) + geom_smooth() + scale_color_gradient(low = "#1a9850", high = "#d73027")
额外小技巧
如果Full.Bath是整数但实际代表不同类别,先把它转成因子类型,再用离散色板,每个类别会有独立的专属颜色,区分度会更高:
data$Full.Bath <- as.factor(data$Full.Bath)
内容的提问来源于stack exchange,提问作者SeanMc152
相关产品推荐
相关产品推荐

