My goal is simple: I want to create a title in ggplot2 where some words are bolded and some are not, then use OfficeR to add it to a Powerpoint object and print said object.
Every time I try to do this however, each word in the PowerPoint title is it's own text box. This would be fine except that one word is always mis-aligned. I'm running thousands of these charts regularly, so it's impractical to adjust by hand.
I've tried using ggtitle, element_markdown, atop, bquote, etc. No solution has worked. Please note I've also tried other approaches using other markdown styles.
Here's a sample of the code (apologies for the look of the graph, that isn't important here)
library(ggtext); library(ggplot2); library(officer)
p <- ggplot2::ggplot() +
geom_line(data = mtcars,
aes(x = mpg, y = cyl, color = am, group = am),size = 2 ) +
#Standard Theme Elements
theme(plot.title = element_markdown(size = 16, hjust = -0, face = 'bold', family = 'Calibri'))+
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black"),
legend.key.height = unit(0.25, "cm"),
legend.position = c(0.85,1),
legend.key = element_blank(),
plot.title.position = "plot",
plot.subtitle = element_text(size = 10.5, hjust = 0, family = 'Calibri', margin=margin(0,0,25,0)),
axis.title.x=element_blank(), axis.title.y=element_blank(),
axis.text.x = element_text(hjust=0.5),
text=element_text(size=11),
) +
#Title
labs(title =paste("<b>Bold Title</b> <i>not bold part</i>")
,subtitle = 'Some Subtitle')
p_dml <- rvg::dml(ggobj = p)
pptx_test <- officer::read_pptx()
pptx_test <- pptx_test %>%
add_slide(layout = "Title and Content", master = "Office Theme") %>%
officer::ph_with(p_dml, ph_location(left = 0, top = 1, width = 5, height = 3))
print(pptx_test,'~/Desktop/test.pptx')
Would really appreciate any insight towards a solution to this problem.
Thanks