如何在ggplotly中更改工具提示以在条形图中显示列的总和

问题描述

我使用ggplot和ggplotly()绘制了此条形图

它以我想要的方式呈现,它按模式类型和年份显示总崩溃的总和。

但是,我正在努力(悬停)工具提示来显示总崩溃数的总和。现在,它只显示“ 1”。

我需要对我的代码做些什么,以配置工具提示以显示如图所示的总崩溃次数,而不是值1?这是我的代码,以及它产生的条形图:

crashplot <- ggplot(totcrashes_by_year,aes(totcrashes_by_year$`Crash Year`,totcrashes_by_year$`Total Crashes`))
crashplot +geom_bar(stat = "identity",aes(fill=totcrashes_by_year$`Mode Type`),position = "stack") + xlab("Crash Year") + ylab("Total Crashes") + ggtitle("Total Crashes by Year and Mode Type") + labs(fill = "Mode Type")

ggplotly tool tip - How do I show the sum of the crashes for each year?

编辑:可复制代码示例 这是用于复制此问题的代码。

crashsample <- data.frame(year = sample(2007:2018,40,replace = T),crashes = 1,type = sample(c('Vehicle','Pedestrian','Bike','Motorcycle'),5,replace = TRUE))

创建ggplot

crashplot <- ggplot(crashsample,aes(x =  year,y = crashes,fill = type)) +
  geom_bar(stat = "identity") + 
  labs(x = 'Crash Year',y = 'Total Crashes',title = "Total Crashes by Year and Mode Type",fill = "Mode Type")
#call to ggplotly to render as a dynamic plotly chart
ggplotly(crashplot)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)