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

如何在ggplot2环形图(Donut Chart)中自定义颜色为#90EE90

修改ggplot2环形图填充颜色为#90EE90的方案

要把环形图的默认蓝色填充改成指定的十六进制色值#90EE90,核心是调整fill参数的设置,根据你的代码结构,有两种常用实现方法:

方法1:直接在几何图层指定统一填充色

如果你的环形图不需要按类别区分颜色(或仅包含单个类别),直接在geom_bar()(或geom_col())中添加fill = "#90EE90"参数,即可覆盖默认蓝色:

library(ggplot2)

# 替换为你的实际数据
df <- data.frame(group = "Sample", value = 80)

ggplot(df, aes(x = 2, y = value)) +
  geom_bar(stat = "identity", color = "white", fill = "#90EE90") # 设置目标填充色
  coord_polar(theta = "y", start = 0) +
  xlim(c(0.5, 2.5)) +
  theme_void()

方法2:通过颜色标度统一设置多类别颜色

如果你的代码中用了aes(fill = 类别列)来做颜色映射,想要让所有类别都使用#90EE90,用scale_fill_manual()统一赋值即可:

library(ggplot2)

# 替换为你的实际多类别数据
df <- data.frame(category = c("X", "Y", "Z"), value = c(40, 35, 25))

ggplot(df, aes(x = 2, y = value, fill = category)) +
  geom_bar(stat = "identity", color = "white") +
  coord_polar(theta = "y", start = 0) +
  xlim(c(0.5, 2.5)) +
  scale_fill_manual(values = rep("#90EE90", nrow(df))) # 为每个类别重复目标色值
  theme_void()

关键提示

  • 若无需按类别区分颜色,建议移除aes()中的fill映射,直接在geom_bar()里设置fill,代码更简洁高效。
  • 注意十六进制色值#90EE90的拼写完整性,大小写不影响识别,但需保证字符无遗漏。

内容的提问来源于stack exchange,提问作者Tran-Huynh Trung Nhu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.12 17:04:58