patchwork icon indicating copy to clipboard operation
patchwork copied to clipboard

Issues using {ggtext}, element_markdown(), element_textbox()

Open MokeEire opened this issue 4 years ago • 1 comments

I have been having trouble using ggtext functions inside the theme argument to plot_annotation(). In particular, the error only occurs when also using plot_layout(). I am trying to write a multi-line subtitle, using either element_textbox_simple(), element_markdown, str_wrap(), or a combination of the three.

The code looks as follows:

p1+
  p2+
  p3+
  # Tried with layout set using design and ncol
  # plot_layout(design = layout)+
  plot_layout(ncol=1)+
  plot_annotation(title = "Some Title",
                  subtitle = "<span>A long subtitle which needs to be on multiple lines, and _ideally_ use markdown syntax too.</span>",
                  caption = "**Source(s)**: Simply using markdown syntax",
                  theme = theme(plot.caption = ggtext::element_markdown(hjust = 0),
                                plot.background = element_rect(fill = my_col_pal[1]),
                                plot.title = element_text(face = "bold",
                                                          lineheight = 1.2),
                                plot.subtitle = ggtext::element_textbox_simple(lineheight = 1.1)))

And the error returned is:

Error in grid.Call(C_convert, x, as.integer(whatfrom), as.integer(whatto), : Viewport has zero dimension(s)

If I remove plot_layout, there is no issue (except that the plot is not laid out how I would like). I can't imagine this is all the information you need to diagnose this issue, please let me know what other info would be needed to figure this out.

MokeEire avatar Aug 24 '20 21:08 MokeEire

Does the error disappear if you make the plot larger? it seems that maybe there's not enough space for gridtext to do its thing?

thomasp85 avatar Nov 06 '20 10:11 thomasp85