2016-03-31 22 views
-3

私はcsvファイルを光沢を持ってアップロードし、選択した列からggplotを描画しようとしています。ggplot2で直線を描くことができません

output$plot = renderPlot(
    { 
     df <- data_set() 
     gp <- NULL 
     if (!is.null(df)){ 
     xv <- input$xaxisGrp 
     yv <- input$yaxisGrp 
     if (!is.null(xv) & !is.null(yv)){ 
      if (sum(xv %in% names(df))>0){ # supress error when changing files 
      mdf <- melt(df,id.vars=xv,measure.vars=yv) 
      gp <- ggplot(data=mdf) + 
       geom_point(aes_string(x=xv,y="value",color="variable"))+ 
       geom_smooth(method="lm")+ 
       theme(axis.text.x=element_text(angle=45, hjust=1))+ 
       theme_hc() + 
       scale_colour_hc()+theme(legend.title=element_blank()) 

      } 
     } 
     } 
     return(gp) 
} 

私はチャートを作成することができますが、私は

+geom_smooth(method="lm") 

を追加しようとすると、私が起こっかもしれないものLMラインに任意のアイデアを得ていないのですか?このようなデータセットが指定

dput(df) 
structure(list(load = c(1L, 18L, 36L, 72L, 108L, 144L, 216L), 
    throughput = c(64.9, 995.9, 1652.4, 1853.2, 1828.9, 1775, 
    1702.2)), .Names = c("load", "throughput"), class = "data.frame", row.names = c(NA, 
-7L)) 

は、私が実行しようとしました:

plot(xy~yv, data=df) 

私は何も表示されません。しかし、それをテストするために、私が次のことをすると、それは動作します。私は問題が何であるかを知ることができませんでした。ここでもまた、光沢のあるアプリにファイルをアップロードして、モデルをプロットして作成します。何か案は?

plot(mtcars$mpg~mtcars$cyl) ##this works 
+0

あなたが利用している場合'aes_string'では' x = "xv" 'をやっていませんか?他のことを確認するために、私は 'method =" auto "'は何の違いもなく、プロットは表示されないことを理解していますか?最後のことは、両方のギオムが同じ 'aes'を使用する場合、最初の' ggplot'呼び出しで 'aes_string'を提供すべきではないでしょうか? – Konrad

+0

私は、 '' ggplot(data = mdf、aes_string(x = "xv"、y = "value"、color = "variable"))+ geom_point()+ geom_smooth(method = "auto") '違いがあるかどうか確認してください。 – Konrad

+0

@Konrad、私は試しましたが、違いはありません、geom_smoothの仕事を得ることができません。 – user1471980

答えて

11

問題は軽微です。geom_smooth()はデータを参照していません。美学はの代わりにggplot()の中に普遍的に設定してください。 再現可能な例は、正しい位置にラインをカットアンドペーストするだけです。

まず、我々が光沢にロードするCSVファイルにmtcarsを書きます:

write.table(mtcars, "c://path//to//your//file.csv", row.names = TRUE, sep=",") 

第二に、このコードを実行します。

library(shiny); library(ggplot2); library(reshape2) 

shinyApp(

    ui = fluidPage(
    sidebarLayout(
     sidebarPanel(
     fileInput("inputFile", "Browse for file"), #Upload button 
     #After file is uploaded, read the columns in the server function, 
     # and create a responsive dropdown menu for plotting the variables 
     uiOutput("plot.params") #Responsive x and y dropdown menu 
    ), 
     mainPanel(
     plotOutput("plot") 
     ) 
    ) 
), 

    server = function(input, output, session) { 
    #Read in the uploaded data file and create a reactive variable called data_set 
    data_set <- reactive({if(is.null(input$inputFile)) return(NULL) 
     read.csv(input$inputFile$datapath, header = TRUE, sep=",") 
    }) 

    #Create a (reactive) dropdown menu for selecting X and Y 
    output$plot.params <- renderUI({ list(
     fluidRow(selectInput(inputId = "xaxisGrp", label = "X", choices = names(data_set()))), 
     fluidRow(selectInput(inputId = "yaxisGrp", label = "Y", choices = names(data_set()))) 
    )}) 

    #Create a plot- copied from OP with minor edit to ggplot() 
    output$plot = renderPlot(
     { 
     df <- data_set() 
     gp <- NULL 
     if (!is.null(df)){ 
      xv <- input$xaxisGrp #from the reactive ui selecInput 
      yv <- input$yaxisGrp #from the reactive ui selecInput 
      if (!is.null(xv) & !is.null(yv)){ 
      if (sum(xv %in% names(df))>0){ # supress error when changing files 
       mdf <- melt(df,id.vars=xv,measure.vars=yv) 
       gp <- ggplot(data=mdf, aes_string(x=xv,y="value",color="variable")) + 
       geom_point()+ #aes() moved from here into ggplot() 
       geom_smooth(method="lm") 
      } 
      } 
     } 
     return(gp) 
     } 
    ) 
    } 
) 

shiny fileInput with reactiveUI and geom_smooth

関連する問題