在基于Rmarkdown的一个shiny小部件中同时绘制2个图。

3
我有一个问题。我想在Rmarkdown文档中并排放置两个图形:
```{r, echo = FALSE}
library(ggplot2)
library(shiny)
```

```{r,  echo = FALSE,fig.show='hold', out.width='50%'}
ggplot(mtcars,aes(qsec, mpg)) + 
  geom_point()
ggplot(mtcars,aes(disp, mpg)) + 
  geom_point()
```

它可以正常工作。但是我还想添加一个闪亮的小部件,与这两个图表相关联。这样,图表就不会并排显示。

```{r, echo = FALSE, fig.show='hold', out.width='50%'}
iteration <- sort(unique(mtcars$cyl))

selectInput('cyl', 'Choose number of cylinder:',
            choices = iteration,
            selected = iteration[1])
data <- reactive(subset(mtcars,cyl == input$cyl))

renderPlot(ggplot(data(),aes(qsec, mpg)) + 
  geom_point())

renderPlot(ggplot(data(),aes(disp, mpg)) + 
  geom_point()) 
```

我应该做哪些改变才能达到所期望的效果?

谢谢。

1个回答

3
在这种情况下,您应该按照Shiny教程中所示进行个性化布局,例如将其分为2列:
```{r, echo = FALSE, fig.show='hold', out.width='50%'}
iteration <- sort(unique(mtcars$cyl))

selectInput('cyl', 'Choose number of cylinder:',
            choices = iteration,
            selected = iteration[1])
data <- reactive(subset(mtcars,cyl == input$cyl))

fluidRow(
  column(6, 
    renderPlot(ggplot(data(),aes(qsec, mpg)) + 
                 geom_point())
         ),
  column(6, 
    renderPlot(ggplot(data(),aes(disp, mpg)) + 
                 geom_point()) 
         )
)
```

在这种情况下,我使用了一个流体等级系统,您可以将其更改为固定的( fixedRow )。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接