在闪亮的 R 中使用 plotly 的多线图
Multiple line graphs using plotly in shiny R
我是 shiny R 和 Plotly 的新手。我正在尝试构建一个具有两个下拉框的仪表板,我们通过这些下拉框获取输入并绘制 Plotly 图表。所有数据集都有时间、温度和权重列。时间在 x 轴上,对于 y 轴,我们可以 select 温度或重量或两者兼而有之。
第一个下拉菜单将输入数据集 select。
第二个下拉框将输入 select 来自数据集 selected 的变量。
然而,我已经弄清楚的大部分事情,y 轴标签不会动态变化。标签正在获取 (input$variable) 而不是温度或重量。
here is the 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)
您可以在 layout()
中指定轴标签。请注意,xaxis
和 yaxis
需要一个列表作为参数(有关详细信息,请参阅 here):
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))
)
编辑: 在评论之后,这里是如何绘制两条线,如果选择了两个变量,否则绘制一条线(不要忘记将 multiple = TRUE
放在 selectInput()
:
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)
根据原答案把你想要的作为y轴标签。请注意,此答案仅在有两种选择时才有效。
我是 shiny R 和 Plotly 的新手。我正在尝试构建一个具有两个下拉框的仪表板,我们通过这些下拉框获取输入并绘制 Plotly 图表。所有数据集都有时间、温度和权重列。时间在 x 轴上,对于 y 轴,我们可以 select 温度或重量或两者兼而有之。
第一个下拉菜单将输入数据集 select。
第二个下拉框将输入 select 来自数据集 selected 的变量。 然而,我已经弄清楚的大部分事情,y 轴标签不会动态变化。标签正在获取 (input$variable) 而不是温度或重量。
here is the 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)
您可以在 layout()
中指定轴标签。请注意,xaxis
和 yaxis
需要一个列表作为参数(有关详细信息,请参阅 here):
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))
)
编辑: 在评论之后,这里是如何绘制两条线,如果选择了两个变量,否则绘制一条线(不要忘记将 multiple = TRUE
放在 selectInput()
:
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)
根据原答案把你想要的作为y轴标签。请注意,此答案仅在有两种选择时才有效。