简体   繁体   English

调整特定 plot 的大小 - Rmarkdown

[英]Resize the particular plot - Rmarkdown

I have in one chunk a few plots and I want to resize them.我有几个地块,我想调整它们的大小。 I always change the size in the chunk options, like that:我总是在块选项中更改大小,如下所示:

```{r fig1, fig.height = 3, fig.width = 5}
```

But now I want to resize a particular one.但现在我想调整一个特定的大小。 I don't want to change the global options.我不想更改全局选项。 Is there any option to resize particular chart?是否有调整特定图表大小的选项? The reason is that I did a loop and in this loop is there a few charts.原因是我做了一个循环,在这个循环中有一些图表。 For example I want to resize this plot:例如我想调整这个 plot 的大小:

  print(ggplot(df_stat, aes(x = value,
                            y=nrow(df_stat))) +
          stat_halfeye()  +
          stat_slab(aes(fill = stat(level)), .width = seq(0.01, 1, by=0.01)) +
          labs(x = i,
               y = NULL) +
          geom_vline(xintercept = 4,
                     colour="red",
                     size=1)+
          theme_classic() +
          theme(axis.text.y = element_blank(),
                axis.ticks.y = element_blank(),
                axis.line.y = element_blank(),
                axis.line.x = element_blank(),
                legend.position="none"))


I think all the plots produced in a chunk will be displayed in the same size frame, but you could change the margins.我认为块中生成的所有图都将显示在相同大小的框架中,但您可以更改边距。 For example,例如,

library(ggplot2)
for (i in 1:3) {
  print(ggplot(mtcars, aes(cyl, mpg)) + geom_point() +
          theme(plot.margin = unit(c(1,1+i,1,1+i), "cm")))
}

截屏

If you're using base graphics, use par(mar =...) to change the margins between plots.如果您使用的是基本图形,请使用par(mar =...)更改绘图之间的边距。

Another way to do it is to save all the plots in a list, then display them in separate chunks, eg另一种方法是将所有绘图保存在列表中,然后将它们显示在单独的块中,例如

```{r}
library(ggplot2)
result <- list()
for (i in 1:3) {
  result[[i]] <- ggplot(mtcars, aes(cyl, mpg)) + geom_point()
}
```

```{r fig.height=2}
result[[1]]
```

```{r fig.height=3}
result[[2]]
```

```{r fig.height=4}
result[[3]]
```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM