ggplot2中ggproto的non_missing_aes参数功能及与required_aes的区别问询
non_missing_aes in ggproto Great question—this is one of those undocumented internals in ggplot2 that trips up even seasoned extension developers. Let’s break down what non_missing_aes does, and how it differs from the more familiar required_aes.
What non_missing_aes Does
At its core, non_missing_aes is a safety check that forces specific aesthetic mappings to have no missing values (NA). If a user tries to pass data with NA in any of the aesthetics listed here, ggplot2 will immediately throw an error and prevent the plot from rendering.
This is designed for cases where your custom geom or stat can’t function correctly with incomplete data. For example, a stat that calculates pairwise distances needs every pair of coordinates to exist, or a geom that draws connected shapes can’t render a segment if one endpoint is missing.
Key Differences from required_aes
The two parameters sound similar, but they enforce entirely different rules:
Mandatory presence vs. mandatory completeness
required_aesonly ensures the user provides the aesthetic mapping—it doesn’t care if there are NAs. If a geom hasrequired_aes = c("x", "y"), the user must include both in theiraes()call, but ggplot2 will just skip rows with NA values instead of throwing an error.non_missing_aesgoes a step further: it requires the mapping to exist and have zero NA values. Even one NA in the specified aesthetic will trigger an error.
Error messages tell the story
- Miss a
required_aes? You’ll get a message like:Error: geom_mycustom requires the following missing aesthetics: x, y—a clear prompt to add the missing mappings. - Have an NA in a
non_missing_aes? The error will be:Error: Aesthetics must not contain NA, Inf, or NaN: xend—directly pointing out the data quality issue.
- Miss a
Use cases vary
required_aesis for the foundational building blocks of your geom/stat. Almost every geom needsxandy, so those go here.non_missing_aesis for critical aesthetics where missing data breaks the core functionality. Think:- A stat that computes density (needs non-missing values to calculate distributions)
- A geom that draws labeled connectors (needs complete start/end coordinates to render lines)
- Custom stats that perform mathematical operations where NA would invalidate results
Quick Example
Suppose you’re building a geom that draws fixed-length arrows between points—no missing coordinates allowed. Here’s how you’d use both parameters:
GeomFixedArrow <- ggproto( "GeomFixedArrow", Geom, # Users must provide all these aesthetics required_aes = c("x", "y", "angle", "length"), # None of these can have NA values non_missing_aes = c("x", "y", "angle", "length"), draw_panel = function(data, panel_params, coord) { # We can safely use all values here—no NAs to handle! coords <- coord$transform(data, panel_params) grid::arrowGrob( x0 = coords$x, y0 = coords$y, angle = coords$angle, length = grid::unit(coords$length, "cm"), gp = grid::gpar(col = coords$colour) ) } )
If a user passes data with an NA in angle, ggplot2 will error out before reaching your draw_panel function, saving you from having to handle invalid values manually.
内容的提问来源于stack exchange,提问作者passiflora

