Skip to content

Add default aesthetic values to geom docs #5461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

steveharoz
Copy link
Contributor

@steveharoz steveharoz commented Oct 8, 2023

A common problem, especially for beginner users, is that you make a plot using default aesthetic values, but then you want to adjust them by a relative amount. For example, you make a plot with geom_point(), but you want the points to be 50% larger. Is the default size 1? 0.01 (proportion of the chart size)? 10 (pixels)? If you don't know the default value, it's tough to know even what order of magnitude to set the new value to.

So, this update adds the default value next to each aesthetic listed in the docs.

It'll impact every geom's documentation, so I wanted to discuss it here before pushing all the generated .RD files.

Stylized values

How values are displayed varies by the aesthetic and type:

  • Color and fill: The background color of the text is set to the value. The foreground text color has the extreme opposite luminance (white for dark colors and black for light colors). NA and "transparent" are handled specially.
  • Shapes show both the number and the name. If one is provided, it will look up the other. E.g., 19 or "circle"
  • Linetypes show both the number and the name. If one is provided, it will look up the other. E.g., 1 or "solid"
  • Other strings have quotes added around them.
  • Anything else is shown as a code block. E.g., ~after_stat(x)

Screenshot from RStudio:
image

image

@thomasp85
Copy link
Member

I like this! ❤️

@teunbrand
Copy link
Collaborator

I also think this is great!

Any reason not to?

Technically, one can change the defaults with update_geom_defaults() at which point the docs no longer reflect the state. However, I think the usefulness of this PR outweighs that consideration.

How should the default value to appear?

I'm a fan of the low clutter option. Maybe the line on top should just read

geom_foobar() understands the following aesthetics. Required aesthetics are displayed in bold and optional aesthetics list their default values.

A few other things to consider:

  • The group's default is -1, unless a discrete mapping prompts a grouping. I don't think this is a convenient place to display this and the group's link already explains this.
  • Should character defaults be displayed with quotes? E.g. "black" instead of black, unless NA.
  • How are aesthetics with delayed evaluation displayed? For example, stat_qq() has x = after_stat(x),

@smouksassi
Copy link

how would the user know the "units" of say size:1.5
and would it be useful to remind users that shapes can be numeric or specific strings?
shape: 19 / "circle"

@teunbrand
Copy link
Collaborator

@smoukassi Most of such details are in the exact vignette that the bottom line refers to, so I think the verbosity is good as-is.

@steveharoz
Copy link
Contributor Author

steveharoz commented Oct 9, 2023

I used @teunbrand's suggestion for the text at the top. Thanks.

I agree with @smouksassi, at least in part. Sure, people can look it up the details, but I remember really struggling with this stuff as a beginner. So now the default value shows as both a number and a string. Which ever one is given, it will look up the other. And it should gracefully handle non-typical values that it can't look up. See the new screenshot at the top.

@steveharoz steveharoz marked this pull request as ready for review October 10, 2023 11:02
ifelse(aes == "linetype", get_linetype_name(default_value),
ifelse(aes %in% c("colour", "fill", "border_colour"),
ifelse(is.na(default_value), '\\code{{NA}} (equivalent to \\code{"transparent"})', get_styled_color(default_value)),
ifelse(aes == "alpha" & is.na(default_value), '\\code{NA} (equivalent to \\code{1})',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for nitpicking this, but alpha(x, NA) is not equivalent to alpha(x, 1). Using alpha(x, NA) preserves the alpha channel, whereas alpha(x, 1) does not.

library(scales)
alpha("#FF000088", NA)
#> [1] "#FF000088"
alpha("#FF000088", 1)
#> [1] "#FF0000FF"

Created on 2023-10-27 with reprex v2.0.2

@teunbrand
Copy link
Collaborator

The spirit of the PR was included as part of #6285, so I'm closing this PR in favour of the other.

@teunbrand teunbrand closed this Mar 26, 2025
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.

4 participants