yihui / xaringan

Presentation Ninja 幻灯忍者 · 写轮眼
https://slides.yihui.org/xaringan/
Other
1.49k stars 280 forks source link

Background color of area surrounding ggplot #371

Closed rpmeira closed 1 year ago

rpmeira commented 1 year ago

Hi, I'm trying to match the background color of a ggplot with the background color of the slide. But there are some areas that I don't know how to change the color of. I don't even know if that is controlled by ggplot or by xaringan, so I apologize if I'm asking it in the wrong place (and I would greatly appreciate a direction to a more appropriate place where to ask this question).

The area I'm talking about is this marked in the image below image

I've added borders to the ggplot and the output of the r code chunk so to delimitate better the area I'm talking about. To reproduce this behavior you can run the code below

---
title: "Presentation Ninja"
subtitle: "⚔<br/>with xaringan"
author: "Yihui Xie"
institute: "RStudio, PBC"
date: "2016/12/12 (updated: `r Sys.Date()`)"
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: github
      highlightLines: true
      countIncrementalSlides: false
---

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(tidyverse)
.remark-slide-content {
  background-color: #bfc7e0;
}

Get Started

mtcars %>% 
  ggplot() +
  geom_line(mapping = aes(x = mpg, y = qsec)) +
  theme_minimal() +
  theme(plot.background = element_rect(fill = "#bfc7e0", colour = "red")) +
  coord_fixed()


<!--
Please keep the below portion in your issue. Your issue will be closed if any of the boxes is not checked. In certain (rare) cases, you may be exempted if you give a brief explanation (e.g., you are only making a suggestion for improvement). Thanks!
-->

---

By filing an issue to this repo, I promise that

- [ ] I have fully read the issue guide at https://yihui.org/issue/.
- [ ] I have provided the necessary information about my issue.
    - If I'm asking a question, I have already asked it on Stack Overflow or RStudio Community, waited for at least 24 hours, and included a link to my question there.
    - If I'm filing a bug report, I have included a minimal, self-contained, and reproducible example, and have also included `xfun::session_info('xaringan')`. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version: `remotes::install_github('yihui/xaringan')`.
    - If I have posted the same issue elsewhere, I have also mentioned it in this issue.
- [ ] I have learned the Github Markdown syntax, and formatted my issue correctly.

I understand that my issue may be closed if I don't fulfill my promises.
yihui commented 1 year ago

I apologize if I'm asking it in the wrong place (and I would greatly appreciate a direction to a more appropriate place where to ask this question).

No need to apologize, but the issue guide at the bottom of your post has told you where to ask questions. Please do read the guide.

To answer your question: you don't need to match the color---the default image format is PNG, which supports transparent background, so you can set the chunk optiondev.args = list(bg = 'transparent').

yihui commented 1 year ago

For the record, this was cross-posted at https://stackoverflow.com/q/77176541/559676 and also answered there (please do mention cross-posting as instructed by the issue guide; thanks).