Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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_Shiny_Plotly_R Plotly - Fatal编程技术网

R 我可以吗;配对;在闪亮的绘图中显示轨迹,以便在单击图例时显示/消失两条轨迹?

R 我可以吗;配对;在闪亮的绘图中显示轨迹,以便在单击图例时显示/消失两条轨迹?,r,shiny,plotly,r-plotly,R,Shiny,Plotly,R Plotly,我正在创建一个应用程序,其中有几个变量的区域数据。该应用程序允许您通过selectInput选择用户想要可视化的区域。出于比较/信息目的,我希望用户在绘图中可视化所选区域以及全国平均值 但是,我希望plot\u ly图例的行为就像区域数据与国家比较“配对”——即,如果单击区域Var1跟踪,国家比较也应该消失 有没有一种方法可以在Shiny/plotly中实现这一点?老实说,除了检查设置“doubleclickedLegendItem”和clickedLegendItem之外,我没有发现任何东西,

我正在创建一个应用程序,其中有几个变量的区域数据。该应用程序允许您通过selectInput选择用户想要可视化的区域。出于比较/信息目的,我希望用户在
绘图中可视化所选区域以及全国平均值

但是,我希望
plot\u ly
图例的行为就像区域数据与国家比较“配对”——即,如果单击区域Var1跟踪,国家比较也应该消失

有没有一种方法可以在Shiny/plotly中实现这一点?老实说,除了检查设置“doubleclickedLegendItem”和
clickedLegendItem
之外,我没有发现任何东西,但我不知道如何配对跟踪,然后复制典型的图例行为

或者,如果这是不可能的,那么只有在区域轨迹上悬停时,才可能显示国家比较轨迹吗?这也是一个可以接受的解决办法

下面是我的应用程序的一个非常简单的示例:

(目前正在这里使用完整的应用程序,第二个选项卡:)

库(闪亮)
图书馆(绘本)
图书馆(dplyr)
种子集(12345)

df为了以防万一,我在这里找到了答案:是的,可以使用legendgroup选项:

library(shiny)
library(plotly)
library(dplyr)

set.seed(12345)

df <-  data.frame(
  Region = c(rep("National", 3),
             rep("Region1", 3),
             rep("Region2", 3)),
  
  Year   = c(rep(2018:2020,3)),
  
  Var1   = c(rnorm(3),
             rnorm(3,1),
             rnorm(3,2)),
  
  Var2   = c(rnorm(3),
             rnorm(3,3),
             rnorm(3,5))
)

linecolors <- c("blue", "green")

ui = fluidPage(
  
  selectInput("region_sel",
              label   = "Select a region:",
              choices = c("Region1", "Region2")
  ),
  
  plotlyOutput("plot")
  
  
  
)

server =  function(input, output, session) {
  
  subset_data_region <- reactive({
    temp_df <- df[df$Region==input$region_sel,]
    return(temp_df)
    
  })
  
  subset_data_nat <- df[df$Region=="National",]
  
  output$plot = renderPlotly({
    
    plot_ly(type = 'scatter',
            mode = 'lines+markers') %>% 
      #regional traces
      add_trace(data = subset_data_region(),
                y = ~Var1,
                x = ~Year,
                name = "Var1",
                line = list(color = linecolors[1]),
                marker = list(color = linecolors[1]),
                legendgroup="group1"
      ) %>%
      add_trace(data = subset_data_region(),
                y = ~Var2,
                x = ~Year,
                name = "Var2",
                line = list(color = linecolors[2]),
                marker = list(color = linecolors[2]),
                legendgroup="group2"
      ) %>%
      #national traces for comparison
      add_trace(data = subset_data_nat,
                y = ~Var1,
                x = ~Year,
                name = "Var1",
                line = list(color = linecolors[1],
                            dash = "dash"),
                marker = list(color = linecolors[1]),
                legendgroup="group1",
                showlegend = F
      ) %>%
      add_trace(data = subset_data_nat,
                y = ~Var2,
                x = ~Year,
                name = "Var2",
                line = list(color = linecolors[2],
                            dash = "dash"),
                marker = list(color = linecolors[2]),
                legendgroup="group2",
                showlegend = F
      )
    
  })
  
  
}

shinyApp(ui = ui, server = server)
库(闪亮)
图书馆(绘本)
图书馆(dplyr)
种子集(12345)
df
library(shiny)
library(plotly)
library(dplyr)

set.seed(12345)

df <-  data.frame(
  Region = c(rep("National", 3),
             rep("Region1", 3),
             rep("Region2", 3)),
  
  Year   = c(rep(2018:2020,3)),
  
  Var1   = c(rnorm(3),
             rnorm(3,1),
             rnorm(3,2)),
  
  Var2   = c(rnorm(3),
             rnorm(3,3),
             rnorm(3,5))
)

linecolors <- c("blue", "green")

ui = fluidPage(
  
  selectInput("region_sel",
              label   = "Select a region:",
              choices = c("Region1", "Region2")
  ),
  
  plotlyOutput("plot")
  
  
  
)

server =  function(input, output, session) {
  
  subset_data_region <- reactive({
    temp_df <- df[df$Region==input$region_sel,]
    return(temp_df)
    
  })
  
  subset_data_nat <- df[df$Region=="National",]
  
  output$plot = renderPlotly({
    
    plot_ly(type = 'scatter',
            mode = 'lines+markers') %>% 
      #regional traces
      add_trace(data = subset_data_region(),
                y = ~Var1,
                x = ~Year,
                name = "Var1",
                line = list(color = linecolors[1]),
                marker = list(color = linecolors[1]),
                legendgroup="group1"
      ) %>%
      add_trace(data = subset_data_region(),
                y = ~Var2,
                x = ~Year,
                name = "Var2",
                line = list(color = linecolors[2]),
                marker = list(color = linecolors[2]),
                legendgroup="group2"
      ) %>%
      #national traces for comparison
      add_trace(data = subset_data_nat,
                y = ~Var1,
                x = ~Year,
                name = "Var1",
                line = list(color = linecolors[1],
                            dash = "dash"),
                marker = list(color = linecolors[1]),
                legendgroup="group1",
                showlegend = F
      ) %>%
      add_trace(data = subset_data_nat,
                y = ~Var2,
                x = ~Year,
                name = "Var2",
                line = list(color = linecolors[2],
                            dash = "dash"),
                marker = list(color = linecolors[2]),
                legendgroup="group2",
                showlegend = F
      )
    
  })
  
  
}

shinyApp(ui = ui, server = server)