缩放直接在Xaringan中渲染的ggplot

时间:2019-03-22 11:29:17

标签: r ggplot2 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)
```

这将导致以下演示。我只想使图变小,而无需执行间接步骤,即不必将其保存为图像,然后调用它并对其进行缩放。

有办法吗? renderedResult

1 个答案:

答案 0 :(得分:1)

使用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)
```

enter image description here

相关问题