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

问题描述

我想在ggplot2中创建一个堆栈条形图,而Im则应用ggplotly()命令将其转换为交互式的绘图图形.

现在的问题是,我希望该图在悬停时显示取决于ITEM的每个值.

这是我的意思...

该图显示每个项目的累加值,例如,绿色条图例显示红色条(因为之前出现)加上绿色条值本身.

我希望每个图例根据项目显示其自身的值.

这是DF:

 >绘图数据Fecha Item Suma1 05/16 BAB 205402 05/16 BZO 10003 05/16 CLZ 14004 05/16 CMS 150005 07/16 BAB 400 

因此,绿色的条形将显示其自己的值1000,而不是显示21540(前一个20540和其自身的1000的总和).

如果您明白我的意思.有什么办法吗?

不想以图形方式显示累积值.

解决方案

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

 #他们在https://plot.ly/ggplot2/interactive-tooltip/上使用paste()技巧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 Suma1 05/16 BAB 205402 05/16 BZO 10003 05/16 CLZ 14004 05/16 CMS 150005 07/16 BAB 400,标头= TRUE) 

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.

Here is what I mean...

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.

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

Here is the 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

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?

Dont want plotly to display the cummulative values.

解决方案

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

Sample data:

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的小节中绘制自定义悬停文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:42