Keywords: ggplot2 | geom_point | scale_colour_manual | scale_fill_manual | R_visualization
Abstract: This article delves into how to independently control fill and border colors in scatter plots (geom_point) using the scale_colour_manual and scale_fill_manual functions in R's ggplot2 package. It first analyzes common issues users face, such as why scale_fill_manual may fail in certain scenarios, then systematically explains the critical role of shape codes (21-25) in managing color attributes. By comparing different code implementations, the article details how to correctly set aes mappings and fixed parameters, and how to avoid common errors like "Incompatible lengths for set aesthetics." Finally, it provides complete code examples and best practice recommendations to help readers master advanced color control techniques in ggplot2.
Problem Background and Common Misconceptions
When using ggplot2 for data visualization, scatter plots (geom_point) are among the most commonly used graphics. Users often need to customize point colors, especially when distinguishing between fill and border colors. However, many beginners encounter issues with scale_colour_manual and scale_fill_manual, such as scale_fill_manual appearing "ineffective." This typically stems from a misunderstanding of the relationship between shape attributes and color properties in ggplot2.
Relationship Between Shape Codes and Color Attributes
In ggplot2, point shapes are controlled by the shape parameter, with values ranging from 0 to 25. Among these, shapes 21 to 25 have unique properties: they support independent settings for colour (border color) and fill (fill color). In contrast, shapes 0 to 20 use only the colour parameter to control overall color, ignoring fill settings. This explains why in the user's original code, even with scale_fill_manual added, no fill color changes were visible—the default shape likely fell outside the 21-25 range.
Correct Implementation of Fill and Border Color Control
To control both fill and border colors simultaneously, shapes 21-25 must be used, and fill and colour must be correctly specified in the aes mapping. Here is an improved example based on the best answer:
library(ggplot2)
# Assume df is a data frame with columns own, method, and label
ggplot(df, aes(x = own, y = method)) +
geom_point(aes(fill = factor(label), colour = factor(label)),
shape = 21, size = 4) +
scale_fill_manual(values = c("A" = "blue", "B" = "cyan4")) +
scale_colour_manual(values = c("A" = "white", "B" = "black"))In this code, shape = 21 ensures the shape supports fill and border colors. Through aes mapping, fill and colour are bound to factor(label), allowing different labels (A and B) to have distinct fill and border colors. scale_fill_manual and scale_colour_manual are used to specify the exact color values.
Avoiding Common Errors
The error reported by the user, "Error: Incompatible lengths for set aesthetics: shape, size, fill," often arises from incorrectly mixing mappings (aes) and fixed parameters in geom_point. For example, the following code will cause an error:
# Incorrect example
ggplot(df, aes(own, method)) +
geom_point(aes(colour = factor(label)), fill = c("blue", "red"), shape = 21)Here, fill = c("blue", "red") is mistakenly placed outside aes as a fixed parameter, but ggplot2 expects fill to be either inside aes as a mapping or as a single fixed value. The correct approach is to include fill in aes or use scale_fill_manual for control.
Advanced Applications and Extensions
Beyond basic control, ggplot2 allows for more complex color settings. For instance, legends can be customized using the guides function, or overall appearance adjusted with theme. The following code demonstrates how to add legend titles and adjust point sizes:
ggplot(df, aes(own, method)) +
geom_point(aes(fill = factor(label), colour = factor(label)),
shape = 21, size = 3, alpha = 0.8) +
scale_fill_manual(name = "Label", values = c("A" = "blue", "B" = "cyan4")) +
scale_colour_manual(name = "Label", values = c("A" = "white", "B" = "black")) +
theme_minimal()Here, the alpha parameter adds transparency to points, the name parameter sets legend titles, and theme_minimal() applies a clean theme.
Summary and Best Practices
Mastering color control in ggplot2 hinges on understanding the interaction between shapes and colors. Key best practices include: 1. Use shapes 21-25 for independent control of fill and border colors; 2. Correctly map fill and colour to data variables within aes; 3. Use scale_fill_manual and scale_colour_manual to specify color values; 4. Avoid incorrectly setting vectorized parameters outside aes. By adhering to these principles, users can create both aesthetically pleasing and informative scatter plots.