【发布时间】:2021-12-19 18:55:31
【问题描述】:
我必须设计一个图表,在使用 plotly 将变量添加到 Shiny R 时累积变量。
例如,如果我使用选择输入绘制变量 x 相对于日期 t 的图表,我添加变量并且它位于变量 x 的右侧,用分隔符指示它是变量 y因此选择了尽可能多的变量。
这是我的代码:
library(shiny)
library(plotly)
library(dplyr)
set.seed(123)
df <- data.frame(x = seq.Date(as.Date("2000/1/1"), by = "month", length.out = 100),
cat = sample(c("m1","m2","m3"),100, replace = TRUE),
a = cumsum(rnorm(100)),
b = rnorm(100),
c = rnorm(100),
d = rnorm(100))
ui <- fluidPage(
selectInput("x","Variable",names(df)[-1],NULL,TRUE),
selectInput("y", "category", unique(df$cat), NULL, TRUE),
numericInput("ls","limite superior",NULL,-100,100),
numericInput("li","limite superior",NULL,-100,100),
plotlyOutput("plot1")
)
server <- function(input, output, session) {
output$plot1 <- renderPlotly({
req(input$y, input$x)
df <- df%>%
filter(cat %in% input$y)%>%
select(one_of("x",input$x))
estado <- ifelse(df[[2]]>input$ls,"red",
ifelse(df[[2]]<input$ls & df[[2]]>input$li,
"orange","green"))
df$estado <- estado
p <- plot_ly(df,
x = ~x,
y = ~df[[2]],
type = "scatter",
mode = "lines")
## Makers
p <- p %>%
add_trace(x = ~x,
y= df[[2]],
marker = list(color = ~estado, size = 20, symbol = "square"),
showlegend = FALSE)
## Lengends and labels
p <- p %>%
layout(legend = list(orientation = 'h'))%>%
layout(title = paste('Comportamiento de calidad de agua residual', input$estacion, sep=' '),
plot_bgcolor = "#e5ecf6",
xaxis = list(title = 'Fecha'),
yaxis = list(title = paste(input$x,"mg/l", sep=" ")))
print(p)
})
}
shinyApp(ui, server)
我需要在添加变量 a、b、c、d 时,在已经存在的变量之后制作图形,使其看起来像这样:
【问题讨论】:
-
您可以使用 plotly 代理,阅读此article 17.3.1。这需要你还需要学习plotly js functions。可以在此处找到使用代理的示例:stackoverflow.com/questions/50620360/…
-
也许我没有让自己理解得很好,但我已经找到了解决方案,但是谢谢!
标签: r shiny plotly r-plotly shinyapps