交互式“ggplotly”图形不是从R中的“Rmd”文件中的“for”循环内部绘制的
我试图在R标记(交互式“ggplotly”图形不是从R中的“Rmd”文件中的“for”循环内部绘制的,r,ggplot2,knitr,plotly,htmlwidgets,R,Ggplot2,Knitr,Plotly,Htmlwidgets,我试图在R标记(.Rmd)文件中为循环从内部为绘制一系列交互式的ggplotly图形。我的.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"
.Rmd
)文件中为循环从内部为绘制一系列交互式的ggplotly
图形。我的.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))
}
```
---
标题:“无标题”
输出:html\u文档
---
```{r}
图库(ggplot2)#用于绘图
库(plotly)#用于交互式绘图
#将4个变量转换为因子变量:
基于此,您应该能够执行以下操作:
---
title: "Untitled"
output: html_document
---
```{r, message = F}
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]))
plt <- htmltools::tagList()
i <- 1
for (VAR in factor_vars) {
# Contents of "VAR" changes inside the loop
p <- ggplot(mtcars, aes_string(x = "mpg", y = "wt", color = VAR)) +
geom_point() +
ggtitle(paste("Factor variable:", VAR))
# Print an interactive plot
# Add to list
plt[[i]] <- as.widget(ggplotly(p))
i <- i + 1
}
```
```{r, echo = F}
plt
```
---
标题:“无标题”
输出:html\u文档
---
```{r,message=F}
图库(ggplot2)#用于绘图
库(plotly)#用于交互式绘图
#将4个变量转换为因子变量:
这是一个很好的工作环境。不幸的是,我无法在当前的自动分析中使用它,在每次迭代的for
循环图之后都会有表格、结果和一些统计测试。在一个地方打印所有情节会让人困惑。我注意到,pander()
的结果也不会从for
循环内部打印。这是knitr中的一些bug吗?看起来你也提出了对github回购的担忧。如果你与timelyportfolio合作,这可能是最好的选择。