12

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

2
  • your post is not reproducible. Anyway you could add some data to your post? Commented Mar 28, 2016 at 19:13
  • @MLavoie I have edited the question to include a reproducible example. Thanks. Commented Mar 29, 2016 at 19:24

2 Answers 2

22

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

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me in 2022. Changed as.widget() to as_widget() after getting a warning about as.widget() being deprecated.
4

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~.))

```

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.