【问题标题】:How to display ggplotly plots with dynamically created tabs and for-loops?如何使用动态创建的选项卡和 for 循环显示 ggplotly 图?
【发布时间】:2020-09-06 10:17:36
【问题描述】:

我有 R markdown 文档,我想动态创建带有 ggplotly 图形的选项卡

---
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
library(ggplot2)
library(plotly)
```

```{r}
fig=ggplot(cars)+geom_point(aes(speed, dist))
```

# level 1
## level 2{.tabset .tabset-pills}

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

for (h in 1:3){
  cat("###", h,'{-}',  '\n\n')
 ggplotly(fig)
  cat( '\n\n')
}
```

我知道它与普通的 ggplot 图表不同,我在这里查看了解决方案:enter link description here 但它对我不起作用

【问题讨论】:

    标签: for-loop r-markdown plotly


    【解决方案1】:

    按照post,可以这样实现:

    编辑:post 之后,我添加了两个函数来将fig.widthfig.height 传递给ggplotly。

    编辑2:添加了额外使用plotly::subplots的代码。

    ---
    title: test
    date: "20 5 2020"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r}
    library(ggplot2)
    library(plotly)
    ```
    
    ```{r, echo=FALSE}
    # Get the current figure size in pixels:
    get_w <- function() {
      with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
           fig.width*dpi/fig.retina)
    }
    
    get_h <- function() {
      with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
           fig.height*dpi/fig.retina)
    }
    ```
    
    ```{r}
    fig <- ggplot(cars) + 
      geom_point(aes(speed, dist))
    ```
    
    # level 1
    
    ## level 2 {.tabset .tabset-pills}
    
    ```{r, include=FALSE}
    htmltools::tagList(ggplotly(fig))
    ```
    
    ```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
    fig <- ggplotly(fig, width = get_w(), height = get_h())
    for (h in 1:3) {
      cat("###", h, '{-}',  '\n\n')
      print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
      cat( '\n\n')
    }
    ```
    

    【讨论】:

    • 斯蒂芬,非常感谢!有没有办法设置图表的大小以使其更大?我尝试这样做:``{r echo=FALSE, results='asis'} 添加 fig.width 和 height,但不起作用我不想在全局范围内这样做,因为每个图都有不同的图形大小
    • 嗨@yuliaUU。我刚刚编辑了我的帖子。我从stackoverflow.com/questions/42532129/… 借用了使用 fig.width 和 fig.height 调整大小的解决方案
    • 抱歉这么多问题,我试图提供一个可重现的示例,但现在当我尝试将其调整为我自己的代码时,它无法正常工作。我实际上在绘制:ff
    • 嗨@yuliaUU。你必须反过来做。首先ggplotly。第二个情节::子情节。而不是 ggplotly 将 plotly::subplot 放在 htmlttols::taglist 中。我相应地编辑了我的代码。
    • 我想知道你是否可以在这里帮助我解决类似的问题:stackoverflow.com/questions/62016121/…。我尝试做与您在此处提供的相同的事情,但它不起作用
    【解决方案2】:

    也从this link 找到了解决方案,这不需要 HTML,只需要 markdown。

     ---
     date: "20 5 2020"
     output: html_document
     ---
    
     ```{r setup, include=FALSE}
     knitr::opts_chunk$set(echo = TRUE)
     ```
    
     ```{r}
     library(ggplot2)
     library(plotly)
     ```
    
     ```{r}
     fig <- ggplot(cars) + 
       geom_point(aes(speed, dist))
     ```
    
     ## Results {.tabset}
    
     ### 1
    
     We show a scatter plot in this section.
    
     ```{r}
     ggplotly(fig)
     ```
    
     ### 2
    
     We show the data in this tab.
    
     ```{r}
     ggplotly(fig)
     ```     
    

    【讨论】:

    • 这个答案与我的问题无关。手动添加标签不是问题。我在问动态生成它们。 ggplotly 在这种情况下不起作用
    猜你喜欢
    • 2018-01-14
    • 2018-09-09
    • 2013-03-09
    • 2018-11-11
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    相关资源
    最近更新 更多