问题描述
我想用 rCharts 包显示两个图表,一个挨着另一个,或多或少像这个链接中显示的两个馅饼:
I want to display two charts with the rCharts package, one next to the other, more or less like the two pies are displayed in this link:
http://nvd3.org/examples/pie.html
我有一个使用 的部分解决方案,但该解决方案存在三个问题:
I have a partial solution using <iframe>
, but the solution has three problems:
- 案例太具体了
- 包含控件成为一项复杂的任务
- 看起来不太好看
最小工作示例:
---
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 包中的其他类型的图表一起使用.
I know pie charts should not be used and that I could use a multi-bar chart. However, I want to use this type of layout with other kinds of charts in the rCharts package.
此外,我想在图表中包含控件,同时它们彼此相邻显示.在 $save()
函数之前添加以下代码以添加控件:
Additionally, I would like to include controls in the charts whilst they are shown next to each other. Including the following code before the $save()
function adds the controls:
```{r message=FALSE, echo=FALSE}
p1$addControls('y','valuea',values=c('valuea','othera'))
p2$addControls('y','valueb',values=c('valueb','otherb'))
```
这个问题与我不太相关,但如果有人有解决方案(最好两个图表只有一个控件),那就太好了.
This issue is less relevant to me, but if someone has a solution (preferably with only one control for both charts), it would be great.
我知道 R 可能无法处理所有这些.感谢您提供任何帮助/建议.
I understand all this might be too much to handle from R. Any help/advice is appreciated.
推荐答案
不优雅,但功能强大(我没有用控件尝试过):
Not elegant, but functional (I did not try it with controls):
---
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>")
```
这篇关于在 rmarkdown 中并排显示两个 rCharts NVD3 图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!