I have this bar chart rendered using ggplot and ggplotly()
It renders just the way I want, in that it displays the SUM of total crashes by Mode Type and Year.
But, I am struggling with getting the (hover) tooltip to display the SUM of the Total Crashes. Right now, it just shows "1".
What do I need to do to my code to configure the tool tip to show the total crashes as shown in the chart, and not a value of 1? Here is my code, and the bar chart it produces:
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")
EDIT: Reproducible Code Example Here is code for replicating this issue.
crashsample <- data.frame(year = sample(2007:2018, 40, replace = T),
crashes = 1,
type = sample(c('Vehicle', 'Pedestrian','Bike','Motorcycle'), 5, replace = TRUE))
Create 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)