将图像动态调整大小 Flexdashboard/Plotly 为不同的图形

Dynamic Re-sizing Flexdashboard/Plotly Image to a Different Graphic

我正在使用 plotly 构建一个动态 flexdashboard,我想知道是否有办法动态调整我的仪表板的大小。例如,我创建了随着时间的推移接受测试的主题图。当我缩小页面时,我希望它能够动态调整到每个测试日该组平均值的时间序列图。

我的数据是这样的:

library(flexdashboard)
library(knitr)
library(tidyverse)
library(plotly)

subject <- rep(c("A", "B", "C"), each = 8)
testDay <- rep(1:8, times = 3)
variable1 <- rnorm(n = length(subject), mean = 30, sd = 10)
variable2 <- rnorm(n = length(subject), mean = 15, sd = 3)
df <- data.frame(subject, testDay, variable1, variable2)

   subject testDay variable1 variable2
1        A       1 21.816831  8.575000
2        A       2 14.947327 17.387903
3        A       3 18.014435 16.734653
4        A       4 33.100524 11.381793
5        A       5 37.105911 13.862776
6        A       6 32.181317 10.722458
7        A       7 41.107293  9.176348
8        A       8 36.674051 17.114815
9        B       1 33.710838 17.508234
10       B       2 23.788428 13.903532
11       B       3 42.846120 17.032208
12       B       4  9.785957 15.275293
13       B       5 32.551619 21.172497
14       B       6 36.912465 18.694263
15       B       7 40.061797 13.759541
16       B       8 41.094825 15.472144
17       C       1 27.663408 17.949291
18       C       2 31.263966 11.546486
19       C       3 39.734050 19.831854
20       C       4 25.461309 19.239821
21       C       5 22.128139 10.837672
22       C       6 31.234339 16.976004
23       C       7 46.273664 19.255745
24       C       8 27.057218 21.086204

我的 plotly 代码如下所示(每个主题随时间变化的图表):

Dynamic Chart
===========================

Row
-----------------------------------------------------------------------

```{r}
p1 <- df %>%
  ggplot(aes(x = as.factor(testDay), y = variable1, color = subject, group = 1)) +
  geom_line() +
  theme_bw() +
  ggtitle("Variable 1")

ggplotly(p1)
```

```{r}
p2 <- df %>%
  ggplot(aes(x = as.factor(testDay), y = variable2, color = subject, group = 1)) +
  geom_line() +
  theme_bw() +
  ggtitle("Variable 2")

ggplotly(p2)
```

有没有一种方法可以在我缩小网站时将这些图动态更改为一组平均图,如下所示:

p1_avg <- df %>%
  ggplot(aes(x = as.factor(testDay), y = variable1, group = 1)) +
  stat_summary(fun.y = "mean", geom = "line") +
  theme_bw() +
  ggtitle("Variable 1 Avg")

ggplotly(p1_avg)
p2_avg <- df %>%
  ggplot(aes(x = as.factor(testDay), y = variable2, group = 1)) +
  stat_summary(fun.y = "mean", geom = "line") +
  theme_bw() +
  ggtitle("Variable 2 Avg")

ggplotly(p2_avg)

您可以将 plotly 对象放入 plotly 函数 renderPlotly() 以动态调整页面大小。请参阅我如何使用此博客中的函数的示例 post: https://medium.com/analytics-vidhya/shiny-dashboards-with-flexdashboard-e66aaafac1f2