Skip to content

Keep zero estimates in impact plots - #19

Open
david-mears-2 wants to merge 1 commit into
mainfrom
fix/retain-zero-impact-points
Open

david-mears-2 wants to merge 1 commit into
mainfrom
fix/retain-zero-impact-points

Conversation

@david-mears-2

@david-mears-2 david-mears-2 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

The PR is for a bug Copilot identified, and the fix is also due to Copilot.

In brief, the issue is that a plotting function was dropping values equal to zero.

I don't know if there was any reason that the logic was the way it was - it quite explicitly filters out zero-values.

I reproduced using this command:

Rscript --vanilla - <<'RS'
library(dplyr)
library(ggplot2)
source('R/fn_plotting_helpers.R')
source('R/fn_plotting_impact.R')

input <- data.frame(
  country = 'Example country',
  year = 2018:2026,
  burden_outcome = 'cases',
  impact = c(0, 142.8, 0, 0, 165.4, 0, 0, 128.6, 0),
  short_name = 'Campaign'
)
p <- plot_impact(input, 'Example country', 'cases', 'year',
                 'Synthetic campaign impact by vaccination year') +
  scale_x_continuous(breaks = 2018:2026) +
  scale_y_continuous(limits = c(0, 200))

ggsave('zero-impact.png', p, width = 8, height = 5, dpi = 150)
print(p$data[c('year', 'impact')])
RS

Before

zero-impact

After

zero-impact

Copilot's PR description

The bug

plot_impact() shows the estimated health benefit of vaccination over time, such as the number of cases averted each year. It also supports grouping estimates by birth cohort.

The function removes estimates equal to zero before drawing the plot. For example, estimates of 10 cases averted in 2020, 0 in 2021, and 10 in 2022 become just the two points for 2020 and 2022. The line joins those points at 10, hiding the year with no estimated benefit. The same problem affects the birth-cohort view.

The fix

Keep zero estimates in the plotting data so that the line drops to zero and rises again in this example. Continue removing missing (NA) estimates.

Preserve the existing error when the filtered data has no nonzero estimates, including all-zero inputs. That check now happens separately from filtering the plotting data. Negative estimates remain supported.

This PR addresses only zero removal; it does not change country selection or other plotting behavior. It also updates the patch version to 0.0.7 and records the fix in NEWS.md, as required by CONTRIBUTING.md.

Tests

The regression checks the actual coordinates of both the line and point layers, not just whether a plot is returned. It covers year and birth-cohort views, a 10, 0, 10 trajectory, a negative-to-positive trajectory through zero, and missing estimates. A second test checks that all-zero or absent outcomes still raise an error even when another outcome has nonzero estimates.

  • Confirmed that the trajectory regression fails before the fix: the middle point and its x-coordinate are missing.
  • Full package suite passes after the fix: 200 assertions, no failures, warnings, or skips, including the unchanged visual snapshots (R 4.3.3, dplyr 1.2.1, ggplot2 4.0.3).
  • Both new tests also pass with dplyr 1.1.4 and ggplot2 3.4.4. That environment has two existing visual-snapshot differences, so this older-version result is for the new tests, not a passing full suite.
  • R CMD INSTALL succeeds, and both new tests pass against the installed package.
  • The test file passes the configured linters. The source file has three namespace warnings also present on unchanged main; this patch adds no lint findings.

Retain zero-valued observations in mixed impact series while preserving the existing no-nonzero-estimates error. Add coordinate regressions for both views and update the patch release notes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@david-mears-2

david-mears-2 commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

This is AI generated. TODO:

  • Visual verification

Comment thread R/fn_plotting_impact.R
)

if (nrow(impact) > 0) {
if (any(impact$impact != 0)) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change @copilot ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous filter removed every zero-valued observation before plotting, so a trajectory such as 10, 0, 10 was drawn as a flat line from the first point to the last and hid the zero year. The !is.na(.data$impact) filter keeps zeros for the plot while still excluding missing estimates. The separate any(impact$impact != 0) check preserves the previous error for all-zero or otherwise non-estimable outcomes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous logic was that plotting is skipped when there are no rows (nrow as zero), but now you're checking whether any value is zero and preventing plots that have only zeros. I am not sure that's desired. Maybe you meant to skip plotting if any value is na? @copilot

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch has not been deployed

No deployments
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.

3 participants