将鼠标悬停在 Plotly 图上时,有没有办法添加更多信息?

Is there a way to add more information when hovering over a Plotly graph?

我做了一个图表,你可以在下面看到。将鼠标悬停在 Plotly 图表上时,您可以看到图表中存在的有用信息,例如 Store Num 等。有没有办法添加到此标签,所以当有人悬停时他们也可以看到 branding ?如,不只是 Store Num?谢谢!

Store Num mean_sales mean_outreach branding
1 200 1200 1
2 4200 1403 2
plotly::ggplotly(ggplot(data, aes(x= mean_sales, y= mean_outreach, label= `Store Num`))+
                   geom_point() +geom_text(aes(label= `Store Num`),hjust=20, vjust=20) +
                   ggtitle("Examining Marketing Campaign Outreach"))

当我尝试写两个标签时,标签本身会发生变化,但 Store Numbranding 的值都是商店编号。因为我想要的两个标签都没有正确显示。

plotly::ggplotly(ggplot(data, aes(x= mean_sales, y= mean_outreach, label= `Store Num`))+
                   geom_point() +geom_text(aes(label= branding),hjust=20, vjust=20) +
                   ggtitle("Examining Marketing Campaign Outreach"))

您可以通过 text 美学(参见 https://plotly-r.com/controlling-tooltips.html#tooltip-text-ggplotly)的自定义工具提示实现您想要的结果:

library(plotly)

ggplot(data, aes(
  x = mean_sales, y = mean_outreach, label = Store.Num,
  text = paste0(
    "mean_sales: ", mean_sales, "<br>",
    "mean_outreach: ", mean_outreach, "<br>",
    "Store Num: ", Store.Num, "<br>",
    "branding: ", branding
  )
)) +
  geom_point() +
  geom_text(hjust = 0, vjust = 0) +
  ggtitle("Examining Marketing Campaign Outreach")

ggplotly(tooltip = "text")

或者,如果您只想将信息添加到默认值,您可以这样做:

ggplot(data, aes(
  x = mean_sales, y = mean_outreach, label = Store.Num,
  text = paste0(
    "branding: ", branding
  )
)) +
  geom_point() +
  geom_text(hjust = 0, vjust = 0) +
  ggtitle("Examining Marketing Campaign Outreach")

ggplotly(tooltip = "all")