【问题标题】:Generating dynamic .tabset in Rmarkdown在 Rmarkdown 中生成动态 .tabset
【发布时间】:2019-12-27 21:45:15
【问题描述】:

我想创建 3 个 .tabsets,每个级别的物种在 Iris 数据集上一个(示例)。 在每个 .tabset 上,需要 3 个或更多选项卡。每个标签都有你自己的情节。

我写了这段代码:

```{r echo=FALSE, results='asis'}

library(tidyverse)

for(especies in levels(iris$Species)){ 
  cat('\n#', especies, '{.tabset}', '\n')
  cat('\n##',  'Petal Lengh x Sepal Length', '\n')
  iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Sepal.Length,
        y = Petal.Length
      )
    ) +
    geom_point() -> p
  print(p)
  cat('\n')
  cat('\n##', 'Sepal Length', '\n')
    iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Sepal.Length
      )
    ) +
    geom_histogram() -> p
  print(p)
  cat('\n')
  cat('\n##', 'Petal Length', '\n')
    iris %>%
    filter(
      Species == especies
    ) %>%
    ggplot(
      aes(
        x = Petal.Length
      )
    ) +
    geom_histogram() -> p
  print(p)
  }
```

我不知道为什么它不起作用。有什么想法吗?

提前致谢!

堆栈不断要求我提供更多详细信息以发布问题,因此我必须再写一些才能被允许发布。

【问题讨论】:

  • Fala danilo blz, se você quer encadear gráficos no R da uma lida aqui cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html mas acho que isso resolver comp(mfrow=c(3,3))
  • 法拉,布鲁诺。 Não quero fazer um grid de plots。 Só dei um exemplos com o Iris dataset do que quero。 Quero realmente gerar dinamicamente um .tabset para casa level de iris$Species.

标签: r r-markdown knitr pandoc


【解决方案1】:

两件事可以得到想要的结果:

1) 插入 {r, results = "asis"} 作为块
2) 在最后一个printcall 之后添加cat('\n')

【讨论】:

  • 如果我输入print(plotly::ggplotly(p)),它不起作用:html 小部件未打印。你知道为什么或有什么办法解决这个问题吗?
【解决方案2】:

这是我认为可以很好地替代您的图表的东西

library(ggforce)


ggplot(iris) +
  aes(color = Species,fill = Species) +
  geom_autohistogram() +
  geom_autopoint() +
  geom_autodensity() +
  facet_matrix(vars(-Species), layer.diag = 1, layer.upper = 3, 
               grid.y.diag = FALSE)

【讨论】:

  • 你好。该图仅用于示例目的。我想要的是为每个 iris$Species 级别动态生成一个 .tabset,每个选项卡中都有图。
猜你喜欢
  • 1970-01-01
  • 2016-10-15
  • 2022-01-17
  • 2018-09-11
  • 2020-09-25
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
  • 2016-01-30
相关资源
最近更新 更多