Skip to content
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

geom_point(): when stroke=NA, cannot change size of the point #4624

Closed
themeo opened this issue Sep 20, 2021 · 7 comments · Fixed by #4667
Closed

geom_point(): when stroke=NA, cannot change size of the point #4624

themeo opened this issue Sep 20, 2021 · 7 comments · Fixed by #4667
Labels
bug an unexpected problem or unintended behavior layers 📈

Comments

@themeo
Copy link

themeo commented Sep 20, 2021

I'm trying to generate a plot with points with no border displayed, just fill and to be able to change their size. However, once stroke=NA, I cannot change the size of the points.

library(ggplot2)

df = data.frame(x=rnorm(100), y=rnorm(100))
ggplot(df, aes(x, y)) + geom_point(shape=21, stroke=NA, fill="black", size=5)

ggplot(df, aes(x, y)) + geom_point(shape=21, stroke=NA, fill="black", size=0.5)

Created on 2021-09-20 by the reprex package (v2.0.1)

@yutannihilation
Copy link
Member

Thanks, confirmed. Here's another version of the reprex. Curious...

library(ggplot2)

df = data.frame(x = rnorm(100), y = rnorm(100))

f <- function(stroke) {
  ggplot(df, aes(x, y)) +
    geom_point(shape = 21, stroke = stroke, colour = "red", fill = "black", size = 50) +
    ggtitle(paste("stroke:", rlang::as_label(stroke)))
}

patchwork::wrap_plots(f(1), f(NA))

Created on 2021-09-20 by the reprex package (v2.0.1)

@yutannihilation yutannihilation added the bug an unexpected problem or unintended behavior label Sep 20, 2021
@clauswilke
Copy link
Member

clauswilke commented Sep 20, 2021

I think it's this line:

fontsize = coords$size * .pt + coords$stroke * .stroke / 2,

The NA poisons the calculation. I would always set stroke = 0 to get rid of the stroke, rather than stroke = NA.

library(ggplot2)

size <- 20
stroke <- 0
size * .pt + stroke * .stroke / 2
#> [1] 56.90551

stroke <- NA
size * .pt + stroke * .stroke / 2
#> [1] NA

Created on 2021-09-20 by the reprex package (v2.0.0)

@themeo
Copy link
Author

themeo commented Sep 20, 2021

The reason why I set stroke = NA was that stroke = 0 does not eliminate the border:

library(ggplot2)
df = data.frame(x=rnorm(100), y=rnorm(100))
ggplot2::ggplot(df, aes(x, y)) + geom_point(shape=21, stroke=0, fill="orange", color="blue", size=3)

Created on 2021-09-20 by the reprex package (v2.0.1)

@clauswilke
Copy link
Member

Ah, apparently graphics devices interpret 0 as "smallest possible visible line thickness". You can always set the color to transparent though, to get rid of the stroke entirely.

library(ggplot2)
df <- data.frame(
  x=rnorm(100), y=rnorm(100)
)

ggplot(df, aes(x, y)) + 
  geom_point(
    shape=21, stroke=0, fill="orange",
    color="transparent", size=3
  )

Created on 2021-09-20 by the reprex package (v2.0.0)

@yutannihilation
Copy link
Member

Thanks. So, I guess the line should be something like

stroke <- coords$stroke
stroke[is.na(stroke)] <- 0
...
  fontsize = coords$size * .pt + stroke * .stroke / 2, 

@clauswilke
Copy link
Member

Not sure entirely. That would still leave the thin outline, but getting rid of it is kind of tricky (we can't just set color = "transparent", as that will make points disappear for most point shapes). So maybe this is the way to go.

@thomasp85
Copy link
Member

We just need to ignore stroke in the size calculations if it is NA

stroke_size <- coords$stroke
stroke_size[is.na(stroke_size)] <- 0
...
  fontsize = coords$size * .pt + stroke_size * .stroke / 2, 
  lwd = coords$stroke * .stroke / 2

Or am I missing something?

(and an aside, yes - the specs for graphics devices is that a line width of 0 should equate to the thinnest possible line, for whatever reason someone thought that was a good idea)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior layers 📈
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants