R - 在 Shiny 应用程序的单页上渲染多个图

R - Render multiple plots on single page of Shiny app

我正在尝试在 shiny 应用程序上排列多个图表。我正在尝试绘制 2 个饼图和一个 ggplot2 图表。

require(ggplot2)
require(gridExtra)

par(mfrow = c(2,2))    

z=data.frame(x=1:10, y=11:20)
pie(z$x,z$y)
pie(z$x,z$y)
ggplot(z, aes(x,y)) + geom_bar(stat="identity", width=.3)

我试过 grid.arrange 但它只呈现 ggplot 图表。我尝试使用 grid.arrange 渲染这三个图,然后它 return 错误消息 input must be grobs! 请帮助在单个页面上渲染这三个图。

我不确定你想要它是什么样子,但这里有一个如何组合你的三个图的例子:

require(ggplot2)
require(grid)
require(gridBase)

z=data.frame(x=1:10, y=11:20)

# setup everything
plot.new()
gl <- grid.layout(2,2)
vp.1 <- viewport(layout.pos.col = 1, layout.pos.row = 1)
vp.2 <- viewport(layout.pos.col = 2, layout.pos.row = 1)
vp.3 <- viewport(layout.pos.col = c(1,2), layout.pos.row = 2)
pushViewport(viewport(layout=gl))

# First plot
pushViewport(vp.1)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport()

# Second plot
pushViewport(vp.2)
par(new = TRUE, fig = gridFIG(), mar=c(0,0,0,0))
pie(z$x,z$y)
popViewport(1)

# Your ggplot
pushViewport(vp.3)
p<-ggplot(z, aes(x,y)) + geom_bar(stat="identity", width=.3)
print(p, newpage = FALSE)
popViewport(2)

注意:我不推荐使用饼图。 See this example