2013-08-06 3 views
5

Ho un'app R Shiny che contiene i file csv dell'utente e grafici grafici dei file csv.Come posso aumentare la dimensione assoluta dei miei grafici ggplot2 in R Shiny?

Il mio programma R lucido utilizza un tabsetPanel e questo riduce i grafici più di quanto mi piacerebbe.

Esiste una proprietà in ggplot() che posso aggiungere per aumentare la dimensione del grafico?

Ho anche notato che quando provo a tracciare più grafici in una scheda, posso solo andare a 2 righe di grafici perché l'altezza dell'interfaccia utente è limitata. Come posso estendere questo?

Per ora ho una scheda ma ho intenzione di aggiungere più tardi.

Ecco il mio codice:

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

Si prega di prendere il tempo per accorciare i tuoi esempi se pubblichi qui. Non ci sono più di 20 righe di codice totale per chiedere "Come cambio la dimensione di un grafico in lucido". –

+1

Lo terrò a mente, è solo che quando accorro le mie domande le persone si lamentano di non avere abbastanza informazioni. – jeffrey

+0

"informazioni sufficienti" significa: abbastanza per eseguire il problema nel nostro ambiente senza ulteriori elementi. –

risposta

6

In renderPlot, è possibile aggiungere i parametri height e width in pixel.

(Aggiunto, e inoltre corretto nel mese di aprile 2017)

Per evitare la barra di scorrimento, utilizzare renderPlot({whatever}, height="auto")

+1

Il mio problema con questo è che aggiunge una barra di scorrimento, ma voglio solo ingrandire il grafico. Ho aggiunto renderPlot (altezza = 1000, unità = "px", – jeffrey

+0

Vedere (Aggiunto) in risposta –

+0

Impressionante, grazie mille! – jeffrey