r - r:R Markdown 不渲染多个绘图
问题描述
我创建了一个函数,它以编程方式为预建模Data Review生成必要的图表。
我的目标是在 R Markdown 中运行这些以创建演示文稿,但是,当我尝试创建html_document
、slidy_presentation
或ioslides_presentation
时,它们不会在输出中呈现。
查看函数代码(该函数在 RStudio 中运行时有效):
data_review<- function(df, dateVar, depVar, indVars){
for (i in indVars){
scale_factor<- max(df[depVar])/max(df[i])
cols <- c("Dependent" = "skyblue", "Independent" = "black")
print(plotly::ggplotly(ggplot2::ggplot(df, ggplot2::aes(x= !!as.name(dateVar))) +
ggplot2::geom_line(ggplot2::aes(y= !!as.name(depVar), color= 'Dependent')) +
ggplot2::geom_line(ggplot2::aes(y= !!as.name(i)*scale_factor, color= "Independent")) +
ggplot2::scale_colour_manual(name= 'Variables', values=cols) +
ggplot2::ylab(depVar) +
ggplot2::xlab(toupper(dateVar)) +
ggplot2::ggtitle(label = paste(depVar, 'vs.', i, sep = ' '))))
}
}
以及对应的数据:
df<- data.frame(date= seq(as.Date('2019-01-01'),as.Date('2019-01-10'),'days'),
sales= rnorm(n = 10, mean = 5, sd = 1),
y1= rnorm(n = 10, mean = 6, sd = 2),
y2= rnorm(n = 10, mean = 3, sd = 1),
y3= rnorm(n = 10, mean = 5, sd = 1)
)
在 Studio 中运行
data_review(df = df, dateVar = 'date', depVar = 'sales', indVars = c('y1', 'y2', 'y3'))
在 R Markdown 中运行(slidy_presentation
示例)
我怎样才能得到要渲染的图?
解决方案
这是否适用于创建 html 文档的 data_review 函数?
data_review<- function(df, dateVar, depVar, indVars){
plotlist = list()
for (i in indVars){
scale_factor<- max(df[depVar])/max(df[i])
cols <- c("Dependent" = "skyblue", "Independent" = "black")
p <- ggplot2::ggplot(df, ggplot2::aes(x= !!as.name(dateVar))) +
ggplot2::geom_line(ggplot2::aes(y= !!as.name(depVar), color= 'Dependent')) +
ggplot2::geom_line(ggplot2::aes(y= !!as.name(i)*scale_factor, color= "Independent")) +
ggplot2::scale_colour_manual(name= 'Variables', values=cols) +
ggplot2::ylab(depVar) +
ggplot2::xlab(toupper(dateVar)) +
ggplot2::ggtitle(label = paste(depVar, 'vs.', i, sep = ' '))
plotlist[[i]] = plotly::ggplotly(p)
}
htmltools::tagList(setNames(plotlist, NULL))
}
推荐阅读
- android - 为什么隐藏地址栏时 html { height:100% } 不起作用?
- javascript - Jest 中针对模拟调用 argumnets 的模糊浮点检查
- mongodb - 请帮助。如何摆脱 mongoDB 中的这个错误
- html - 媒体文件使用
- android - 信号 5 / 信号 11 的本机崩溃 - Chrome.apk
- python - 在python中显示下载百分比进度条
- java - 无法将过滤后的图像保存到外部存储android
- c - 更快的 AVX/2 矩阵向量乘法?
- javascript - 将视频分段流式传输为 1 个视频
- javascript - 我可以使用 Javascript 遍历文件夹中的文件并在 HTML 页面上创建指向它们的链接吗?