2016-03-26 5 views
1

Ich konvertiere ggplot2 Diagramme mit plotly und setze sie in eine knit HTML-Ausgabe. Hier ist eine Arbeitskopie des Berichts. http://rpubs.com/kausti/NSEFO-23-Mar-2016 Das erste Diagramm ist Diagrammdiagramm und die folgenden sind nicht. Ich möchte, dass sie Plots sind, aber ich stecke fest.Plot-Diagramme in einer for-Schleife

Im folgenden Code gibt die plotChart-Funktion ein ggplot2-Diagramm zurück. Dieser Code funktioniert!

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) 

} 

Nun, wenn ich die letzte print-Anweisung zu print(ggplotly(p)) ändern sie einfach nicht die Charts in html drucken. Der Code funktioniert außerhalb von Knitr perfekt.

Auch funktioniert print(ggplotly(p)) perfekt außerhalb Schleifen. Fehle ich etwas?

EDIT:

unter einem reproduzierbaren Beispiel Einschließlich. Folgendes ist meine RMD-Datei.

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

``` 

Dies funktioniert perfekt mit der Ausgabe HTML zeigt drei Grafiken. Jetzt nur die letzte Druckanweisung zu print(ggplotly(ggplot(s,aes(x = x, y = y)) + geom_point())) ändern erzeugt ein leeres HTML ohne Fehler.

den gleichen Code in Terminal Lauf funktioniert perfekt obwohl

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

jede Hilfe dankbar.

Danke, Kaustubh

+0

Ihr Beitrag ist nicht reproduzierbar. Wie auch immer, du könntest Daten zu deinem Beitrag hinzufügen? – MLavoie

+0

@MLavoie Ich habe die Frage bearbeitet, um ein reproduzierbares Beispiel aufzunehmen. Vielen Dank. –

Antwort

4

Es stellt sich heraus das Problem https://github.com/ropensci/plotly/issues/273

knitr hier gelöst hat Probleme beim Drucken von Charts in einer Schleife. Zitiert die endgültige Antwort cpsievert von hier

```{r} 
l <- htmltools::tagList() 
for (i in 1:3) { 
    l[[i]] <- as.widget(plot_ly(x = rnorm(10))) 
} 
l 
``` 

Dies löste mein Problem. Obwohl ich alles von ggplot2 zu plotly R api portierte, bevor ich diese Lösung fand.

MLavoie, danke für Ihre Hilfe. Ich habe einen Dummy-Datensatz erstellt, um ein reproduzierbares Beispiel zu erstellen. Obwohl Ihre Lösung wahrscheinlich das Dummy-Beispiel löst, ist es für das aktuelle Problem nicht relevant. Ich denke, die hier zur Verfügung gestellte Lösung sollte generisch sein.

Danke, Kaustubh

1

Ich habe ein paar Alternativen für Sie. Lass es mich wissen, wenn es hilfreich ist oder nicht!

Suggestion1 (Basis plotlysubplot verwenden):

```{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 (mit ggplot2 aber mit 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~.)) 

``` 
Verwandte Themen