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

如何在ggplot2绘图中移除图例?示例代码求修改方案

移除ggplot2绘图中的图例方法

针对你给出的瓦片图代码,有三种常用方法可以移除fill对应的图例:

方法1:在图层中直接关闭图例

给geom_tile添加show.legend = FALSE参数,仅关闭当前图层的图例,适合只需要隐藏单个图层图例的场景:

df <- expand.grid(X1 = 1:10, X2 = 1:10)
df$value <- df$X1 * df$X2

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value), show.legend = FALSE)

方法2:全局关闭所有图例

通过theme()设置legend.position = "none",会隐藏绘图中的所有图例,适合整个图不需要任何图例的情况:

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value)) +
  theme(legend.position = "none")

方法3:精准关闭指定美学的图例

使用guides()针对fill美学关闭图例,这种方式可以保留其他美学(比如color、size)的图例,只隐藏指定的:

ggplot(df, aes(X1, X2)) + 
  geom_tile(aes(fill = value)) +
  guides(fill = "none")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 09:01:00