tidymodels / infer

An R package for tidyverse-friendly statistical inference
https://infer.tidymodels.org
Other
721 stars 80 forks source link

visualize() doesn't pass all arguments to ggplot2 functions #504

Closed wmorgan485 closed 10 months ago

wmorgan485 commented 1 year ago

< -- ~~~~~~~~~~~~ -->

The problem

Some arguments included in visualize( ) apparently aren't used by ggplot2 functions. This is apparent in Figure 8.34 of the ModernDive textbook, as described below.

Reproducible example

# This code from 
# https://github.com/moderndive/ModernDive_book/blob/master/08-confidence-intervals.Rmd
# is to visualize a bootstrap distribution:

bootstrap_distribution <- read_rds("https://github.com/moderndive/ModernDive_book/blob/master/rds/bootstrap_distribution_balls.rds")

# Visualize bootstrap distribution of p-hat
bootstrap_distribution %>%
  visualize(binwidth = 0.05, boundary = 0.4, color = "white") +
  labs(
    x = "Proportion of 50 balls that were red",
    title = "Bootstrap distribution"
  )

# Based on Figure 8.34 of ModernDive textbook, 
# the `color` argument is passed to ggplot2 functions, 
# but not the `binwidth` and `boundary` arguments, 
# since there are ~3 bins per 0.1 units and no bin has a boundary at 0.4.

reprex::reprex(si = TRUE)
simonpcouch commented 12 months ago

Thanks for the issue!

A reprex:

library(tidyverse)
library(infer)

bootstrap_distribution <- read_rds("https://github.com/moderndive/ModernDive_book/raw/master/rds/bootstrap_distribution_balls.rds")

# Visualize bootstrap distribution of p-hat
bootstrap_distribution %>%
   visualize(binwidth = 0.05, boundary = 0.4, color = "white") +
   labs(
      x = "Proportion of 50 balls that were red",
      title = "Bootstrap distribution"
   )

Created on 2023-09-05 with reprex v2.0.2

It looks to me like every one of binwidth, boundary, and color here are either ignored or overwritten internally. We can debug on infer:::simulation_layer and infer:::theoretical_layer to see how these do or don't get passed. In this case:

https://github.com/tidymodels/infer/blob/6854b6e5f8b356d4b518c2ca198cc97e66cd4fcb/R/visualize.R#L412-L422

github-actions[bot] commented 9 months ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.