[英]Multiple Plots in R Shiny
我試圖在我的Shiny應用程序的主面板中顯示多個圖表。
我正在使用R cookbook的多重繪圖功能
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
library(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
然后調用multiplot
multiplot(p1,p2,p3,p4,p5,p6, cols=1)
但它似乎試圖將所有內容壓入主面板
這是我的應用程序只有一個情節的樣子
這是我的ui.R
shinyUI(fluidPage(
titlePanel("Baseball"),
sidebarLayout(
sidebarPanel(
selectInput(
"var", label = "Choose a group to visualize",
choices =c(Overall = "overall", Offense = "offense", Pitchers = "pitchers", Simpsons = "simpsons"),
selected = "offense")
),
mainPanel(
plotOutput("plotOffense")
)
)
)
)
我是否需要使用某些東西而不是mainPanel來允許在瀏覽器中放置更多圖形?
好吧,實際上只需要發生兩件事:應該調用plotOutput
來為實際輸出創建div,並且需要調用renderPlot
以正確的方式格式化繪圖。 因此,這里有一些可以動態執行此操作的函數,並且允許您使用寬度/高度/列數,類似於多重繪圖,只能以閃亮的方式進行。 請參閱此要點 。
我把東西分成了函數,但它也可以直接放到服務器函數中。
編輯:我忘了提,寬度和高度輸入框是文本,應該是有效的CSS,所以它可以是例如10,10px或10%。
library(shiny)
library(ggplot2)
## Some sample data
dat <- setNames(data.frame(matrix(runif(100),10)), letters[1:10])
dat$time <- seq(nrow(dat))
## Make some random plots because it looks cooler
## But you would just define your 10 different plots
rndmPlot <- function(input)
sample(list(geom_line(), geom_bar(stat='identity'), geom_point(), geom_jitter(),
geom_density(aes_string(x=input$var), inherit.aes=FALSE)), 1)
makePlotContainers <- function(n, ncol=2, prefix="plot", height=100, width="100%", ...) {
## Validate inputs
validateCssUnit(width)
validateCssUnit(height)
## Construct plotOutputs
lst <- lapply(seq.int(n), function(i)
plotOutput(sprintf('%s_%g', prefix, i), height=height, width=width))
## Make columns
lst <- lapply(split(lst, (seq.int(n)-1)%/%ncol), function(x) column(12/ncol, x))
do.call(tagList, lst)
}
renderPlots <- function(n, input, output, prefix="plot") {
for (i in seq.int(n)) {
local({
ii <- i # need i evaluated here
## These would be your 10 plots instead
output[[sprintf('%s_%g', prefix, ii)]] <- renderPlot({
ggplot(dat, aes_string(x='time', y=input$var)) + rndmPlot(input)
})
})
}
}
ui <- shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput('nplots', 'Number of Plots', min=1, max=10, value=8),
selectInput("var", label = "Choose", choices=letters[1:10]),
textInput('height', 'Plot Height', value="100"),
textInput('width', 'Width', value="100%"),
sliderInput('ncol', 'Columns', min=1, max=3, value=2)
),
mainPanel(
uiOutput('plots')
)
)
)
)
server <- shinyServer(function(input, output) {
output$plots <- renderUI({
makePlotContainers(input$nplots, ncol=input$ncol, height=input$height, width=input$width)
})
observeEvent(input$nplots, renderPlots(input$nplots, input, output))
})
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.