Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# desplot 1.11 ()

* `ggdesplot()` now leaves cells with a missing value empty, as `desplot()` does. Previously they were filled with the ggplot2 default grey, which lies inside the range of the default red-gray-blue scale and so looked like a mid-range value. (P.Schmidt)

* `ggdesplot()` no longer draws a spurious `no_color` legend when `text` or `num` is used without `col`. (P.Schmidt)

* `ggdesplot()` now facets on every conditioning variable in a formula such as `yield ~ col*row | site + rep`. Previously only the first was used and the others were silently dropped, overplotting cells. (P.Schmidt)
Expand Down
8 changes: 6 additions & 2 deletions R/ggdesplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -482,17 +482,21 @@ ggdesplot <- function(data,
out <- out +
facet_wrap(panel.string, scales="free")

# Note, cells with a missing value are left empty instead of being filled
# with the ggplot2 default grey50, which is hard to tell from the lightgray
# midpoint of RedGrayBlue. The lattice version leaves such cells empty too.
if(fill.type=="num")
out <- out +
#geom_tile(aes_string(fill = fill.string)) +
geom_tile(aes(fill = .data[[fill.string]])) +
scale_fill_gradientn(colours=col.regions, guide="colorbar")
scale_fill_gradientn(colours=col.regions, guide="colorbar",
na.value="transparent")

if(fill.type=="factor")
out <- out +
#geom_tile(aes_string(fill = fill.string)) +
geom_tile(aes(fill = .data[[fill.string]])) +
scale_fill_manual(values=col.regions)
scale_fill_manual(values=col.regions, na.value="transparent")

if(has.out1)
out <- out +
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test_ggdesplot_fixes.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,23 @@ test_that("ggdesplot single conditioning variable keeps its panel labels", {
p <- suppressWarnings(ggdesplot(besag.met, yield ~ col * row | county))
expect_equal(levels(p$data$.panel), paste0("C", 1:6))
})

test_that("ggdesplot leaves a cell with a missing value empty", {
# 4x4 field with a hole at col 2 / row 2, once numeric and once a factor
hole <- data.frame(
col = rep(1:4, times = 4),
row = rep(1:4, each = 4),
yield = c(1:5, NA, 7:16),
B = factor(c(rep("b1", 5), NA, rep("b2", 10)))
)

fill_num <- ggplot2::layer_data(ggdesplot(hole, yield ~ col * row), 1)$fill
expect_equal(sum(fill_num == "transparent"), 1L)
# the guard: the other 15 cells still get a real colour from col.regions,
# so the empty cell comes from the missing value and not from a broken scale
expect_equal(length(unique(fill_num[fill_num != "transparent"])), 15L)

fill_fac <- ggplot2::layer_data(ggdesplot(hole, B ~ col * row), 1)$fill
expect_equal(sum(fill_fac == "transparent"), 1L)
expect_equal(length(unique(fill_fac[fill_fac != "transparent"])), 2L)
})
Loading