在 for 循环中绘制图表 [英] Plotly charts in a for loop
问题描述
我正在使用 plotly 转换 ggplot2 图表并将它们放入 knitr html 输出中.这是报告的工作副本.http://rpubs.com/kausti/NSEFO-23-Mar-2016第一个图表是情节图表,以下不是.我希望它们是图表,但我被卡住了.
I am converting ggplot2 charts using plotly and putting them in a knitr html output. Here is a working copy of the report. http://rpubs.com/kausti/NSEFO-23-Mar-2016 The first chart is plotly chart and the following ones are not. I would like them to be plotly charts but I am stuck.
在下面的代码中,plotChart 函数返回一个 ggplot2 绘图.此代码有效!
In the code below, plotChart function returns a ggplot2 plot. This code works!
for (tick in tradeOps$ticker)
{
tmpDF = filter(tradeOps, ticker == tick)
p = plotChart(tick = tick,Entry = tmpDF$Entry, Target = tmpDF$Target, SL= tmpDF$SL, TradeType = tmpDF$TradeType, data = wd)
p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$Entry, label = tmpDF$Entry, colour = "blue")
p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$Target, label = tmpDF$Target)
p = p + annotate("text", x = as.Date(tmpDF$Date)+30, y = tmpDF$SL, label = tmpDF$SL)
print(p)
}
现在,如果我将最后一个打印语句更改为 print(ggplotly(p))
,它就不会在 html 中打印图表.该代码在 knitr 之外完美运行.
Now if I change the last print statement to print(ggplotly(p))
it just won't print the charts in html.
The code works outside knitr perfectly.
此外,print(ggplotly(p))
在循环外也能完美运行.我错过了什么吗?
Also, print(ggplotly(p))
works perfectly outside loops.
Am I missing something?
包括以下可重现的示例.以下是我的 rmd 文件.
Including a reproducible example below. Following is my rmd file.
---
title: "tmp"
output: html_document
---
```{r, echo=FALSE, message=FALSE, fig.width=8, fig.height=6}
library(ggplot2)
library(plotly)
library(dplyr)
s = NULL
a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
for(i in unique(a$z)){
s = a[a$z == i,]
print(ggplot(s,aes(x = x, y = y)) + geom_point())
}
```
这与显示三个图表的输出 html 完美配合.现在,只需将最后一个打印语句更改为print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))
生成一个没有任何错误的空白 html.
This works perfectly with the output html showing three graphs.
Now, just changing the last print statement to
print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))
produces a blank html without any errors.
在终端中运行相同的代码效果很好
Running the same code in terminal works perfectly though
library(ggplot2)
library(plotly)
library(dplyr)
s = NULL
a = data.frame(id = 1:15,x = 2:16, y = 15:1, z = c(rep("a",5),rep("b",5),rep("c",5)))
for(i in unique(a$z)){
s = a[a$z == i,]
print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point()))
}
感谢任何帮助.
谢谢,考斯图布
推荐答案
原来问题在这里解决了https://github.com/ropensci/plotly/issues/273
knitr 在循环中打印绘图图表时存在已知问题.从这里引用最终答案cpsievert
knitr has known issues printing plotly charts in a loop. Quoting the final answer cpsievert from here
```{r}
l <- htmltools::tagList()
for (i in 1:3) {
l[[i]] <- as.widget(plot_ly(x = rnorm(10)))
}
l
```
这解决了我的问题.尽管在找到此解决方案之前,我最终将所有内容从 ggplot2 移植到 plotly R api.
This solved my problem. Though I ended up porting everything from ggplot2 to plotly R api before I found this solution.
MLavoie,感谢您的帮助.我生成了一个虚拟数据集,只是为了创建一个可重现的示例.尽管您的解决方案可能解决了虚拟示例,但在当前问题中与我无关.我认为这里提供的解决方案应该是通用的.
MLavoie, thanks for your help. I had generated a dummy dataset just to create a reproducible example. Though your solution probably solves the dummy example, it is not all that relevant to me in the current problem. I think the solution provided here should be generic.
谢谢,考斯图布
这篇关于在 for 循环中绘制图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!