How to set different text and hoverinfo text

*爱你&永不变心* 提交于 2021-02-07 06:19:04

问题


I am working with the plotly package, and I cannot find a way to display different things on the chart itself and in the hoverinfo. Here is an example of a barchart:

library(plotly)
library(dplyr)

data(iris)

df <- iris %>%
  group_by(Species) %>%
  summarise(n = n(),
            avg = mean(Sepal.Length))

p1 <- plot_ly(data = df,
             x = ~Species,
             y = ~n,
             type = "bar",
             text = ~paste("Species :", Species,
                           "<br> Avg :", avg),
             textposition = "auto",
             hoverinfo = "text")

From this code I get this: And I would like to display the frequency (n) value in each bar instead of the same thing as the hoverinfo.

I have been looking at this thread but the solution described is too complicated for me and I think there must be an easier way to solve this issue.


回答1:


Something like this?

p1 <- plot_ly(data = df,
              x = ~Species,
              y = ~n,
              type = "bar",
              text = ~n,
              textposition = "auto",
              hoverinfo = "text",
              hovertext = paste("Species :", df$Species,
                                "<br> Avg :", df$avg))



来源:https://stackoverflow.com/questions/49901771/how-to-set-different-text-and-hoverinfo-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!