-3

I am uploading csv file into a via shiny and trying to draw ggplot from the selected columns.

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)
}

I can create the chart but when I try to add

+geom_smooth(method="lm")

I am not getting the lm line any ideas what might be happening?

given a data set like this:

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))

I tried to do:

plot(xy~yv, data=df)

I don't see anything. But to test it, when I do the following, it works. I was not able to find out what the problem is. Again, I am uploading a file to shiny app to plot and create models. Any ideas?

plot(mtcars$mpg~mtcars$cyl) ##this works
Axeman
  • 32,068
  • 8
  • 81
  • 94
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • If you are making use of the `aes_string` wouldn't you rather do `x="xv"`? And just to confirm other thing, I understand that `method="auto"` makes no difference, the plot does not show? The last thing, if both geoms use the same `aes` shouldn't `aes_string` be provided in the initial `ggplot` call? – Konrad Mar 31 '16 at 15:40
  • I would try: `ggplot(data=mdf, aes_string(x="xv",y="value",color="variable")) + geom_point() + geom_smooth(method="auto")`, just to check if it makes the difference – Konrad Mar 31 '16 at 15:44
  • @Konrad, I've tried, no difference, cannot get the geom_smooth working – user1471980 Mar 31 '16 at 15:48
  • How about the second change to *aes*? – Konrad Mar 31 '16 at 15:52
  • @Konrad, changed x="xv", no difference. It seems like, when I import the data, all columns comes in as factors. Is there a way to convert to values to numeric when doing geom_smooth only? – user1471980 Mar 31 '16 at 15:55
  • I was wondering whether you moved the *aes* call to the initial *ggplot* outside from within the *geom_point*. You can apply transformations to variables within the geom call: *geom_line(aes(x = function(var)))*. – Konrad Mar 31 '16 at 16:02
  • @Konrad I get this error: non-numeric argument to binary operator – user1471980 Mar 31 '16 at 16:06
  • This explains a lot, could you post a data extract and the corresponding plot call? The Shiny app stuff is not relevant to this error at the moment. – Konrad Mar 31 '16 at 16:15
  • @Konrad, I am using the mtcars data set. In this case df is the mtcars and in the original post, I've posted how I am trying to create the ggplot etc. – user1471980 Mar 31 '16 at 16:25
  • The error you are seeing means that one of the indicators you are trying to chart is no-numeric. It's a common error that folk experience when trying to chart strings or things that shouldn't be charted. If you want to post the whole app as a [single file](http://shiny.rstudio.com/articles/single-file.html) it will be easier to have a look and suggest fixes. – Konrad Apr 01 '16 at 08:29
  • @Konrad, would you be able to chat? I am inserting an external csv file into shiny and based on columns, I'm creating ggplot. I think when I import the data, all columns are becomes factors. I need to convert it to numeric. – user1471980 Apr 01 '16 at 13:32
  • You can easily [avoid that](http://stackoverflow.com/a/5187757/1655567). Broadly speaking, there is no problem with plotting various charts using *mtcars* data but there is no way to help you as the question is incomplete, the example is not reproducible. – Konrad Apr 01 '16 at 15:14
  • 2
    Please, insert `message(str(mdf))` just before the line with `ggplot` and see what is printed on the console. – Uwe Apr 04 '16 at 07:03
  • 'data.frame': 7 obs. of 2 variables: $ load : num 1 18 36 72 108 144 216 $ throughput: num 64.9 995.9 1652.4 1853.2 1828.9 ... – user1471980 Apr 05 '16 at 16:44

1 Answers1

11

Your problem is minor: geom_smooth() does not reference any data. Set the aesthetics aes() universally inside ggplot() instead of just in geom_point(). The reproducible example below simply cut-and-pastes the line to the correct location.

First, we'll write mtcars into a csv file to load into shiny:

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

Second, run this code:

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

oshun
  • 2,319
  • 18
  • 32