You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I racked my brain on this one for hours trying to figure out what I was doing wrong. I think I've identified either a bug or undocumented behavior of using a manual scale.
The problem seems to stem from passing a named vector to values. NA values are translated and displayed correctly on the plot, but the label in the legend is dropped.
It seems like there was some discussion of this issue when the behavior of values changed (#4471 (comment), #4619), but it seems like it was resolved (in favor of not displaying an explicit NA value in the legend regardless of whether values is named, or values + labels are passed.
If that's the case, this appears to be a regression. Otherwise, it would be awesome if the docs pointed out how to add an explicit NA level, since this is often required/requested in my space.
library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
#> #> Attaching package: 'dplyr'#> The following objects are masked from 'package:stats':#> #> filter, lag#> The following objects are masked from 'package:base':#> #> intersect, setdiff, setequal, union
library(scales)
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE# Read in NC Examplenc_shp<- system.file("shape/nc.shp", package="sf")
agr<- c(
AREA="aggregate", PERIMETER="aggregate", CNTY_="identity",
CNTY_ID="identity", NAME="identity", FIPS="identity", FIPSNO="identity",
CRESS_ID="identity", BIR74="aggregate", SID74="aggregate", NWBIR74="aggregate",
BIR79="aggregate", SID79="aggregate", NWBIR79="aggregate"
)
nc<- st_read(nc_shp, agr=agr, quiet=TRUE) |>
mutate(
value= runif(100, 0, 100),
value_bin= cut(value, 4)
)
# Assigned color brewer scalecol_scale<- levels(nc$value_bin)
col_scale<- setNames(
brewer_pal("seq")(length(col_scale)),
col_scale
)
# Randomly remove 10 valuesto_na<- sample(seq_len(nrow(nc)), 10)
nc[to_na, "value_bin"] <-NA_character_# NA displays if color is discrete
ggplot(nc) +
geom_sf(aes(fill=value_bin)) +
labs(title="North Carolina", subtitle="default scale")
# NA is dropped in manual scale# if values is a named vector
ggplot(nc) +
geom_sf(aes(fill=value_bin)) +
scale_fill_manual(values=col_scale, na.value="#CCCCCC") +
labs(title="North Carolina", subtitle="manual scale")
I racked my brain on this one for hours trying to figure out what I was doing wrong. I think I've identified either a bug or undocumented behavior of using a manual scale.
The problem seems to stem from passing a named vector to
values
. NA values are translated and displayed correctly on the plot, but the label in the legend is dropped.It seems like there was some discussion of this issue when the behavior of
values
changed (#4471 (comment), #4619), but it seems like it was resolved (in favor of not displaying an explicit NA value in the legend regardless of whethervalues
is named, orvalues
+labels
are passed.If that's the case, this appears to be a regression. Otherwise, it would be awesome if the docs pointed out how to add an explicit NA level, since this is often required/requested in my space.
Session info
The text was updated successfully, but these errors were encountered: