Skip to content

Named shapes "dot" and "blank" - #5143

Closed
teunbrand wants to merge 3 commits into
tidyverse:mainfrom
teunbrand:translate_dot_blank
Closed

teunbrand wants to merge 3 commits into
tidyverse:mainfrom
teunbrand:translate_dot_blank

Conversation

@teunbrand

Copy link
Copy Markdown
Collaborator

This PR implements the suggestion in #5088 (comment) to fix #5088.

Briefly, it adds "dot" and "blank" as valid names for shapes in geom_point() that get translated to shape = "." and shape = NA respectively. The specs vignette was also updated to reflect this.

@yutannihilation

Copy link
Copy Markdown
Member

I personally against adding dot. Since "." doesn't support size, I think this is outside of "grammar of graphics."

library(ggplot2)

d <- data.frame(x = 1:3)

ggplot(d, aes(x, size = 3^x)) +
  geom_point(aes(y = "circle"), shape = "circle") +
  geom_point(aes(y = "."), shape = ".")

Created on 2023-02-21 with reprex v2.0.2

For blank, while there's a blank linetype (0) for lty, there's no blank point type for pch. Maybe we should just recommend using NA directly?

@teunbrand

Copy link
Copy Markdown
Collaborator Author

Thanks for your thoughts!

I personally against adding dot. Since "." doesn't support size, I think this is outside of "grammar of graphics."

I agree it doesn't really play well with the grammar of graphics. However, there currently aren't any prohibitions on specifying shape = ".", so the question then becomes whether it should be recognised as a named shape or not.

For blank, while there's a blank linetype (0) for lty, there's no blank point type for pch. Maybe we should just recommend using NA directly?

The documentation of ?points mentions that NA_integer is reserved for plotting no symbol. Using NA directly drops the data in the GeomPoint$handle_na method with a warning. My thought is that if you go through the trouble of specifying shape = "blank", your intent is to hide the symbols and thus don't need the warning. I think the warning is mostly there to alert the user against unintentional dropping of data.

Aside from that, you cannot currently mix valid names with NAs (backtraces omitted):

ggplot2:::translate_shape_string(c("circle", NA_character_))
#> Error in `ggplot2:::translate_shape_string()`:
#> ! Shape aesthetic contains invalid value: NA

# This following is particularly uninformative
ggplot2:::translate_shape_string(c(NA_character_, "circle"))
#> Error in if (nchar(shape_string[1]) <= 1) {: missing value where TRUE/FALSE needed

Created on 2023-02-21 with reprex v2.0.2

@yutannihilation

Copy link
Copy Markdown
Member

A bit off-topic, but I think ggplot2 should have prohibited any use of these single characters as a shape. We can use arbitrary letter here, but I feel this isn't a "point" geom anymore. That said, even so, I guess shape = 46 should remain valid, so I have no idea what's the right API would be. I'd like to wait for other maintainers' comments.

library(ggplot2)

ggplot(mtcars, aes(wt, mpg)) +
  geom_point(shape = "龍", size = 30, alpha = 0.3)

Created on 2023-02-22 with reprex v2.0.2

The documentation of ?points mentions that NA_integer is reserved for plotting no symbol.

Yeah, that's the point, but I think

you cannot currently mix valid names with NAs

is not right. From the users' perspective, NAs can be passed and get removed before translate_shape_string(). Honestly, I don't use linetype = "blank" etc., so I might be biased.

library(ggplot2)

d <- data.frame(
  id = c("non-missing", "missing"),
  shape = c("circle", NA)
)

ggplot(d, aes(id, y = 0L, shape = shape)) +
  geom_point(size = 30) +
  scale_shape_identity()
#> Warning: Removed 1 rows containing missing values (`geom_point()`).

Created on 2023-02-22 with reprex v2.0.2

@thomasp85

Copy link
Copy Markdown
Member

I too am against adding "dot"... I think a pixel-sized spot has value but not as part of any scaling etc so I think its main use is when setting shape directly rather than mapping it - and in these instances shape = "." is just as informative IMO

@teunbrand

Copy link
Copy Markdown
Collaborator Author

Right, but all the arguments made for "dot", also apply to "blank", so I think it'd be better to close this PR then, no?

@thomasp85

Copy link
Copy Markdown
Member

yeah, I think you can close it

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.

Allow point shapes to be mixtures of names and symbols

3 participants