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

在R的ggplot中隐藏等高线图图例的方法(原误称色条)

移除ggplot密度等高线图的图例(色条)

有两种简单方法可以隐藏你生成的等高线图中的归一化水平图例:

方法1:精准关闭fill映射对应的图例

使用guides()函数直接指定关闭fill图例,不会影响其他可能存在的图例:

# 生成数据(保留原数据)
xnorm <- c(100.63747,  99.61373, 102.78140,  97.55596,  99.73000,  97.48554, 101.09937, 102.27442,  98.44345,  98.63937, 95.18071, 100.99034, 100.25796,  99.85286, 101.37154, 103.62904,  98.52518,  99.29083, 98.43506, 100.04028, 100.50882, 101.83978,  97.61325,  99.25491,  97.67031, 100.76768, 101.15345,  99.45239,  99.00706,  99.25894, 101.67204,  98.53558, 103.28598,  99.37674,  98.29560, 102.57874, 101.78405,  96.64773,  98.57429, 102.58449, 102.34099, 101.76433,  99.42075,  96.68118,  98.21205,  98.60703, 103.40764, 100.38115, 101.02439,  96.77698,  96.95160,  98.77218, 100.74975,  96.60388, 100.82192, 100.31073,  98.66408,  96.43205, 101.66969,  99.22321, 100.76002,  96.41930,  99.01634,  96.66087, 100.78227,  95.19014,  98.61710,  98.12931, 101.02011,  99.38666, 104.99480, 100.88542, 100.12007,  97.54670,  98.56362,  98.38221,  98.01062,  99.57980,  99.79977, 101.61598,  99.70361,  98.49786,  97.03384,  98.88931,  96.49175, 101.65014,  98.50976, 102.70204, 103.10644, 100.1625, 101.50492 , 96.78505,  97.64607,  96.23355, 100.83890, 103.89213,  96.55422,  98.14217,  97.33865, 103.09313)  
ynorm <- c(101.00065,  99.01183, 101.03233,  99.87815, 101.42570,  98.62362,  98.75669,  99.11513,  98.08216,  99.93417,  99.55935,  98.59495, 100.56597,  99.82176, 100.28809, 101.41834, 101.15464, 100.27552, 98.47943,  99.26038, 100.03685, 101.29748, 100.78959, 98.89097, 101.30694, 100.49280,  98.63089, 100.79754, 100.14726, 101.13723,  99.52545,  98.15186,  98.04923, 100.32256,  99.40237,  99.29262, 100.35762,  98.66520, 100.22026,  99.13309,  98.59964, 101.88812,  99.92022, 100.63701, 100.05824, 100.99503,  98.82681,  99.17443, 100.80232,  99.28913, 100.57291, 100.37855,  99.84050,  98.30520, 99.69184,  98.44987,  99.44457,  99.44025,  98.90528,  98.95814,  99.95515,  99.79427,  99.53203,  99.08064, 100.30757,  98.70576,  96.13955, 100.01945, 100.54303, 101.35302,  99.91294,  98.85087, 101.03512, 100.74202,  99.40307, 100.08632, 100.26612, 101.96658,  98.12320,  99.77385,  99.97723, 100.41529,  99.99487,  99.56625,  97.87832, 101.47635, 101.01801,  99.67586,  99.17761, 100.50633, 99.61379, 101.79323, 100.37171, 100.58981,  99.44867, 100.62373,  99.69169, 100.65622,  99.91878,  99.65549)  

# 修改后的绘图代码,添加guides(fill = "none")
cplot <- ggplot(mapping = aes(x = xnorm, y = ynorm)) + 
  geom_density_2d_filled(aes(fill = after_stat(level)), breaks = seq(0.1, 1, length.out = 10), contour_var = 'ndensity') + 
  geom_density_2d(contour_var = 'ndensity') +
  guides(fill = "none")
print(cplot)

方法2:隐藏所有图例

如果你的图中只有这一个图例,也可以直接用theme()函数隐藏所有图例:

cplot <- ggplot(mapping = aes(x = xnorm, y = ynorm)) + 
  geom_density_2d_filled(aes(fill = after_stat(level)), breaks = seq(0.1, 1, length.out = 10), contour_var = 'ndensity') + 
  geom_density_2d(contour_var = 'ndensity') +
  theme(legend.position = "none")
print(cplot)

说明

图例是由geom_density_2d_filled中的fill = after_stat(level)映射生成的,上述两种方法都能有效移除它。其中guides(fill = "none")更精准,仅关闭fill对应的图例;theme(legend.position = "none")则会隐藏所有图例,适合单图例场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 01:12:35