Xaringan slides made with Xaringanthemer does not show the plot properly

101 views Asked by At

As I prepare my presentation, I'm using data wrangling to create visualizations and exploring packages like Xaringan and Xaringanthemer to take advantage of their data-driven presentation capabilities. However, I've run into an issue with one of my slides, specifically in creating a donut plot where the background doesn't display as expected. I cannot share the original data due to privacy concerns, and I'm uncertain whether the issue is related to Xaringan or ggplot2, which I used to make the plot. Nevertheless, I've included a similar code below that produces a comparable output, along with the YAML and initial code for customizing the theme:

The YAML and the initial code for customizing the theme;

---
title: "Title"
subtitle: "Subtitle"  
author: 
  - "Author 1"
  - "Author 2"
date: '`r Sys.Date()`'
output:
  xaringan::moon_reader:
    includes:
      after_body: insert-logo.html
    css: xaringan-themer.css
    nature:
      slideNumberFormat: "%current%"
      highlightStyle: github
      highlightLines: true
      ratio: 16:9
      countIncrementalSlides: true
knit: pagedown::chrome_print 
---

{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(
  fig.width=9, fig.height=3.5, fig.retina=3,
  out.width = "100%",
  cache = FALSE,
  echo = TRUE,
  message = FALSE, 
  warning = FALSE,
  hiline = TRUE
)


{r xaringan-themer, include=FALSE, warning=FALSE}
library(xaringanthemer)
library(htmltools)
style_mono_light(base_color = "#ff961c",
                 header_font_google = google_font("Montserrat"),
                 text_font_google   = google_font("Lora", "400", "400i"),
                 code_font_google   = google_font("Fira Mono"),
                 title_slide_background_color = "#FFF4E8",
                 title_slide_text_color = "#ff961c")

The slides;

<div class="logo1"></div>
<div class="logo2"></div>

<style>
  body {
    line-height: 1.5; /* Increase line spacing by setting line-height to 1.5 */
  }
</style>

<div class="center">
  <br>
  <br>
  <br>
  <br>
  <br>
  <h1>AAA</h1>
</div>
---
## History 1

* BBB

* AAAA

---

{r plot1, echo=FALSE, fig.height= 6}
library(tidyverse)
library(readxl)
library(xaringanthemer)
library(ggtext)
library(showtext)
library(showtextdb)

# Create test data.
data <- data.frame(
  category=c("A", "B", "C"),
  count=c(10, 60, 30)
)
 
# Compute percentages
data$fraction <- data$count / sum(data$count)

# Compute the cumulative percentages (top of each rectangle)
data$ymax <- cumsum(data$fraction)

# Compute the bottom of each rectangle
data$ymin <- c(0, head(data$ymax, n=-1))

# Compute label position
data$labelPosition <- (data$ymax + data$ymin) / 2

# Compute a good label
data$label <- paste0(data$category, "\n value: ", data$count)

# Make the plot
ggplot(data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category)) +
  geom_rect() +
  geom_text( x=2, aes(y=labelPosition, label=label, color=category), size=6) + # x here controls label position (inner / outer)
  scale_fill_brewer(palette=3) +
  scale_color_brewer(palette=3) +
  coord_polar(theta="y") +
  xlim(c(-1, 4)) +
  theme_void() +
  theme(legend.position = "none",
        plot.background = element_rect(fill = "#FFF4E8"))

And the output is, particular for the slide 4 which my question is concerned,

enter image description here

So, from the plot, it's apparent that there are white spaces around the donut chart that disrupt the plot background. I'm unsure why these spaces are present, as they don't appear on other slides. I suspect the issue may be related to using ggplot2 to create the plot, but I haven't been able to find a solution thus far. Any suggestions you have would be greatly appreciated. Thank you in advance.

0

There are 0 answers