wilkelab / ggtext

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

element_markdown() makes my title jump. #44

Closed bersbersbers closed 3 years ago

bersbersbers commented 4 years ago

Do I need to say more? ;)

animation_test

library(ggplot2)
library(ggtext)
library(gganimate)
library(magick)
plot <- ggplot(data.frame(step = 1:3), aes(x = step, y = step)) +
    ggtitle("({frame}/9) Hi there. I should not be jumping like this!") +
    theme(plot.title = element_markdown()) +
    transition_manual(step)
anim <- animate(plot, renderer = function(frames, fps) {
    pngs <- image_read(frames)
    gifs <- image_animate(pngs, dispose = "previous")
    image_write(gifs, "animation_test.gif")
})

animation_test

library(ggplot2)
library(ggtext)
library(gganimate)
library(magick)
plot <- ggplot(data.frame(step = 1:3), aes(x = step, y = step)) +
    ggtitle("({frame}/9) Hi there. Without element_markdown, I stand still!") +
    # theme(plot.title = element_markdown()) +
    transition_manual(step)
anim <- animate(plot, renderer = function(frames, fps) {
    pngs <- image_read(frames)
    gifs <- image_animate(pngs, dispose = "previous")
    image_write(gifs, "animation_test.gif")
})
clauswilke commented 4 years ago

This is probably due to rounding errors because ggtext breaks down the text into individual pieces and calculates where they should be placed. Try to render this at a higher resolution and see if it gets better.

If it is due to rounding errors, there isn't much I can do at this time.

bersbersbers commented 4 years ago

This is probably due to rounding errors because ggtext breaks down the test into individual pieces and calculates where they should be placed.

Thanks - this explains why the second part jumps around while the first one remains pixel-perfect. I'm already at a quite high resolution - what I did was to lower the resolution, which fixed the issue for now.

bersbersbers commented 4 years ago

@clauswilke

If it is due to rounding errors, there isn't much I can do at this time.

Could you point me to the place in the code where this rounding occurs? I imagine that these individual pieces are placed based on where each previous piece (generally, the one left of it) is placed. If that is the case, I'd think that one should be able to compute the rounding error of each piece and propagate it to the next element to maybe alleviate this issue.

I could imagine that it's here somewhere:

https://github.com/wilkelab/gridtext/blob/bf752d0b7b2fd944bf30e961108a5fd69cadad17/src/vbox.h#L70-L85

or here:

https://github.com/wilkelab/gridtext/blob/bf752d0b7b2fd944bf30e961108a5fd69cadad17/src/vbox.h#L102-L108

clauswilke commented 3 years ago

This should be fixed once we switch over to the sinab rendering engine, because it doesn't break strings into pieces unless needed for formatting changes. See #53.