r-lib / marquee

Markdown Parser and Renderer for R Graphics
https://marquee.r-lib.org
Other
85 stars 1 forks source link

Full-width by default? #35

Open z3tt opened 3 months ago

z3tt commented 3 months ago

Hey Thomas,

Thanks for another awesome package! I noticed that element_marquee() requires a width to be specified when passing images only, which means the value must be adjusted whenever the plot width changes.

library(ggplot2)
library(marquee)

logo <- system.file("help", "figures", "logo.png", package = "marquee")
md <- marquee_glue("![]({logo})")

p <- ggplot(mtcars) + 
  geom_point(aes(x = mpg, y = disp)) + 
  labs(caption = md)

## works
p + theme(plot.caption = element_marquee(width = unit(15, "cm")))

## doesn't show the image (guess because width = 0?)
p + theme(plot.caption = element_marquee())

Is there any chance to modify its behavior so that the width is automatically set according to the plot aspect ratio in case the user doesn’t specify a width?

aphalo commented 3 months ago

Have you tried using NPC units? The code below seems to work.

## works?
p + theme(plot.caption = element_marquee(width = unit(0.5, "npc")))
z3tt commented 3 months ago

Hi @aphalo, thanks for the tip! This helps quite a lot.

However it is not spanning the full plot width by setting width = unit(1, "npc") (also makes sense as it's referring to the panel width). So in case it's possible, I think it still would be nice if the image spans the full plot width by default as the width of the axis label and the legend are influencing the width one needs to specify.