【问题标题】:Rename and Remove Plotly Hover information重命名和删除 Plotly Hover 信息
【发布时间】:2021-10-24 09:13:16
【问题描述】:

我目前正在探索 Shiny 中的 Plotly 输出,并且想知道是否有一种方法可以删除和重命名当您将鼠标悬停在每个观察值上时出现的信息。我在网站上看到了一些示例,但没有一个可以直接使用插入到 renderPlotly() 中的 ggplot 图表

就我而言,我正在尝试删除年份信息并将计数重命名为计数。有什么建议吗?

示例代码:

library(shiny)
library(tidyverse)
library(plotly)

A <- structure(list(Year = c(2020, 2021, 2021), Size = c(
  "L", "M",
  "S"
), count = c(83L, 93L, 216L)), row.names = c(NA, -3L), groups = structure(list(
  Year = c(2020, 2021), .rows = structure(list(1L, 2:3), ptype = integer(0), class = c(
    "vctrs_list_of",
    "vctrs_vctr", "list"
  ))
), row.names = c(NA, -2L), class = c(
  "tbl_df",
  "tbl", "data.frame"
), .drop = TRUE), class = c(
  "grouped_df",
  "tbl_df", "tbl", "data.frame"
))

ui <- fluidPage(
  titlePanel("Test Remove/Rename"),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      plotlyOutput(outputId = "test")
    )
  )
)

server <- function(input, output) {
  output$test <- renderPlotly({
    ggplot(A, aes(Year, count, fill = Size)) +
      geom_col()
  })
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    我会先重命名变量count,然后将其传递给ggplotly,相应地设置tooltip 参数。

    output$test <- renderPlotly({
        p <- A %>% 
          rename(Count = count) %>% 
          ggplot(aes(Year, Count, fill = Size)) +
          geom_col()
        p %>% ggplotly(tooltip = c('Count','Size'))
      })
    

    【讨论】:

    • 我之前尝试使用 ggplot 中的工具提示,但没有奏效。没有意识到我需要通过 ggplotly 来绘制它,感谢 Tom!
    猜你喜欢
    • 2013-06-27
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多