davidgohel / rvg

https://davidgohel.github.io/rvg/
132 stars 15 forks source link

dml() interacting with ggtext() elements #57

Open nickjparkinson opened 3 weeks ago

nickjparkinson commented 3 weeks ago

Having trouble getting the rvg package to work with html and markdown formatting options in ggtext (for example, using theme(element.subtitle = geom_richtext()or usinggeom_richtext()).

geom_richtext() produces misaligned boxes in the vector (see image), and when added to a PPT content placeholder, the subtitle just renders the raw html or markdown.

Min reprex below.

ibrary(tidyverse)
library(officer)
library(rvg)
library(ggtext)

plot <- 
  ggplot(mtcars, aes(x = wt, y = mpg, col = factor(am))) +
  geom_point() +
  # Add richtext legend
  geom_richtext(data = ~last(.), 
                aes(x = 4, 
                    y = 30, 
                    label = "<span style = 'color:#00BFC4;'>Automatic</span>
         <br><span style = 'color:#F8766D;'> Manual</span>"),
                fill = NA, label.color = NA) +
  # Add markdown subtitle
  labs(title = "Heavier cars use more gallons per mile", 
       subtitle = "Fuel efficiency in miles per gallon for <span style = 'color:#00BFC4;'>automatic</span>
         and<span style = 'color:#F8766D;'> manual</span> cars", 
         x = "Weight") +
  theme_minimal() +
  theme(plot.subtitle = element_markdown(size = 11, lineheight = 1.2),
        legend.position = 'none')

editable_graph <- dml(ggobj = plot)
doc <- read_pptx()
doc <- add_slide(doc)
doc <- ph_with(x = doc, editable_graph,
               location = ph_location_type(type = "body") )

print(doc, target = "example.pptx")

image