r - 如何在 ggplotly 中为条形图更改悬停背景颜色

标签 r ggplot2 shiny plotly shiny-server

我使用 ggplotly 绘制图形。当光标移动到图形顶部时,我使用工具提示函数来表示条形图中的值。

 Source_Data <-
 data.frame(
 key = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
 Product_Name = c(
  "Table",
  "Table",
  "Chair",
  "Table",
  "Bed",
  "Bed",
  "Sofa",
  "Chair",
  "Sofa"
 ),
 Product_desc = c("XX", "XXXX", "YY", "X", "Z", "ZZZ", "A", "Y", "A"),
 sd = c(0.1, 0.3, 0.4, 0.5, 0.6, 0.7, 0.7, 0.8, 0.5),
 Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)

 )


ggplotly((
Source_Data %>%
ggplot(
  aes(
    Product_Name,
    Cost,
    ymin = Cost - sd,
    ymax = Cost + sd,
    fill = Product_desc,
    text = paste("Product Name:", Product_Name, "<br>", "Cost:", Cost)
  )
  ) +
  geom_col(position = position_dodge2(width = .9, preserve = "single")) +
  geom_errorbar(position = position_dodge2(
  width = .9,
  preserve = "single",
  padding = .5
  )) +
  geom_text(
  aes(y = Cost + sd, label = Cost),
  position = position_dodge2(width = .9),
  vjust = -1
  ) +
  facet_wrap( ~ key, scales = "free_x", strip.position = "bottom") +
  theme(strip.placement = "outside") +
  theme_bw()
  ),
  tooltip = "text"
  )

当我将光标移到栏上时,我得到了文本值,这很好。当我将它移动到误差线的顶部时,我得到了不同颜色的文本值。有没有办法可以更改此悬停文本的背景颜色。

我对所有建议持开放态度。

最佳答案

您可以添加 %>% layout(hoverlabel=list(bgcolor="white"))在您的 ggplotly(...) 之后调用将悬停标签背景颜色设置为白色。

通常,您可以从 https://plot.ly/r/reference/ 设置任何 Plotly 布局属性。以这种方式在用 plot_ly() 创建的任何图形上或 ggplotly() .

关于r - 如何在 ggplotly 中为条形图更改悬停背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59959385/

相关文章:

r - 在 dplyr 中使用动态位置数创建滞后/超前变量

r - 是否可以使用 ggplot 在 map 上覆盖散点图?

r - 如何在 Shiny 的数据表中保存排序?

r - 根据列名中的匹配值和 R 中的特定列创建一个 0-1 数据框

Rmarkdown 不在公式中显示点(pdf 输出)

r - SAS 和 R 中的二项式检验 - 不同的结果

r - 如何在ggplot中为平滑线设置alpha

r - 在不规则网格上绘制轮廓

r - 将包含总计和百分比的行添加到 DT 数据表

r - Shiny 的观察者响应所有未选中的复选框