Skip to content

Conversation

@zeileis
Copy link
Collaborator

@zeileis zeileis commented Dec 15, 2025

Fixes #534

With this PR tinyplot allows formulas without x variable, e.g., as in ~ 1 or ~ 0 or y ~ 1 or y ~ 0 etc. All of these are translated to x = NULL in the default method call.

df <- data.frame(x0 = c(0, .1), y0 = c(.2,1), x1 = c(1, .9), y1 = c(.75, 0))
tinyplot(~ 0, xmin=x0, ymin=y0, xmax=x1, ymax=y1, data = df, type = "segments")
tinyplot-nullformula

Note that the axis labels are not ideal, though. I wasn't sure where this would best be sanitized.

@zeileis
Copy link
Collaborator Author

zeileis commented Dec 15, 2025

When checking how the type would be handled for x = NULL, I noticed that the type sanitizer currently only handles bivariate y ~ x plots:

https://github.com/grantmcdermott/tinyplot/blob/main/R/sanitize_type.R#L45-L55

I would recommend that we add support for the following:

  • x = <factor>, y = NULL: barplot
  • x = NULL, y = <factor>: barplot
  • x = NULL, y = !<factor>: histogram
  • x = !<factor>, y = NULL: scatterplot against index

Note, however, that the latter two are not consistent with the base R plot() function. But maybe that's ok, given that base R's formula and default method are not even consistent here. For plot(~ x) a stripchart() is generated (which I find far less useful than a histogram) and for plot(x) a scatterplot against the index is generated.

@grantmcdermott
Copy link
Owner

Note, however, that the latter two are not consistent with the base R plot() function. But maybe that's ok, given that base R's formula and default method are not even consistent here.

Thanks @zeileis and I agree. We should follow base plot conventions where possible, but be fine to deviate in cases like this where it makes sense. Do you want to tackle these univariate cases in this PR, or a separate one?

Everything else looks good, except the axis label as you say. I'd need to play around with the code a bit to see the best place to fix.

@zeileis
Copy link
Collaborator Author

zeileis commented Dec 20, 2025

Thanks for the feedback! I think it makes sense to include the univariate defaults in this PR because we can think about which formulas map to which x/y specifications which in turn map to plot types. I'll try to post a suggestion later today...

@grantmcdermott
Copy link
Owner

Hi @zeileis. Just a ping to see if you still want to take a look at the axis labeling issue. If not, I should have time next week.

- x = <factor>, y = NULL: barplot
- x = NULL, y = <factor>: barplot
- x = NULL, y = !<factor>: histogram
- x = !<factor>, y = NULL: scatterplot against index (previous default)

note that the cases with x = NULL necessitate switching settingsx and settings$y
axis labels still need fixing
@zeileis
Copy link
Collaborator Author

zeileis commented Feb 4, 2026

Apologies for the long delay - university politics got into the way (and then the end of the semester).

I finally had a stab at the defaults for the y ~ 1 and ~ x type formulas. It's now in:

https://github.com/grantmcdermott/tinyplot/blob/formula-extensions/R/sanitize_type.R#L45-L68

A couple of comments:

  • If x is categorical (factor or character), the both x ~ 1 and ~ x generate a barplot now.
  • If x is numeric, then x ~ 1 now generates a histogram but ~ x still generates a scatterplot against the index because the latter is what you also get in plot(x).
  • To handle these cases I also switch settings$x and settings$y which is probably not the idea in the sanitize_type() function (which should arguably just modify the type). Other solutions I could think of seemed to be even more messy, though.
  • The axis labels still need to be adjusted in these cases and I'm not sure where this would best be added.

Grant @grantmcdermott would you be able to look at the labeling issues (both from this post and the very first item in this issue)?

Examples:

tinyplot(flipper_len ~ 1, data = penguins, main = "flipper_len ~ 1")
tinyplot(~ flipper_len, data = penguins, main = "~ flipper_len ")
tinyplot(species ~ 1, data = penguins, main = "species ~ 1")
tinyplot(~ species, data = penguins, main = "~ species")
tinyplot-onesided

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.

Clean way to enter formula method for types that don't require x (rect, segments)

3 participants