type_density(): add echo.bw for reporting the smoothing bandwidth - #672
Conversation
6c5ab8d to
459d2f0
Compare
|
This is great, thanks. I updated/added some snapshot tests, since that's what we benchmark against in general. The only remaining issue that I could see is that the pkgload::load_all("~/Documents/Projects/tinyplot/")
#> ℹ Loading tinyplot
plt(~ Sepal.Length, data = iris, type = type_density(echo.bw = "xlab"))Created on 2026-07-31 with reprex v2.1.1 |
|
Oh, and feel free to add yourself as a contributor in the |
|
Thanks for adding the tests! I tracked down the xlab issue: the formula interface fills xlab with the variable name before type_data() runs, so the guard against overwriting user labels was mistaking the auto-derived label for a user-set one. Derived labels now carry a small tp_auto attribute on the value and the echo block checks that instead of NULL-ness. Verified that echo.bw = "xlab" now renders the bandwidth text and that an explicit user xlab is still left alone. Happy to rework the marker if you'd prefer a different mechanism. And thanks for the DESCRIPTION invitation, added myself as ctb. |
The formula interface fills xlab with the variable name before type_data() runs, so the never-overwrite guard mistook the auto-derived label for a user-set one and skipped it. Mark derived labels with a tp_auto attribute on the value and have the echo block check that instead of NULL-ness. User-supplied labels are still left alone.
6a5f8da to
7f34f73
Compare
Hmmm. I'm probably being pedantic, but is it okay if we just drop the I think supporting |
Removes the tp_auto marker and the branching it needed in sanitize_xylab() and tinyplot(), so the main code is back to what it was. sub, cap, and cat remain.
|
Not pedantic at all, that's a fair call. The main code is back to what it was: I dropped the tp_auto marker and the branching it needed in sanitize_xylab() and tinyplot(). xlab is no longer a valid destination and errors with a message listing the three that are, and the docs and NEWS entry now only mention sub, cap, and cat. |
|
Thanks! |

Closes #287.
Adds an
echo.bwargument totype_density()for reporting the smoothing bandwidth and the number of observations behind it, neither of which is visible from the curve.Destinations are
"sub","xlab","cap", and"cat"(console, with"print"as a synonym), in any combination.TRUEis shorthand for"sub"; the defaultFALSEreports nothing.Following your two calls in the issue: all bandwidths are reported, and the argument sits on
type_density()rather thantpar(). The"cap"and"cat"destinations are the ones you suggested.I took the formatting conventions from the code you linked, including the version that was removed in #284:
sprintf("%.4g"), bracketed lists collapsed to three entries plus an ellipsis, and "Joint Bandwidth" when the bandwidth is shared. Output:That same code answered a question I had been about to ask: a destination the user has already labelled is left alone, matching
if (is.null(dots[["xlab"]]))in your original. Sosub = "..."supplied by the user survivesecho.bw = "sub".Two notes on the implementation:
lapply()is split into alapply()that computes the densities and aMap()that reshapes them, so the bandwidths are still in hand when the label is built. To confirm this is a pure refactor, I rendered six configurations to SVG againstmainand diffed them: byte-identical withecho.bwoff.match_echo_bw()andformat_echo_vec()are internal helpers, so NAMESPACE is unchanged.The "Titles" section said the x-axis title omits the observation count and bandwidth, which
echo.bw = "xlab"now contradicts, so it points at the new argument.Tests: 16 non-snapshot assertions covering argument normalization, the two error paths, the display formatting, and the reported text for joint, individual, and single-group cases. I could not run the snapshot tests locally (macOS), so CI will need to confirm those.
One point where I departed from the first mock-up, and where a word from you would settle it. That mock-up calls
type_density(echo.bw = 'sub'), sojoint.bwis at its"mean"default, and the subtitle lists[0.1229, 0.2124, 0.2073]. Those are the three individual bandwidths, but underjoint.bw = "mean"none of them is the bandwidth actually used: all three curves are drawn with their weighted mean, 0.1809. Reporting the individual values there seemed likely to suggest the groups were smoothed differently when they were not, so the rule I implemented is to report what was used: one value, named as joint, whenever the bandwidth is shared, and the full list only when the groups genuinely differ.That reading also matches your answer in the issue, since "all of them" was in response to the case where bandwidths differ across groups. Both mock-ups reproduce exactly under it:
joint.bw = "none"gives[0.1229, 0.2124, 0.2073], andjoint.bw = "full"gives0.2736. If you would rather the individual bandwidths always be shown, that is a one-line change.