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

ggplot2中color与fill参数的区别及代码效果疑问

color vs fill in ggplot2: Why You Didn't See a Difference with geom_point

Great question! Let's break down the core distinction between these two aesthetic parameters, and why your geom_point test didn't show a visible change.

First, the general rule across most ggplot2 geoms:

  • color (or colour) controls the outline/border color of a geometric object.
  • fill controls the inner fill color of the object.

The reason you saw no difference in your code is because the default point shape (shape = 19) is a solid circle—this shape doesn't separate the outline and fill into distinct visual properties. For shape 19, setting color or fill will affect the same single color attribute of the point.

To see the actual difference, you need to use a point shape that supports separate fill and outline colors. Shapes 21-25 are perfect for this (they have a distinct border and fill area). Try this code:

ggmap(location) + 
  geom_density_2d(aes(long, lat), df) + 
  geom_point(
    aes(long, lat, color = special, fill = special),
    shape = 21,  # This shape has separate border and fill
    size = 3,     # Make the points bigger to see the difference clearly
    alpha = 0.5,
    data = df
  )

Now you'll notice:

  • color sets the color of the point's outer border
  • fill sets the color of the inner solid part of the point

To put this in context with other geoms:

  • For geom_bar(), fill controls the color inside the bars, while color controls the bar borders—this difference is immediately obvious.
  • For geom_text(), color sets the text color, and fill only matters if you use label.size to add a background outline to the text.

So the key takeaway is: whether color and fill look different depends entirely on the geom (and in this case, the shape of the point) you're using.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:11:52