在 R 的条形图中绘制自定义悬停文本 [英] Plotly custom hover text in barplot from R

查看:26
本文介绍了在 R 的条形图中绘制自定义悬停文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 ggplot2 中制作一个堆栈条形图,并应用 ggplotly() 命令将其转换为交互式绘图图形.

I want have a stack barplot made in ggplot2 and Im applying the ggplotly() command to transform it into a interactive plotly graphic.

现在的问题是,我希望图表在悬停时根据项目显示每个值.

The issue now is that I want the plot to display on hover each value depending on the ITEM.

这就是我的意思...

该图显示了每个项目的累积值,例如,绿色条图例显示红色条(因为它在前面)加上绿色条值本身.

The plot is displaying a cumulative value for each of the items, so for example the green bar legend is displaying the red bar(because it comes before) PLUS the green bar value itself.

我希望每个图例根据 ITEM 显示它们自己的值.

I want each legend to display their own value based on the ITEM.

这里是 DF:

> plotlydata
  Fecha Item  Suma
1 05/16  BAB 20540
2 05/16  BZO  1000
3 05/16  CLZ  1400
4 05/16  CMS 15000
5 07/16  BAB   400

所以绿色条不会显示 21540(之前的 20540 和自己的 1000 的总和),而是显示自己的值 1000.

So green bar instead of displaying 21540 (sum of previous 20540 and the own 1000) would display its own value of 1000.

如果你明白我的意思.有没有办法做到这一点?

If you get what I mean. Is there any way of doing this?

不想情节显示累积值.

推荐答案

您可以通过添加文本美学并将其包含在工具提示参数中来添加该信息:

You can add that information by adding a text aesthetic and including it in the tooltip argument:

# they use the paste() trick on https://plot.ly/ggplot2/interactive-tooltip/
ggplot(d, aes(y = Suma, x = Fecha, fill = Item, text = paste("Suma:", Suma))) + 
  geom_bar(stat = "identity")

ggplotly(tooltip = c("text", "x", "fill"))

样本数据:

d <- read.table(text ="   Fecha Item  Suma
1 05/16  BAB 20540
2 05/16  BZO  1000
3 05/16  CLZ  1400
4 05/16  CMS 15000
5 07/16  BAB   400", header = TRUE)

这篇关于在 R 的条形图中绘制自定义悬停文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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