Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 向有光泽的图形添加多条无功几何图形线_R_Ggplot2_Shiny - Fatal编程技术网

R 向有光泽的图形添加多条无功几何图形线

R 向有光泽的图形添加多条无功几何图形线,r,ggplot2,shiny,R,Ggplot2,Shiny,我想将两个数据集的无功输出作为不同的geom_线包含在同一个ggplotly图形中。当只有一个无功data.frame作为geom_线包含时,代码按预期运行。为什么不是两个呢 ui <- fluidPage( sidebarLayout( selectInput("Var1", label = "Variable", #DATA CHOICE 1 selected = 10, choices = c(10:100)), selectIn

我想将两个数据集的无功输出作为不同的geom_线包含在同一个ggplotly图形中。当只有一个无功data.frame作为geom_线包含时,代码按预期运行。为什么不是两个呢

ui <- fluidPage(


   sidebarLayout(

  selectInput("Var1",
     label = "Variable", #DATA CHOICE 1
     selected = 10,
     choices = c(10:100)),

   selectInput("Var1",
               label = "Variable2", #DATA CHOICE 2
               selected = 10,
               choices = c(10:100))

      # Show a plot of the generated distribution

   ),
  mainPanel(
    plotlyOutput('plot') #Draw figure
  )
)


server <- function(input, output) {

  out <- reactive({

    data.frame(x = rnorm(input$Var1), #Build data set 1
               y  = 1:input$Var1)

  })

  out2 <- reactive({
    data.frame(x = rnorm(input$Var2), #Build data set 2
               y  = 1:input$Var2)
  })

  output$plot <- renderPlotly({
    p <- ggplot() +
      geom_line(data = out(), aes(x = x, y = y)) #Add both data sets in one ggplot
      geom_line(data = out2(), aes(x = x, y = y), color = "red")

    ggplotly(p)
  })

}

# Run the application 
shinyApp(ui = ui, server = server)

ui当您将数据转换为长格式并为每个组指定一个组标识符时,它似乎可以工作。请注意,您应该能够将
sliderInput
更改回
selectInput
——这是我在测试期间切换的条目之一,但UI小部件的选择应该无关紧要

这是可行的——代码可以从这里简化为反应式:

library(plotly)

  ui <- fluidPage(

  sidebarLayout(
     sliderInput("Var1",
            label = "Variable", #DATA CHOICE 1
            min=10, max=100, value=10),
     sliderInput("Var2",
            label = "Variable2", #DATA CHOICE 2
            min=10, max=100, value=10),
  ),
  mainPanel(
     plotlyOutput('plot') #Draw figure
  )
)

   server <- function(input, output) {

   out <- reactive({
      x1 <- rnorm(input$Var1)
      y1 <- seq(1:input$Var1)
      x2 <- rnorm(input$Var2)
      y2 <- seq(1:input$Var2)
      xx <- c(x1,x2)
      yy <- c(y1,y2)
      gg <- c( rep(1,length(y1)), rep(2,length(y2)) )
      df <- data.frame(cbind(xx,yy,gg))
      df
   })

   output$plot <- renderPlotly({
   p <- ggplot() +
       geom_line(data=out(), aes(x = xx, y = yy, group=gg, colour=gg))
   ggplotly(p)
   })

 }

shinyApp(ui = ui, server = server)
library(plotly)
用户界面