【发布时间】:2016-07-14 01:11:10
【问题描述】:
我正在使用 plotly 转换 ggplot2 图表并将它们放入 knitr html 输出中。 这是报告的工作副本。 http://rpubs.com/kausti/NSEFO-23-Mar-2016 第一个图表是情节图表,以下不是。我希望它们是图表,但我被卡住了。
在下面的代码中,plotChart 函数返回一个 ggplot2 绘图。此代码有效!
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 之外完美运行。
另外,print(ggplotly(p)) 在循环外也能完美运行。
我错过了什么吗?
编辑:
包括以下可重现的示例。 以下是我的 rmd 文件。
---
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。
在终端中运行相同的代码效果很好
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()))
}
感谢任何帮助。
谢谢, 考斯图布
【问题讨论】:
-
您的帖子不可复制。无论如何,您可以在帖子中添加一些数据吗?
-
@MLavoie 我已编辑问题以包含一个可重现的示例。谢谢。