elisa
elisa

Reputation: 995

Scale a ggplot rendered directly in Xaringan

I made the following plot in my Rmarkdown file, and render it using 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)
```

This results in the following presentation. I just want to make the plot smaller, without the indirect step of having to save it as an image, then calling it and scaling it.

Is there a way? renderedResult

Upvotes: 1

Views: 2219

Answers (1)

Dan
Dan

Reputation: 12084

Using either out.width or out.height works, while maintaining the aspect ratio. Using them together allows you to alter the aspect ratio. Using either fig.width or fig.height, as suggested by @RichardTelford, works too, but doesn't maintain the aspect ratio. Nevertheless, you can use both together to get the correct aspect ratio.


Bottom line: if I just wanted to scale down the image, I'd use either out.width or out.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)
```

enter image description here

Upvotes: 4

Related Questions