如何在运行时闪亮的rmarkdown html_notebook中使用renderPlot布局2行紧随其后的1列

时间:2018-12-17 21:52:19

标签: r shiny r-markdown rnotebook

考虑以下rmarkdown html_notebook示例:

---
output: html_notebook
runtime: shiny
---

```{r}
library(ggplot2)
library(shiny)

blank1 <- renderPlot({ ggplot() + labs(title = "Plot 1") })
blank2 <- renderPlot({ ggplot() + labs(title = "Plot 2") })
blank3 <- renderPlot({ ggplot() + labs(title = "Plot 3") })

column(6, blank1, blank2)
column(6, blank3)
```

我希望地块显示为:

plot-layout

我尝试了一些事情,包括:

fluidRow(
  column(6, blank1, blank2),
  column(6, blank3)
)

但是我无法获得Plot 3来跨越多行。


附加说明(按评论):

  • 我欢迎使用cowplotpatchwork解决方案,但是我需要shiny(例如ggplot(aes(x = input$var_select)) + ...。)的响应。
  • 理想情况下,我想利用column()和/或fluidRow()来保持响应式设计方面。

2 个答案:

答案 0 :(得分:1)

我能够通过将高度显式传递到renderPlot中来解决此问题。我对其他解决方案仍然很感兴趣:

blank1 <- renderPlot({ ggplot() + labs(title = "Plot 1") }, height = 200)
blank2 <- renderPlot({ ggplot() + labs(title = "Plot 2") }, height = 200)
blank3 <- renderPlot({ ggplot() + labs(title = "Plot 3") }, height = 400)

fluidRow(
  column(6, fluidRow(blank1), fluidRow(blank2)),
  column(6, fluidRow(blank3))
)

从响应设计的角度来看,这并不是完美的方法,但是它会起作用。

Shiny with Plot Heights

答案 1 :(得分:1)

您可以尝试以下类似方法,将图和列的边距设置为0,并将图3的高度设置为图1和图2的两倍。

---
output: html_notebook
runtime: shiny
---

<!-- break between code folding button and fluid layout -->
<br>

```{r echo=FALSE, message=FALSE}
library(ggplot2)
library(shiny)

# create the plots telling ggplot to use 0 margins

p1 <- ggplot() +
      theme(plot.margin = unit(c(0,0,0,0), "cm"),
            panel.background=element_rect(fill = "lightgreen"))
p2 <- ggplot() +
      theme(plot.margin = unit(c(0,0,0,0), "cm"),
            panel.background=element_rect(fill = "lightblue"))
p3 <- ggplot() +
      theme(plot.margin = unit(c(0,0,0,0), "cm"),
            panel.background=element_rect(fill = "orange"))

# Set the heights in renderPlot so that plot 3 is twice the height
# of the other 2 plots

blank1 <- renderPlot({p1}, height=200)
blank2 <- renderPlot({p2}, height=200)
blank3 <- renderPlot({p3}, height=400)

# Tell the fluid layout to set padding and margin to 0 for the column divs

fluidPage(
  fluidRow(
    column(6, blank1, blank2, offset=0, style='padding:0px;margin:0px;'),
    column(6, blank3, offset=0, style='padding:0px;margin:0px;')
  )
)
```

结果如下:

enter image description here