R - 在Shiny应用程序的单个页面上渲染多个图 [英] R - Render multiple plots on single page of Shiny app
本文介绍了R - 在Shiny应用程序的单个页面上渲染多个图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在闪亮的应用中排列多个图表。
I am trying to arrange multiple charts on shiny app. I am trying to plot 2 pie charts and a ggplot2 chart.
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
渲染这三个图,然后返回错误消息输入必须是grobs!请帮助渲染这三个单页面。
I tried with grid.arrange but it only render ggplot chart. I tried to to render these three plot using grid.arrange
, then it return error message input must be grobs! Please help to render these three on single page.
推荐答案
我不确定你想要它的样子,但这里有一个例子, :
I'm not certain what you want it to look like but here's an example how to combine your three plots:
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)
注意:我不推荐使用饼图。 看到这个例子
NOTE: I do not recommend using pie charts. See this example
这篇关于R - 在Shiny应用程序的单个页面上渲染多个图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文