grattan / grattan/grattantheme

`grattan_richlegend()` errors in faceted plots when the legend data doesn't cover every panel

Open
#256 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
24
Forks
6
Avg merge
30m
Merged PRs (30d)
1

Description

grattan_richlegend() fails to render whenever it's used in a faceted plot and its layer data is present in only some panels (the normal case — you usually want the legend in a single facet). Instead of drawing the legend in the panels that have data and nothing in the others, it errors out during grob construction.

The error comes from grid:

Error in `grattan_richlegend()`:
! Problem while converting geom to grob.
ℹ Error occurred in the Nth layer.
Caused by error in `checkNA()`:
! mixture of missing and non-missing values for fontsize

As an example:

library(grattantheme)
library(ggplot2)
library(dplyr)

d <- tibble(
  facet = rep(c("a", "b"), each = 4),
  x = rep(1:4, 2),
  y = c(1, 2, 3, 4, 2, 3, 4, 5),
  g = rep(c("G1", "G2"), 4)
)

# Legend data only for facet "a"
leg <- tibble(facet = "a", g = c("G1", "G2"))

ggplot(d, aes(x, y, colour = g)) +
  geom_line(aes(group = g)) +
  grattan_richlegend(
    data = leg,
    aes(colour = g, label = g),
    legend.position = "topleft",
    inherit.aes = FALSE
  ) +
  facet_wrap(~facet) +
  scale_colour_manual(values = c(G1 = grattan_orange, G2 = grattan_red),
                      guide = "none") +
  theme_grattan()
#> Error ... checkNA(): mixture of missing and non-missing values for fontsize

Ideally grattan_richlegend should be able to write to one single facet, defaulting to the top left, but changeable by the user.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the faceted-plot example in the issue and start at the grattan_richlegend() entry point, following its grob construction for panels with and without legend data. Done means the legend renders in the panel containing data, other panels remain empty, and the target facet can be changed by the user without the fontsize error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.