106 lines
3.8 KiB
R
106 lines
3.8 KiB
R
#
|
||
#
|
||
# Home
|
||
# Public
|
||
#
|
||
# Questions
|
||
# Tags
|
||
# Users
|
||
# Find a Job
|
||
# Jobs
|
||
# Companies
|
||
#
|
||
# Teams
|
||
# Stack Overflow for Teams – Collaborate and share knowledge with a private group.
|
||
#
|
||
# How can put multiple plots side-by-side in shiny r?
|
||
# Asked 5 years, 5 months ago
|
||
# Active 2 years, 1 month ago
|
||
# Viewed 59k times
|
||
# 28
|
||
# 11
|
||
#
|
||
# In mainpanel, I try to handle this problem via fluidrow. However, one of my plot is optional to be displayed or not by users. When user clicks the button, the second plot appears below the first plot.
|
||
#
|
||
# fluidRow(
|
||
# column(2, align="right",
|
||
# plotOutput(outputId = "plotgraph1", width = "500px",height = "400px"),
|
||
# plotOutput(outputId = "plotgraph2", width = "500px",height = "400px")
|
||
# ))
|
||
#
|
||
# I played with "align" and "widths", but nothing changed.
|
||
# r
|
||
# plot
|
||
# ggplot2
|
||
# shiny
|
||
# Share
|
||
# Improve this question
|
||
# Follow
|
||
# edited Jan 21 '17 at 17:51
|
||
# Mike Wise
|
||
# 18.8k66 gold badges7171 silver badges9595 bronze badges
|
||
# asked Dec 20 '15 at 19:25
|
||
# can.u
|
||
# 42511 gold badge44 silver badges1111 bronze badges
|
||
# Add a comment
|
||
# 3 Answers
|
||
# 30
|
||
#
|
||
# So it is a couple years later, and while the others answers - including mine - are still valid, it is not how I would recommend approaching it today. Today I would lay it out using the grid.arrange from the gridExtra package.
|
||
#
|
||
# It allows any number of plots, and can lay them out in a grid checkerboard-like. (I was erroneously under the impression splitLayout only worked with two).
|
||
# It has more customization possibilities (you can specify rows, columns, headers, footer, padding, etc.)
|
||
# It is ultimately easier to use, even for two plots, since laying out in the UI is finicky - it can be difficult to predict what Bootstrap will do with your elements when the screen size changes.
|
||
# Since this question gets a lot of traffic, I kind of think more alternative should be here.
|
||
#
|
||
# The cowplot package is also worth looking into, it offers similar functionality, but I am not so familiar with it.
|
||
#
|
||
# Here is a small shiny program demonstrating that:
|
||
#
|
||
library(shiny)
|
||
library(ggplot2)
|
||
library(gridExtra)
|
||
|
||
u <- shinyUI(fluidPage(
|
||
titlePanel("title panel"),
|
||
sidebarLayout(position = "left",
|
||
sidebarPanel("sidebar panel",
|
||
checkboxInput("donum1", "Make #1 plot", value = T),
|
||
checkboxInput("donum2", "Make #2 plot", value = F),
|
||
checkboxInput("donum3", "Make #3 plot", value = F),
|
||
sliderInput("wt1","Weight 1",min=1,max=10,value=1),
|
||
sliderInput("wt2","Weight 2",min=1,max=10,value=1),
|
||
sliderInput("wt3","Weight 3",min=1,max=10,value=1)
|
||
),
|
||
mainPanel("main panel",
|
||
column(6,plotOutput(outputId="plotgraph", width="500px",height="400px"))
|
||
))))
|
||
|
||
s <- shinyServer(function(input, output)
|
||
{
|
||
set.seed(123)
|
||
pt1 <- reactive({
|
||
if (!input$donum1) return(NULL)
|
||
qplot(rnorm(500),fill=I("red"),binwidth=0.2,main="plotgraph1")
|
||
})
|
||
pt2 <- reactive({
|
||
if (!input$donum2) return(NULL)
|
||
qplot(rnorm(500),fill=I("blue"),binwidth=0.2,main="plotgraph2")
|
||
})
|
||
pt3 <- reactive({
|
||
if (!input$donum3) return(NULL)
|
||
qplot(rnorm(500),fill=I("green"),binwidth=0.2,main="plotgraph3")
|
||
})
|
||
output$plotgraph = renderPlot({
|
||
ptlist <- list(pt1(),pt2(),pt3())
|
||
wtlist <- c(input$wt1,input$wt2,input$wt3)
|
||
# remove the null plots from ptlist and wtlist
|
||
to_delete <- !sapply(ptlist,is.null)
|
||
ptlist <- ptlist[to_delete]
|
||
wtlist <- wtlist[to_delete]
|
||
if (length(ptlist)==0) return(NULL)
|
||
|
||
grid.arrange(grobs=ptlist,widths=wtlist,ncol=length(ptlist))
|
||
})
|
||
})
|
||
shinyApp(u,s)
|