繁体   English   中英

当已经具有 event_data("plotly_click") 时,如何使用 r、plolty、shin 为条形图中单击的条着色

[英]How to color a clicked bar from barchart with r, plolty, shiny when having already event_data("plotly_click")

我试图通过使用 r plotly 和 Shiny重新创建基于交互式网络的数据可视化的示例来了解event_data()如何工作,章节链接视图与闪亮https : //plotly-r.com/linking-views-with-shiny .html#fig:plotlyEvents以便我可以为已选择的栏着色。 首先,当我运行代码时,我得到:

“警告:与'sub_category'的源ID绑定的'plotly_click'事件未注册。为了获取此事件数据,请将event_register(p, 'plotly_click')到您希望获取 event 的 plot ( p )数据来自。警告:与 'order_date' 源 ID 相关的 'plotly_click' 事件未注册。为了获取此事件数据,请将event_register(p, 'plotly_click')到您希望的绘图 ( p )从中获取事件数据。警告:与“sub_category”的源 ID 绑定的“plotly_click”事件未注册。为了获取此事件数据,请将event_register(p, 'plotly_click')到您的绘图 ( p ) 中希望从中获取事件数据。警告:与 'order_date' 源 ID 绑定的 'plotly_click' 事件未注册。为了获取此事件数据,请将event_register(p, 'plotly_click')到绘图 ( p )您希望从中获取事件数据。”

然后我读到event_register()我正在尝试修改代码,但除了破坏它之外,我没有取得太多成就。 我还尝试使用highlight()为点击的栏着色,但我想我在这个例子中没有正确使用它,因为代码再次中断。 请您给我一些启示,让我了解如何为选定的条形和子类别着色以具有相同的颜色。 非常感谢您的宝贵时间!

在此处输入图片说明

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


  sales <- diamonds
  sales$category = sales$cut
  sales$sub_category = sales$color
  sales$sales = sales$price
  sales$order_date = sample(seq(as.Date('2020-01-01'), as.Date('2020-02-01'), by="day"),nrow(sales), replace = T)



  ui <- fluidPage(
    plotlyOutput("category", height = 200),
    plotlyOutput("sub_category", height = 200),
    plotlyOutput("sales", height = 300),
    DT::dataTableOutput("datatable")
  )

  # avoid repeating this code
  axis_titles <- . %>%
    layout(
      xaxis = list(title = ""),
      yaxis = list(title = "Sales")
    )

  server <- function(input, output, session) {

    # for maintaining the state of drill-down variables
    category <- reactiveVal()
    sub_category <- reactiveVal()
    order_date <- reactiveVal()

    # when clicking on a category, 
    observeEvent(event_data("plotly_click", source = "category"), {
      category(event_data("plotly_click", source = "category")$x)
      sub_category(NULL)
      order_date(NULL)
    })

    observeEvent(event_data("plotly_click", source = "sub_category"), {
      sub_category(
        event_data("plotly_click", source = "sub_category")$x
      )
      order_date(NULL)
    })

    observeEvent(event_data("plotly_click", source = "order_date"), {
      order_date(event_data("plotly_click", source = "order_date")$x)
    })

    output$category <- renderPlotly({
      sales %>%
        count(category, wt = sales) %>%
        plot_ly(x = ~category, y = ~n, source = "category") %>%
        axis_titles() %>% 
        layout(title = "Sales by category")
    })

    output$sub_category <- renderPlotly({
      if (is.null(category())) return(NULL)

      sales %>%
        filter(category %in% category()) %>%
        count(sub_category, wt = sales) %>%
        plot_ly(x = ~sub_category, y = ~n, source = "sub_category") %>%
        axis_titles() %>%
        layout(title = category())
    })

    output$sales <- renderPlotly({
      if (is.null(sub_category())) return(NULL)

      sales %>%
        filter(sub_category %in% sub_category()) %>%
        count(order_date, wt = sales) %>%
        plot_ly(x = ~order_date, y = ~n, source = "order_date") %>%
        add_lines() %>%
        axis_titles() %>%
        layout(title = paste(sub_category(), "sales over time"))
    })

    output$datatable <-  DT::renderDataTable({
      if (is.null(order_date())) return(NULL)

      sales %>%
        filter(
          sub_category %in% sub_category(),
          as.Date(order_date) %in% as.Date(order_date())
        )
    })

  }

  shinyApp(ui, server)

伙计,我只是根据您点击的内容添加了颜色。

默认情况下,线图是绿色的,所以我们不需要担心。
对于第一个图,如果单击 category(),我将改变红色。 由于某种原因,我无法直接改变它,所以我在绘图之前创建了一个 plot_data 并且有 if else 语句来这样做(嵌套 if_else 不起作用)
对于第二个图,如果单击 sub_category(),我将改变绿色。

希望这就是你要找的!

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


sales <- diamonds
sales$category = sales$cut
sales$sub_category = sales$color
sales$sales = sales$price
sales$order_date = sample(seq(as.Date('2020-01-01'), as.Date('2020-02-01'), by="day"),nrow(sales), replace = T)



ui <- fluidPage(
  plotlyOutput("category", height = 200),
  plotlyOutput("sub_category", height = 200),
  plotlyOutput("sales", height = 300),
  DT::dataTableOutput("datatable")
)

# avoid repeating this code
axis_titles <- . %>%
  layout(
    xaxis = list(title = ""),
    yaxis = list(title = "Sales")
  )

server <- function(input, output, session) {

  # for maintaining the state of drill-down variables
  category <- reactiveVal()
  sub_category <- reactiveVal()
  order_date <- reactiveVal()

  # when clicking on a category, 
  observeEvent(event_data("plotly_click", source = "category"), {
    category(event_data("plotly_click", source = "category")$x)
    sub_category(NULL)
    order_date(NULL)
  })

  observeEvent(event_data("plotly_click", source = "sub_category"), {
    sub_category(
      event_data("plotly_click", source = "sub_category")$x
    )
    order_date(NULL)
  })

  observeEvent(event_data("plotly_click", source = "order_date"), {
    order_date(event_data("plotly_click", source = "order_date")$x)
  })

  output$category <- renderPlotly({
    print(category())
    if (is.null(category())) {
      plot_data <- sales %>%
        count(category, wt = sales) %>%
        mutate(current_color = "blue")
    } else {
      plot_data <- sales %>%
        count(category, wt = sales) %>%
        mutate(current_color = if_else(category %in% category(), "red", "blue"))
    }
      plot_ly(
        plot_data, x = ~category, y = ~n, source = "category", type = "bar",
              marker = list(color = ~current_color)
      ) %>%
      axis_titles() %>% 
      layout(title = "Sales by category")
  })

  output$sub_category <- renderPlotly({
    if (is.null(category())) return(NULL)
    sales %>%
      filter(category %in% category()) %>%
      count(sub_category, wt = sales) %>%
      mutate(current_color = if_else(sub_category %in% sub_category(), "green", "red")) %>%
      plot_ly(
        x = ~sub_category, y = ~n, source = "sub_category", type = "bar",
        marker = list(color = ~current_color)
      ) %>%
      axis_titles() %>%
      layout(title = category())
  })

  output$sales <- renderPlotly({
    if (is.null(sub_category())) return(NULL)
    sales %>%
      filter(sub_category %in% sub_category()) %>%
      count(order_date, wt = sales) %>%
      plot_ly(x = ~order_date, y = ~n, source = "order_date", line = list(color = "green")) %>%
      add_lines() %>%
      axis_titles() %>%
      layout(title = paste(sub_category(), "sales over time"))
  })

  output$datatable <-  DT::renderDataTable({
    if (is.null(order_date())) return(NULL)

    sales %>%
      filter(
        sub_category %in% sub_category(),
        as.Date(order_date) %in% as.Date(order_date())
      )
  })

}

shinyApp(ui, server)

在此处输入图片说明

暂无
暂无

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

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