Skip to content

ggdesplot(): leave cells with a missing value empty - #18

Merged
kwstat merged 1 commit into
kwstat:mainfrom
SchmidtPaul:gg-na-value
Aug 5, 2026
Merged

ggdesplot(): leave cells with a missing value empty#18
kwstat merged 1 commit into
kwstat:mainfrom
SchmidtPaul:gg-na-value

Conversation

@SchmidtPaul

@SchmidtPaul SchmidtPaul commented Aug 5, 2026

Copy link
Copy Markdown
Collaborator

Problem

desplot() leaves a cell whose value is missing empty, ggdesplot() fills it with the
ggplot2 default grey50. That is not a neutral colour here: the default RedGrayBlue scale
runs firebrick -> lightgray -> #375997, so grey is what the middle of the data range
looks like, and a hole in the field reads as an average value rather than as no data. With a
factor response the grey cell is a fill that the key has no entry for.

Reproducer

besag.met is the dataset the vignette uses. 36 of its 1188 plots have no yield, and the gg
path drew every one of them grey (desplot 1.11, b18cfbb):

data(besag.met, package = "agridat")
f <- ggplot2::layer_data(ggdesplot(besag.met, yield ~ col * row | county), 1)$fill

sum(is.na(besag.met$yield))    #> 36
sum(f == "grey50")             #> 36    on main
sum(f == "transparent")        #> 36    with this branch

desplot(besag.met, yield ~ col * row | county)   # the same cells are empty here

The grey is hard to tell from data in that map, because the mid-range cells are themselves
grey (#D3D1D1 and #C1C5CC here).

The vignette draws this dataset with desplot() rather than ggdesplot(), so no rendered
documentation changes.

Before / after

ggdesplot(besag.met, yield ~ col * row | county), rendered against main and against this
branch. There are six missing plots in each of the six counties, so every panel is affected.

Before (main), 36 plots filled grey50:

besag_BEFORE

After (this branch), the same 36 plots left empty:

besag_AFTER

Fix

na.value = "transparent" on both fill scales, scale_fill_gradientn() for a numeric
response and scale_fill_manual() for a factor. The gg panel background is
element_blank(), so a transparent cell comes out white, which is what the lattice path
draws. The observed cells are unchanged, so the fix touches the missing cells only and does
not shift the scale.

Regression test added to tests/testthat/test_ggdesplot_fixes.R. It covers both the numeric
and the factor branch on a small synthetic field, and it also pins that the remaining cells
still get a real colour, so an empty cell cannot come from a broken scale instead. On main
it fails (4 failures, the 29 existing expectations pass), with the fix the suite is 33
passing, 0 failures, 0 warnings. R CMD check --no-manual including the vignette rebuild is
Status: OK.

Prepared with the help of an AI coding assistant; the reproducers above were run and
verified locally.

The gg path filled a missing value with grey50, which is hard to tell from the
lightgray midpoint of the default RedGrayBlue scale, so a hole in the field
read as a mid-range value. The lattice path leaves such a cell empty; set
na.value on both fill scales to match it.
@kwstat
kwstat merged commit 71caea7 into kwstat:main Aug 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants