You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言geom_point添加颜色调色板后数据丢失问题求助

问题原因分析

问题出在scale_color_brewer(palette = 'Greens')的使用逻辑上:

  • Greens是RColorBrewer提供的序列型调色板,它最多仅支持9种离散颜色值。
  • 你的数据集包含30个不同的Town,当把color映射到这个有30个水平的离散变量时,调色板无法为所有Town分配有效颜色,导致21个Town无法获取对应颜色值,被判定为缺失值,最终被geom_point过滤移除。
解决思路

提供两种可行方案,按需选择:

方案1:使用支持大量离散值的调色板

如果一定要用颜色区分不同Town,可以换用支持任意数量离散值的调色板,比如viridis系列的离散版:

sales %>% group_by(Town) %>%
    summarise(ratio_mean = mean(Sales.Ratio)) %>%
    arrange(-ratio_mean) %>%
    subset(ratio_mean < 3 & ratio_mean >0.1) %>%
    head(30) %>%
    ggplot(aes(x = ratio_mean, y = reorder(Town, ratio_mean), color=Town)) +
    geom_point(size=4) +
    theme_bw() +
    theme(panel.grid.major.x=element_blank(),
          panel.grid.minor.x=element_blank(),
          panel.grid.major.y=element_line(linetype="dashed",color="gray"),
          legend.position = "none")+
    labs(x="Sales Ratio",y="Town", 
         title="Top 30 Towns with the highest Sales Ratio",
         subtitle = "Units that were sold over the listed price") +
    scale_color_viridis_d(option = "G") # viridis绿色系离散调色板,支持多离散值

也可以用colorRampPalette生成足够多的绿色渐变颜色:

# 生成30种绿色系渐变颜色
green_palette <- colorRampPalette(RColorBrewer::brewer.pal(9, "Greens"))(30)

sales %>% group_by(Town) %>%
    summarise(ratio_mean = mean(Sales.Ratio)) %>%
    arrange(-ratio_mean) %>%
    subset(ratio_mean < 3 & ratio_mean >0.1) %>%
    head(30) %>%
    ggplot(aes(x = ratio_mean, y = reorder(Town, ratio_mean), color=Town)) +
    geom_point(size=4) +
    theme_bw() +
    theme(panel.grid.major.x=element_blank(),
          panel.grid.minor.x=element_blank(),
          panel.grid.major.y=element_line(linetype="dashed",color="gray"),
          legend.position = "none")+
    labs(x="Sales Ratio",y="Town", 
         title="Top 30 Towns with the highest Sales Ratio",
         subtitle = "Units that were sold over the listed price") +
    scale_color_manual(values = green_palette)

方案2:改用连续颜色映射(更贴合图表需求)

你的图表核心是展示Sales Ratio的大小,把颜色映射到ratio_mean(连续变量)比映射到Town更有业务意义,还能从根源避免离散颜色数量不足的问题:

sales %>% group_by(Town) %>%
    summarise(ratio_mean = mean(Sales.Ratio)) %>%
    arrange(-ratio_mean) %>%
    subset(ratio_mean < 3 & ratio_mean >0.1) %>%
    head(30) %>%
    ggplot(aes(x = ratio_mean, y = reorder(Town, ratio_mean), color=ratio_mean)) +
    geom_point(size=4) +
    theme_bw() +
    theme(panel.grid.major.x=element_blank(),
          panel.grid.minor.x=element_blank(),
          panel.grid.major.y=element_line(linetype="dashed",color="gray"),
          legend.position = "right")+
    labs(x="Sales Ratio",y="Town", 
         title="Top 30 Towns with the highest Sales Ratio",
         subtitle = "Units that were sold over the listed price",
         color="Sales Ratio") +
    scale_color_distiller(palette = "Greens", direction = 1) # 用Greens序列型连续调色板

内容的提问来源于stack exchange,提问作者Humberto Hernandez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 23:25:10