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

如何在geom_col_pattern双配色场景下设置主导填充色?

解决ggpattern渐变条形颜色占比问题

要让#1E466E占据条形约90%的长度,你需要使用pattern_stop参数控制渐变的起止位置,pattern_gravity仅用于图案对齐,无法调整颜色占比。

修改后的代码如下:

library(ggplot2)
library(ggpattern)
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
gg <- ggplot(df) +
  geom_col_pattern(
    aes(outcome, level),
    pattern = 'gradient',
    pattern_fill = "#1E466E",       
    pattern_fill2 = "#00A8E8",
    pattern_orientation = "horizontal",
    # 主色覆盖0-90%区域,次要色覆盖90%-100%区域
    pattern_stop = c(0.9, 1)
  ) +
  theme_bw(18) +
  theme(legend.position = 'none')
plot(gg)

参数说明

pattern_stop接受长度为2的数值向量,分别对应pattern_fill和pattern_fill2的渐变终止位置。设置c(0.9, 1)后,#1E466E会占据条形90%的长度,剩余10%由#00A8E8填充。

内容的提问来源于stack exchange,提问作者umair durrani

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 05:37:36