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(orcolour) controls the outline/border color of a geometric object.fillcontrols 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:
colorsets the color of the point's outer borderfillsets the color of the inner solid part of the point
To put this in context with other geoms:
- For
geom_bar(),fillcontrols the color inside the bars, whilecolorcontrols the bar borders—this difference is immediately obvious. - For
geom_text(),colorsets the text color, andfillonly matters if you uselabel.sizeto 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

