rggplot2quartoreveal.js

How can I get rid of the white borders appearing in R output in quarto reveal.js


When working on a quarto document in R, the ggplot using geom_sf usually produce white borders which appear in dark theme. You can get rid of those what using #| fig-width: xx #| fig-height: yy but it takes a lot of trials to find the right values. Is there a way of getting rid of the extra white space around plots ?


---
title: "test"
footer: "test "
author: "me"
format: revealjs
theme: dark
editor: visual
---


    ## wrong output
    ```{r test1}

     library(ggplot2)
     library(maps)
     library(ggdark)
      world1 <- sf::st_as_sf(map('world', plot = FALSE, fill = 
 TRUE))
     ggplot() + geom_sf(data = world1) + dark_theme_gray() 


    ```

    ## correct output

    ```{r test2}


    ggplot(mtcars, aes(x = wt, y = mpg)) +
     geom_point() + dark_theme_gray()


    ```
 

ggplot from ggmap has not the standard format and has white borders

This standard ggplot has no border


Solution

  • Following my answer to this related post dealing with xaringan, you can achieve your desired result by setting both the background fill of the plotting device and ggplot's plot.background fill and colour to transparent:

    ---
    title: "test"
    footer: "test "
    author: "me"
    format: revealjs
    theme: dark
    editor: visual
    ---
    
    ```{r}
    knitr::opts_chunk$set(dev.args = list(bg = "transparent"))
    ```
    
    ## wrong output
    ```{r test1}
    library(ggplot2)
    library(maps)
    library(ggdark)
    world1 <- sf::st_as_sf(map('world', plot = FALSE, fill = 
     TRUE))
    ggplot() + 
      geom_sf(data = world1) + 
      dark_theme_gray() +
      theme(
        plot.background = element_rect(
          fill = "transparent",
          color = "transparent"
        )
      )
    ```
    
    ## correct output
    ```{r test2}
    ggplot(mtcars, aes(x = wt, y = mpg)) +
     geom_point() + 
     dark_theme_gray()
    ```
    

    enter image description here