考虑以下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)
```
我希望地块显示为:
我尝试了一些事情,包括:
fluidRow(
column(6, blank1, blank2),
column(6, blank3)
)
但是我无法获得Plot 3
来跨越多行。
附加说明(按评论):
cowplot
或patchwork
解决方案,但是我需要shiny
(例如ggplot(aes(x = input$var_select)) + ...
。)的响应。column()
和/或fluidRow()
来保持响应式设计方面。答案 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))
)
从响应设计的角度来看,这并不是完美的方法,但是它会起作用。
答案 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;')
)
)
```
结果如下: