【问题标题】:Display two rCharts NVD3 figures next to each other in rmarkdown在 rmarkdown 中并排显示两个 rCharts NVD3 图形
【发布时间】:2015-03-13 10:18:21
【问题描述】:

我想用 rCharts 包显示两个图表,一个并排,或多或少像此链接中显示的两个饼图:

http://nvd3.org/examples/pie.html

我有一个使用<iframe>的部分解决方案,但是该解决方案存在三个问题:

  1. 过于具体
  2. 包含控件成为一项复杂的任务
  3. 看起来不太好看

最小工作示例:

---
title: "Example"
output: html_document
---
```{r rcht, message=FALSE, echo=FALSE, results='asis'}
library(rCharts)
df<-data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),othera=c(10,11,12),
valueb=c(4,5,6),otherb=c(10,11,12),stringsAsFactors = FALSE)
p1 <- nPlot(valuea~ label, data = df, type = 'pieChart',height = 225, width = 300)
p2<- nPlot(valueb~ label, data = df, type = 'pieChart',height = 225, width = 300)
p1$show('inline', include_assets = TRUE, cdn = F)
p2$show('inline', include_assets = TRUE, cdn = F)
```
```{r message=FALSE, echo=FALSE}
p1$save("pie1.html", standalone = TRUE)
p2$save("pie2.html", standalone = TRUE)
```
<div  align="center"> 
<font size="10" color="black" face="sans-serif">Both Pies</font><br>
<p>
<iframe src="pie1.html" height="400" width="400"></iframe>
<iframe src="pie2.html" height="400" width="400"></iframe>
</p>
<div>

我知道不应使用饼图,我可以使用多条形图。但是,我想在 rCharts 包中将这种类型的布局与其他类型的图表一起使用。

此外,我想在图表中包含控件,同时它们彼此相邻显示。在$save()函数添加控件之前包括以下代码:

```{r message=FALSE, echo=FALSE} 
p1$addControls('y','valuea',values=c('valuea','othera'))
p2$addControls('y','valueb',values=c('valueb','otherb'))
```

这个问题与我无关,但如果有人有解决方案(最好两个图表都只有一个控件),那就太好了。

我知道 R 可能无法处理所有这些问题。感谢您提供任何帮助/建议。

【问题讨论】:

    标签: r nvd3.js r-markdown rcharts


    【解决方案1】:

    不优雅,但实用(我没有尝试使用控件):

    ---
    title: "Example"
    output: html_document
    ---
    
    ```{r rcht, message=FALSE, echo=FALSE, results='asis'}
    library(rCharts)
    library(htmltools)
    df <- data.frame(label=c("One","Two","Three"),valuea=c(1,2,3),othera=c(10,11,12),
    valueb=c(4,5,6),otherb=c(10,11,12),stringsAsFactors = FALSE)
    p1 <- nPlot(valuea~ label, data = df, type = 'pieChart',height = 225, width = 300)
    p2 <- nPlot(valueb~ label, data = df, type = 'pieChart',height = 225, width = 300)
    ```
    
    ```{r echo=FALSE, results="asis"}
    cat("<table width='100%'><tr style='width:100%'><td width='50%'>")
    ```
    
    ```{r echo=FALSE, results="asis"}
    p1$show('inline', include_assets = TRUE, cdn = FALSE)
    ```
    
    ```{r echo=FALSE, results="asis"}
    cat("</td><td>")
    ```
    
    ```{r echo=FALSE, results="asis"}
    p2$show('inline', include_assets = TRUE, cdn = FALSE)
    ```
    
    ```{r echo=FALSE, results="asis"}
    cat("</td></tr></table>")
    ```
    

    【讨论】:

    • 我已经离开几天了,抱歉回复晚了。正如你所指出的那样不优雅但实用。它确实有效,但我显然需要继续提高我的 javascript 技能。谢谢。
    • *.com/questions/30212788/… 我试过p1$print('inline', include_assets = F, cdn = FALSE)p1$show('inline', include_assets = F, cdn = FALSE)p1p1$show()p1$print(),但没有一个对我有用。 rpubs.com/englianhu/Milestone-Report
    【解决方案2】:

    您好,我在控件方面遇到了同样的问题,看起来在 R-studio 的查看器中一切正常,但当我使用 Rmarkdown 编译时,它根本不显示绘图。

    ```{r results = 'asis', comment = NA}
        require(rCharts)
        require(datasets)
    p2 <- nPlot(mpg ~ cyl, group = 'wt', 
            data = mtcars, type = 'scatterChart')
    
    p2$xAxis(axisLabel = 'Log2')
    p2$yAxis(axisLabel = 'Log2')
    p2$chart(tooltipContent = "#! function(key, x, y, e){ 
         return  '<b>Name:</b>  ' + e.point.GeneID
         } !#")
    p2$chart(color = c('red', 'green'))
    p2$addControls("x", value = 'mpg', values = names(mtcars))
    p2$addControls("y", value = 'cyl', values = names(mtcars))
    cat('<style>.nvd3{height: 400px;}</style>')
    p2$print('chart2', include_assets = TRUE)
    ```
    

    上面的代码是去掉了addControls,实际上在rmarkdown中也可以。

    此外,如果您尝试在 Rstudio 控制台中运行上面的代码(仅从 p2

    【讨论】: