Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotly charts in a for loop

Tags:

r

knitr

plotly

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.

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)

}

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.

Also, print(ggplotly(p)) works perfectly outside loops. Am I missing something?

EDIT:

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())
}

```

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()))
}

Appreciate any help.

Thanks, Kaustubh

like image 845
kaustubh shinde Avatar asked Mar 26 '16 10:03

kaustubh shinde


2 Answers

It turns out the problem is solved here https://github.com/ropensci/plotly/issues/273

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

This solved my problem. Though I ended up porting everything from ggplot2 to plotly R api before I found this solution.

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.

Thanks, Kaustubh

like image 143
kaustubh shinde Avatar answered Oct 23 '22 12:10

kaustubh shinde


I have a couple of alternatives for you. Let me know if it is helpful or not!

Suggestion1 (base plotly using subplot):

```{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)))
a$id2 <- as.integer(a$z)
p <- plot_ly(a, x = x, y = y, group=z, xaxis = paste0("x", id2), mode = "markers")
p <- subplot(p, nrows=3)
p

```

Suggestion2 (with ggplot2 but using facet):

```{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)))
ggplotly(ggplot(a,aes(x = x, y = y)) + geom_point() + facet_grid(z~.))

```
like image 22
MLavoie Avatar answered Oct 23 '22 12:10

MLavoie