在for循环中绘制图表 [英] Plotly charts in a for loop

查看:117
本文介绍了在for循环中绘制图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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()))
}

感谢任何帮助.

谢谢, Kaustubh

Thanks, Kaustubh

推荐答案

事实证明,问题已在此处解决 https://github.com/ropensci/plotly/issues/273

It turns out the problem is solved here 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移植到了可打印的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.

谢谢, Kaustubh

Thanks, Kaustubh

这篇关于在for循环中绘制图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆