简体   繁体   English

在 shiny R 中使用 plotly 的多线图

[英]Multiple line graphs using plotly in shiny R

I'm new to shiny R and Plotly.我是 shiny R 和 Plotly 的新手。 I'm trying to build a dashboard that has two drop-down boxes and we take input through these dropdown boxes and plot Plotly graphs.我正在尝试构建一个具有两个下拉框的仪表板,我们通过这些下拉框和 plot Plotly 图表进行输入。 All the datasets have time, temp, and weight columns.所有数据集都有时间、温度和重量列。 time goes on the x-axis and for y-axis we can select either temp or weight or maybe both.时间在 x 轴上,对于 y 轴,我们可以 select 温度或重量或两者兼而有之。

  1. the first drop-down takes the input to which dataset to select.第一个下拉菜单将输入到 select 的数据集。

  2. second dropdown box takes the input to select the variable from the dataset selected.第二个下拉框将输入 select 来自所选数据集中的变量。 Most of the things I have figured out, however, y-axis label does not change dynamically.然而,我已经弄清楚的大多数事情,y 轴 label 不会动态变化。 the label is getting (input$variable) instead of temp or weight. label 正在获取(输入$变量)而不是温度或重量。

here is the shiny r output also here is the reproducible example and my code这是 shiny r output这里也是可重复的示例和我的代码

library(shiny)
library(plotly)
library(DT)

df1 <- data.frame("time" = 1:10, "temp" = c(21,15,31,12,23,45,67,34,54,10), "weight" = c(10,20,30,40,65,35,68,89,100,23), stringsAsFactors = FALSE)
df2 <- data.frame("time" = 1:10, "temp" = c(31,65,31,22,23,45,67,54,54,45), "weight" = c(30,20,40,40,65,85,68,89,14,24), stringsAsFactors = FALSE)





    ui <- fluidPage(

            titlePanel( div(column(width = 5, h2('title here')), )),
            # Input: Selector for choosing dataset
            selectInput(inputId = "dataset",
                        label = "Choose a dataset:",
                        choices = c("df1","df2")),

            selectInput(inputId = "variable",
                        label = "Variable selection", 
                        choices = c("temp","weight"),
                        selected = "weight",
                        multiple = FALSE),
            mainPanel(
                    # Output
                    tabsetPanel(type = "tabs",
                                tabPanel("Plot", plotlyOutput('plot')),
                                tabPanel("Data", DT::dataTableOutput("table")),
                                tabPanel("Key_metrics", DT::dataTableOutput("Key_metrics")))
            )
    )

    server <- function(input, output) {
            dataDf <- reactive({
                    temp <- get(input$dataset)

            })

            output$plot <- renderPlotly(
                    plot_ly(dataDf(), x = ~time, y =~get(input$variable), type = 'scatter', mode = 'lines', name = "temp") %>%
                            add_trace(dataDf(), x = ~time, y = ~weight, type = 'scatter', mode = 'lines',name = "weight") 

            )

            output$table <- DT::renderDataTable({
                    dataDf()
            })
            output$Key_metrics <- DT::renderDataTable({

            })

    }

    shinyApp(ui,server)

You can specify axis labels in layout() .您可以在layout()中指定轴标签。 Note that xaxis and yaxis require a list as argument (see here for more details):请注意, xaxisyaxis需要一个列表作为参数(有关详细信息,请参见此处):


output$plot <- renderPlotly(
    plot_ly(dataDf(), x = ~time, y =~get(input$variable), type = 'scatter', mode = 'lines', name = "temp") %>%
      add_trace(dataDf(), x = ~time, y = ~weight, type = 'scatter', mode = 'lines',name = "weight") %>%
      layout(xaxis = list(title = "Time"), yaxis = list(title = input$variable))

)

Edit: following a comment, here's how to plot two lines if two variables are selected and one otherwise (don't forget to put multiple = TRUE in selectInput() :编辑:在评论之后,这里是如何 plot 如果选择了两个变量,则为两行,否则为另一行(不要忘记在selectInput()中放置multiple = TRUE

library(shiny)
library(plotly)
library(DT)

df1 <- data.frame("time" = 1:10, "temp" = c(21,15,31,12,23,45,67,34,54,10), "weight" = c(10,20,30,40,65,35,68,89,100,23), stringsAsFactors = FALSE)
df2 <- data.frame("time" = 1:10, "temp" = c(31,65,31,22,23,45,67,54,54,45), "weight" = c(30,20,40,40,65,85,68,89,14,24), stringsAsFactors = FALSE)



ui <- fluidPage(

  titlePanel( div(column(width = 5, h2('title here')), )),
  # Input: Selector for choosing dataset
  selectInput(inputId = "dataset",
              label = "Choose a dataset:",
              choices = c("df1","df2")),

  selectInput(inputId = "variable",
              label = "Variable selection", 
              choices = c("temp","weight"),
              selected = "weight",
              multiple = TRUE),
  mainPanel(
    # Output
    tabsetPanel(type = "tabs",
                tabPanel("Plot", plotlyOutput('plot')),
                tabPanel("Data", DT::dataTableOutput("table")),
                tabPanel("Key_metrics", DT::dataTableOutput("Key_metrics")))
  )
)

server <- function(input, output) {
  dataDf <- reactive({
    temp <- get(input$dataset)

  })

  output$plot <- renderPlotly({

    if (length(input$variable) > 1){
      plot_ly(dataDf(), x = ~time, y =~get(input$variable[1]), 
              type = 'scatter', mode = 'lines', name = "temp") %>%
        add_trace(dataDf(), x = ~time, y = ~get(input$variable[2]), 
                  type = 'scatter', mode = 'lines',name = "weight") %>%
        layout(xaxis = list(title = "Time"))
    }
    else {
      plot_ly(dataDf(), x = ~time, y =~get(input$variable[1]), type = 'scatter', mode = 'lines', name = "temp") %>%
        add_trace(dataDf(), x = ~time, y = ~get(input$variable[1]), type = 'scatter', mode = 'lines',name = "weight") %>%
        layout(xaxis = list(title = "Time"), yaxis = list(title = input$variable))
    }

  })

  output$table <- DT::renderDataTable({
    dataDf()
  })
  output$Key_metrics <- DT::renderDataTable({

  })

}

shinyApp(ui,server)

Put what you want as y-axis label based on the original answer.根据原始答案将您想要的内容作为 y 轴 label 。 Note that this answer only works if there are two choices.请注意,此答案仅在有两种选择时才有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM