如何设置不同的文本和悬停信息文本 [英] How to set different text and hoverinfo text

查看:39
本文介绍了如何设置不同的文本和悬停信息文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 plotly 包,但找不到在图表本身和 hoverinfo 中显示不同内容的方法.下面是一个条形图的例子:

图书馆(情节)图书馆(dplyr)数据(虹膜)df <-虹膜%>%group_by(物种)%>%总结(n = n(),平均=平均值(萼片长度))p1 <- plot_ly(data = df,x = ~物种,y = ~n,类型=酒吧",text = ~paste(物种:",物种,<br>平均 :", 平均),textposition = 自动",悬停信息 =文本")

从这段代码我得到了这个:我想在每个栏中显示频率 (n) 值,而不是与 hoverinfo 相同的内容.

我一直在看

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.

解决方案

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))

这篇关于如何设置不同的文本和悬停信息文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆