闪亮 - > Plotly->轮廓 - > EVENT_DATA()

时间:2017-07-17 22:00:53

标签: r shiny plotly shinydashboard

下面代表我正在努力的事情。底线和右线图表示主曲线沿其各自轴的投影。 Contour

当我放大主图时,我希望投影根据xmin,xmax,ymin和ymax进行缩放,并进一步投影主图显示的内容,而不是总是完全投影主图。

这可能吗?我尝试使用event_data(“plot_hover”)和event_data(“plot_selected”),但似乎都没有用。他们都提出了无效。

对于更简单的图表有很多文档,但对于轮廓图没有那么多,所以我想我会问这个。

这是我的代码的简化版本(老实说,非常粗糙):

ui.R

shinyUI(dashboardPage(
  dashboardHeader(title = "Oscillations in Spectroscopy"),
  dashboardSidebar(sidebarMenu(
    id = "mytabs",
    menuItem(
      "Data Input",
      tabName = "input",
      icon = icon("dashboard")
    )
  )),
  dashboardBody(tabItems(tabItem(
    "input",
    box(
      title = "Data Input",
      status = "warning",
      solidHeader = TRUE,
      width = "100%",
      height = "100%",
      fluidPage(fluidRow(
        column(7,
               plotlyOutput("raw"),
               plotlyOutput("raw.x")),
        column(2, width = 5,
               plotlyOutput("raw.y"))
      ))
    )
  )))
))

Server.r

shinyServer(function(input, output, session) {
  process.data <- reactive({
    #Do some back - end stuff
    return(df)
  })

  output$raw <- renderPlotly({
    print(.mainplot())
  })

  output$raw.x <- renderPlotly({
    .xplot()
  })

  output$raw.y <- renderPlotly({
    .yplot()
  })

  .mainplot <- function() {
    df <- data.frame(process.data())
    p <-
      plot_ly(
        df,
        x = ~ series,
        y = ~ time,
        z = ~ value,
        type = "contour",
        source = "A"
      ) %>% hide_colorbar()

    return (p)
  }

  .xplot <- function() {
    df <- data.frame(process.data())
    p <-
      plot_ly(
        df,
        x = ~ series,
        y = ~ value,
        type = 'scatter',
        mode = 'lines'
      ) %>%
      layout(xaxis = list(title = ""),
             yaxis = list(title = "", showticklabels = F))
    return(p)
  }
  .yplot <- function() {
    df <- data.frame(process.data())
    p <-
      plot_ly(
        df,
        x = ~ x,
        y = ~ y,
        type = 'scatter',
        mode = 'lines'
      ) %>%
      layout(xaxis = list(title = ""),
             yaxis = list(title = "", showticklabels = F))
    return (p)
  }

})

1 个答案:

答案 0 :(得分:0)

解决:

p <-
      plot_ly(x = df[,2], y = df[,4]) %>%
        add_contour(x = dat@wavelength, y = dat@data$x, z = dat@data$spc, visible = TRUE) %>%
        add_markers(x = df[,2], y = df[,4], opacity = 0.01, hoverinfo="x+y") %>%
        layout(dragmode = "select")