我试图从 R markdown ( ) 文件ggplotly
的内部for
循环中绘制一系列交互式图形。.Rmd
我的.Rmd
文件内容:
---
title: "Untitled"
output: html_document
---
```{r}
library(ggplot2) # for plots
library(plotly) # for interactive plots
# Convert 4 variables to factor variables:
factor_vars <- c("vs", "am", "gear", "carb")
mtcars[factor_vars] <- data.frame(Map(as.factor, mtcars[factor_vars]))
for (VAR in factor_vars) {
cat(paste("Factor variable:", VAR))
# Contents of "VAR" changes inside the loop
p <- ggplot(mtcars, aes_string(x = "mpg", y = "wt", color = VAR)) + geom_point()
# Print an interactive plot
print(ggplotly(p))
}
```
我按下Knit HTML
按钮RStudio
。不幸的是,文件中没有出现任何交互式绘图.html
。
问题:为什么不绘制图表?以及如何结合文件中的for
循环创建交互式绘图Rmd
?
ps 如果我使用print(p)
而不是print(ggplotly(p))
,ggplot2
绘图会出现在结果.html
文件中。