2013-08-06 60 views
5

我有一個R Shiny應用程序,它接收用戶csv文件並繪製csv文件的圖形。如何增加R Shiny中ggplot2圖的絕對大小?

我的R Shiny程序正在使用tabsetPanel,這使得圖形比我想要的更多。

ggplot()中是否有屬性可以添加以增加圖形的大小?

我也注意到,當我嘗試在一個選項卡中繪製多個圖形時,我只能訪問2行圖形,因爲UI的高度是有限的。我怎樣才能擴展它?

現在我有一個選項卡,但我打算以後再添加更多。

這裏是我的代碼:

ui.R

dataset <- list('Upload a file'=c(1)) 

shinyUI(pageWithSidebar(

    headerPanel(''), 

    sidebarPanel(

    wellPanel(
     fileInput('file', 'Data file'), 
     radioButtons('format', 'Format', c('CSV', 'TSV')) 
    ), 

    wellPanel(
      selectInput('xMulti', 'X', names(dataset)), 
      selectInput('yMulti', 'Y', names(dataset), multiple=T) 

                ) 

     wellPanel(
      checkboxInput('normalize', 'Normalize y axes', value=TRUE) 
    ), 


     wellPanel(
      sliderInput("cols", 
      "Plots per row", 
      min = 1, 
      max = 4, 
      value = 2 
     ) 
    ) 
) 

    mainPanel( 
     tabsetPanel(
      tabPanel("Multiplot", plotOutput('plotMulti'), value="multi"), 
      id="tsp"   #id of tab 
      ) 


) 
)) 

server.R

library(reshape2) 
library(googleVis) 
library(ggplot2) 

#Increase max upload size 
options(shiny.maxRequestSize=-1) 

shinyServer(function(input, output, session) { 


     data <- reactive({ 

    if (is.null(input$file)) 
     return(NULL) 
    else if (identical(input$format, 'CSV')) 
     return(read.csv(input$file$datapath)) 
    else 
     return(read.delim(input$file$datapath)) 
    }) 

    observe({ 
    df <- data() 
    str(names(df)) 


    if (!is.null(df)) { 

     updateSelectInput(session, 'xMulti', choices = names(df)) 
     updateSelectInput(session, 'yMulti', choices = names(df)) 


    } 
    }) 



    # Multiple plot function 
    # 
    # ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects) 
    # - cols: Number of columns in layout 
    # - layout: A matrix specifying the layout. If present, 'cols' is ignored. 
    # 
    # If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE), 
    # then plot 1 will go in the upper left, 2 will go in the upper right, and 
    # 3 will go all the way across the bottom. 
    # 
    multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) { 
    require(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)) 
     } 
    } 
    } 

    output$plotMulti <- renderPlot({ 
    if (is.null(data())) 
     return(NULL) 


    plots <- list() # new empty list 
    names <- input$yMulti 

    maxVal <- 0 

    for (i in 1:length(input$yMulti)) { 
     maxVal <- max(maxVal, max(data()[names[i]])) 
    } 

    for (i in 1:length(input$yMulti)) { 
     temp <- input$xMulti 


     p <- ggplot(data(), aes_string(x=temp, y=names[i])) 
     p <- p + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1)) 
     p <- p + labs(title=paste("",input$xMulti," VS ",input$yMulti,"")) 
     h <- ggplot(data(), aes_string(x=temp)) 
     h <- h + opts(axis.text.x=theme_text(angle=45, hjust=1, vjust=1)) 
     h <- h + labs(title=paste("",input$xMulti," VS ",input$yMulti,"")) 

     if (input$normalize) { 
     p <- p + scale_y_continuous(limits = c(0, maxVal)) 
     h <- h + scale_y_continuous(limits = c(0, maxVal)) 
     } 

     if (input$type == "Scatter") { 
     p <- p + geom_point(size = 3) 
     plots[[i]] <- p 
     } else if (input$type == "Line"){ 
     p <- p + geom_line(aes(group=1)) + geom_point() 
     plots[[i]] <- p 
     } else if (input$type == "Bar") { 
     p <- p + geom_bar() 
     plots[[i]] <- p 
     } else if (input$type == "Histogram") { 
     h <- h + geom_histogram(aes(fill = ..count..)) 
     h <- h + scale_fill_gradient("Count", low = "green", high = "red") 
     plots[[i]] <- h 
     } 

    } 


    multiplot(plotlist = plots, cols=input$cols) 


    }) 
}) 
+0

請花時間來縮短你的例子,如果你張貼在這裏。這不超過20行代碼的總要問「我如何更改圖形大小閃亮」。 –

+1

我會記住這一點,它只是當我這樣做縮短我的問題的人抱怨說,他們沒有足夠的信息。 – jeffrey

+0

「足夠的信息」意味着:足以在我們自己的環境中運行問題而無需額外的東西。 –

回答

6

renderPlot,您可以添加參數像素heightwidth

(新增,並在除了2017年4月修正)

爲了避免滾動條,使用renderPlot({whatever}, height="auto")

+1

我這個問題是,它增加了一個滾動條,但我只是想使圖形更大。我加入renderPlot(高度= 1000,單位=「PX」, – jeffrey

+0

見(新增)在回答 –

+0

真棒,太感謝你了 – jeffrey