如何在ggplot2/Rmarkdown中跨标签? [英] How to facet across tabs in ggplot2 / Rmarkdown?
问题描述
我知道 facet_wrap
和 facet_grid
可以进行构面.有关需要更多空间的更详细的图形,可以轻松地在
以下是制作这些自动标签的方法
---标题:无标题"作者:我"日期:"2020年4月14日"输出:html_document---```{r}库(ggplot2)库(ggforce)```##标头{.tabset}```{r,results ='asis'}n <-10#制作主图情节<-ggplot(钻石)+geom_point(aes(克拉,价格),alpha = 0.1)#Facet _ * _ paginate在构面上循环情节<-lapply(seq_len(n),function(i){图+ facet_wrap_paginate(〜cut:clarity,ncol = 1,nrow = 1,page = i)})#为每个n打印一个选项卡和一个图#在块选项中设置结果='asis'"很重要对于(i in seq_len(n)){cat(paste0("\ n \ n ###制表符",i,"\ n"))打印(图[[i]])}```
I am aware of facet_wrap
and facet_grid
for faceting. For more detailed graphics requiring more space, it could be useful to easily be able to facet across Rmarkdown tabs
Other than copying the graphic's code into each tab and filtering for the facet variable separately in each one, is there a native (ggplot2/Rmarkdown) way to do this?
To borrow the example from the linked question, the desired output would be to have the first facetted plot where it says 'tab content 1', and the second in the second tab, and so on (with as many tabs as facets)
Here is how you could go about making these automated tabs
---
title: "Untitled"
author: "me"
date: "14 April 2020"
output: html_document
---
```{r}
library(ggplot2)
library(ggforce)
```
## A header {.tabset}
```{r, results='asis'}
n <- 10
# Make main plot
plot <- ggplot(diamonds) +
geom_point(aes(carat, price), alpha = 0.1)
# Facet_*_paginate loop over facets
plots <- lapply(seq_len(n), function(i) {
plot + facet_wrap_paginate(~ cut:clarity, ncol = 1, nrow = 1, page = i)
})
# Print a tab and a plot for each n
# Important to set "results = 'asis'" in chunk options
for (i in seq_len(n)) {
cat(paste0("\n\n### Tab ", i, "\n"))
print(plots[[i]])
}
```
这篇关于如何在ggplot2/Rmarkdown中跨标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!