wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

label.padding shorthand fails (i.e. padding requires all four arguments) #11

Closed krassowski closed 4 years ago

krassowski commented 4 years ago

Thanks again for this package! It's a huge help for me, and I would love to help making it even better.

Description: The label.padding argument of geom_richtext fails with:

Error in `[.unit`(padding, c(1, 3)): index out of bounds ('unit' subsetting)

when less than four arguments are passed.

I expected that similarly to geom_label, unit(0.5, "lines") would be a valid value.

library(ggplot2)
library(ggtext)

df <- data.frame(
  label = c("Some text **in bold.**"),
  x = c(.5),
  y = c(.5)
)

ggplot(df) +
  aes(x, y, label = label) +
  geom_richtext(
    label.padding = unit(0.5, "lines")
  )
#> Error in `[.unit`(padding, c(1, 3)): index out of bounds ('unit' subsetting)

Created on 2019-12-18 by the reprex package (v0.3.0)

Passing all the four values works:

library(ggplot2)
library(ggtext)

df <- data.frame(
  label = c("Some text **in bold.**"),
  x = c(.5),
  y = c(.5)
)

ggplot(df) +
  aes(x, y, label = label) +
  geom_richtext(
    label.padding = unit(c(0.5, 0.5, 0.5, 0.5), "lines")
  )

Created on 2019-12-18 by the reprex package (v0.3.0)

It seems to be related to the previously opened issue about the failing annotate() #8, which was worked around in c4538ece186c01e06119e025bb57b6f7d2672e6e by adding all four arguments to the default argument in draw_panel().

The user-level workaround for now would be to use unit(rep(0.5, 4), "lines") which is slightly more verbose, but still allows to quickly adjust the value for all four sides.

Solution proposition:

Would adding following lines to the body of geom_richtext() be a good solution to this issue (it works locally for me)?

if (length(label.padding) == 1) {
    label.padding = rep(label.padding, 4)
}

I would be happy to create a PR if you agree that this is thee way forward.

clauswilke commented 4 years ago

Thanks for reporting this issue. I've fixed it in the underlying gridtext package. You'll have to update that package for the fix to work.

library(ggplot2)
library(ggtext)

df <- data.frame(
  label = c("Some text **in bold.**"),
  x = c(.5),
  y = c(.5)
)

ggplot(df) +
  aes(x, y, label = label) +
  geom_richtext(
    label.padding = unit(0.5, "lines")
  )

Created on 2019-12-19 by the reprex package (v0.3.0)