无法在ggplot2中绘制线性线

use*_*980 -3 r ggplot2 shiny

我正在将csv文件上传到一个闪亮的过滤器并尝试从所选列中绘制ggplot.

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)
}
Run Code Online (Sandbox Code Playgroud)

我可以创建图表但是当我尝试添加时

+geom_smooth(method="lm")
Run Code Online (Sandbox Code Playgroud)

我没有得到任何想法可能发生的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))
Run Code Online (Sandbox Code Playgroud)

我试着这样做:

plot(xy~yv, data=df)
Run Code Online (Sandbox Code Playgroud)

我什么都没看到.但是为了测试它,当我执行以下操作时,它可以工作.我无法找出问题所在.我再次将文件上传到闪亮的应用程序以绘制和创建模型.有任何想法吗?

plot(mtcars$mpg~mtcars$cyl) ##this works
Run Code Online (Sandbox Code Playgroud)

osh*_*hun 11

您的问题很小:geom_smooth()不引用任何数据.aes()普遍地将美学设置在内部ggplot()而不仅仅是内部geom_point().下面的可重复示例简单地将线切割并粘贴到正确的位置.

首先,我们将mtcars写入csv文件以加载到闪亮的:

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

其次,运行以下代码:

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)
      }
    )
  }
)
Run Code Online (Sandbox Code Playgroud)

带有reactiveUI和geom_smooth的闪亮fileInput