在 rmarkdown 中并排显示两个 rCharts NVD3 图形

Display two rCharts NVD3 figures next to each other in rmarkdown

我想用 rCharts 包显示两个图表,一个挨着另一个,或多或少像两个饼图显示在这个 link:

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 中处理。感谢任何 help/advice。

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

---
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>")
```

您好,我在使用控件时遇到了同样的问题,看起来在 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<-nPlot 到 cat 命令)然后调用 p2,我实际上可以看到控件。