缩放直接在 Xaringan 中呈现的 ggplot

Scale a ggplot rendered directly in Xaringan

我在我的 Rmarkdown 文件中制作了以下图,并使用 Xaringan 渲染它。

---
title: "myTitle"
output:
  xaringan::moon_reader:
    css: ["default", "kunoichi", "ninjutsu", "metropolis-fonts"]
    lib_dir: libs
    chakra: libs/remark-latest.min.js
    seal: false
    nature:
    countIncrementalSlides: false
    ratio: '16:9'
---

# Two pretty gaussians  

## and a few vertical lines

```{r, echo=FALSE, message=FALSE, warning=FALSE}
library(cowplot)
ggplot(data = data.frame(x = c(-3, 3)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 1, sd = 1)) + 
  stat_function(fun = dnorm, n = 101, args = list(mean = -1, sd = 1)) + 
  geom_vline(xintercept = 0, colour="black", linetype = "solid") +
  geom_vline(xintercept = c(-1.5,1.5), colour="black", linetype = "longdash") +
  ylab("Density") + xlab("\'Internal Signal\'") +
  scale_y_continuous(breaks = NULL)
```

这导致了以下演示文稿。我只是想让情节变小,而不需要将其保存为图像,然后调用它并缩放它的间接步骤。

有办法吗?

使用 out.widthout.height 均可,同时保持纵横比。一起使用它们可以改变纵横比。按照@RichardTelford 的建议,使用 fig.widthfig.height 也可以,但不会保持纵横比。不过,您可以同时使用两者以获得正确的纵横比。


底线: 如果我只想缩小图像,我会使用 out.widthout.height

---
title: "myTitle"
output:
  xaringan::moon_reader:
    css: ["default", "kunoichi", "ninjutsu", "metropolis-fonts"]
    lib_dir: libs
    chakra: libs/remark-latest.min.js
    seal: false
    nature:
    countIncrementalSlides: false
    ratio: '16:9'
---

# Two pretty gaussians  

## and a few vertical lines

```{r, echo=FALSE, message=FALSE, warning=FALSE, out.width = '200px'}
library(cowplot)
ggplot(data = data.frame(x = c(-3, 3)), aes(x)) +
  stat_function(fun = dnorm, n = 101, args = list(mean = 1, sd = 1)) + 
  stat_function(fun = dnorm, n = 101, args = list(mean = -1, sd = 1)) + 
  geom_vline(xintercept = 0, colour="black", linetype = "solid") +
  geom_vline(xintercept = c(-1.5,1.5), colour="black", linetype = "longdash") +
  ylab("Density") + xlab("\'Internal Signal\'") +
  scale_y_continuous(breaks = NULL)
```