编织图的动态高度和宽度 [英] Dynamic height and width for knitr plots

查看:81
本文介绍了编织图的动态高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在knitr中,可以通过在块选项中简单地指定图的大小来指定它的大小.

In knitr, one can specify the size of plot by simply specifying it in the chunk options.

例如:

```{r, fig.width=9,fig.height=3}
plot(x)
```

我希望能够根据x中的变量动态调整图形的高度和宽度.假设x是data.frame:

I would like to be able to dynamically adjust the figure height and width on the basis of variables in x. Let's say that x is a data.frame:

x <- data.frame(x=factor(letters[1:3]),y=rnorm(3))

例如,假设我想将fig.height调整为等于length(unique(x$x))

For example's sake lets say I would like to adjust fig.height to be equal to length(unique(x$x))

推荐答案

例如,您可以定义另一个块的宽度,然后使用它

You can for example define the width in another chunk, then use it

```{r,echo=FALSE}
x <- data.frame(x=factor(letters[1:3]),y=rnorm(3))
len = length(unique(x$x))
```


```{r fig.width=len, fig.height=6}

plot(cars)
```

这篇关于编织图的动态高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆