按照此post https://github.com/ropensci/plotly/issues/273#issuecomment-531254687这可以像这样实现:
Edit:按照此post https://stackoverflow.com/questions/42532129/passing-fig-width-into-a-taglist我添加了两个函数来传递fig.width
and fig.height
到 ggplotly。
Edit 2:添加了额外使用的代码plotly::subplot
s.
---
title: test
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r, echo=FALSE}
# Get the current figure size in pixels:
get_w <- function() {
with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
fig.width*dpi/fig.retina)
}
get_h <- function() {
with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
fig.height*dpi/fig.retina)
}
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
# level 1
## level 2 {.tabset .tabset-pills}
```{r, include=FALSE}
htmltools::tagList(ggplotly(fig))
```
```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
fig <- ggplotly(fig, width = get_w(), height = get_h())
for (h in 1:3) {
cat("###", h, '{-}', '\n\n')
print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
cat( '\n\n')
}
```