代码之家  ›  专栏  ›  技术社区  ›  Jacksonsox

R Plot\u ly list在中绘制多个绘图。Rmd R-Studio跑步模式,但针织时不适用

  •  4
  • Jacksonsox  · 技术社区  · 7 年前

    我正在尝试从数据列表中绘制多个绘图。框架。我正在使用降价来渲染数据。在R-Studio中,当我单击“>”运行按钮,我得到所有的绘图。

    我尝试使用的代码是:

    ### Plot each list
    ```{r plotSWBS6IByPhaseAndWave, echo=TRUE, eval=TRUE}
    plotList <- list()
    
    for(i in 1:length(seriesFigureSaleDataBS6I_PhaseWave)) {
      plotList[[i]] <- plot_ly(data = seriesFigureSaleDataBS6I_PhaseWave[[i]],
                     x = ~priceDate,
                     y = ~amount,
                     color = ~actionFigurePackageName,
                     colors = "Pastel2",
                     type = "scatter",
                     mode = "markers") %>%
                     layout(title = paste("Phase", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Phase, "& Wave", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Wave))
    }
    # p <- lapply(seriesFigureSaleDataBS6I_PhaseWave, function(phaseWaveRow) plot_ly(data = phaseWaveRow, x = ~priceDate, y = ~amount, color = ~actionFigureUniqueId, colors = "Pastel2"))
    
    print(class(seriesFigureSaleDataBS6I_PhaseWave))
    print(summary(seriesFigureSaleDataBS6I_PhaseWave))
    
    #rm(seriesFigureSaleDataBS6I_PhaseWave)
    
    plotList
    ```
    

    列表如下所示:

    print(summary(seriesFigureSaleDataBS6I_PhaseWave))
                                                Length Class      Mode
    40th.1                                      35     data.frame list
    40th.2                                      35     data.frame list
    40th.Legacy                                 35     data.frame list
    Blue.5                                      35     data.frame list
    Blue.6                                      35     data.frame list
    Blue.7                                      35     data.frame list
    Blue.8                                      35     data.frame list
    ...
    

    Some data from the list

    运行模式下的输出如下所示:

    Run output

    knit输出只提供了R控制台输出:

    ## [[1]]
    ## 
    ## [[2]]
    ## 
    ## [[3]]
    ## 
    ## [[4]]
    ## 
    ## [[5]]
    

    如果我尝试以下代码,我将丢失R控制台输出(这很好),并在R-Studio“运行”模式下获得绘图,但在knit模式下没有获得绘图输出:

    for(i in 1:length(seriesFigureSaleDataBS6I_PhaseWave)) {
      print(plot_ly(data = seriesFigureSaleDataBS6I_PhaseWave[[i]],
                     x = ~priceDate,
                     y = ~amount,
                     color = ~actionFigurePackageName,
                     colors = "Pastel2",
                     type = "scatter",
                     mode = "markers") %>%
                     layout(title = paste("Phase", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Phase, "& Wave", seriesFigureSaleDataBS6I_PhaseWave[[i]]$Wave)))
    }
    
    1 回复  |  直到 7 年前
        1
  •  7
  •   Martin Schmelzer    7 年前

    使用 htmltools::tagList 功能:

    ---
    title: "Knit a List of Plotly Graphs"
    output: html_document
    ---
    
    ```{r, include = F}
    library(dplyr)
    library(plotly)
    library(htmltools)
    ```
    
    
    ```{r, echo=TRUE, eval=TRUE}
    
    dat <- list(mtcars, mtcars)
    plots <- lapply(dat, function(x) {
      plot_ly(data = x, x = ~hp, y = ~mpg) %>% 
        add_trace(type = "scatter", mode = "markers")
    })
    tagList(plots)
    ```
    

    enter image description here