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

R语言ggplot中图表与图例元素形状不一致,如何将图例改为圆形?

让ggplot2图例显示为圆形的方法

默认情况下,当通过size映射变量时,ggplot2生成的图例符号是矩形。要将图例改为圆形,可通过以下两种方式实现:

方法一:使用guides()函数覆盖图例属性

通过guide_legend()的override.aes参数,强制将图例的形状设置为圆形:

library(ggplot2)

ggplot(mtcars) + 
  aes(x = mpg, y = disp, size = cyl) + 
  geom_point() +
  geom_smooth(level = 0.99, method = "loess") +
  guides(size = guide_legend(override.aes = list(shape = 19)))

方法二:在尺寸刻度中指定图例设置

直接在scale_size_continuous()里配置guide参数,效果与方法一一致:

library(ggplot2)

ggplot(mtcars) + 
  aes(x = mpg, y = disp, size = cyl) + 
  geom_point() +
  geom_smooth(level = 0.99, method = "loess") +
  scale_size_continuous(guide = guide_legend(override.aes = list(shape = 19)))

参数说明

  • shape = 19对应实心圆形,若需要空心圆可改用shape = 1,其他形状代码可参考ggplot2内置的形状参数规则。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 00:40:42