Reputation: 749
For the data with random values below:
structure(list(ActualDateCreated = structure(c(17116, 17118,
17118, 17118, 17119, 17119, 17119, 17119, 17119, 17119, 17119,
17119, 17120, 17120, 17120, 17120, 17120, 17120, 17120, 17120,
17120, 17120, 17121, 17121, 17121, 17121, 17121, 17121, 17121,
17121, 17121, 17121, 17122, 17122, 17122, 17122, 17122, 17122,
17122, 17122, 17122, 17123, 17123, 17123, 17123, 17123, 17123,
17123, 17123, 17124), class = "Date"), Business.Sub.Area = c("Team 1",
"Team 2", "Team 3", "Team 4", "Team 1", "Team 11", "Team 2",
"Team 3", "Team 4", "Team 5", "Team 8", "Team 9", "Team 1", "Team 10",
"Team 11", "Team 2", "Team 3", "Team 4", "Team 5", "Team 7",
"Team 8", "Team 9", "Team 1", "Team 10", "Team 11", "Team 2",
"Team 3", "Team 4", "Team 5", "Team 7", "Team 8", "Team 9", "Team 1",
"Team 10", "Team 11", "Team 2", "Team 3", "Team 4", "Team 5",
"Team 8", "Team 9", "Team 1", "Team 11", "Team 3", "Team 4",
"Team 5", "Team 6", "Team 7", "Team 9", "Team 1"), n = c(80L,
86L, 32L, 32L, 67L, 31L, 54L, 94L, 92L, 44L, 50L, 47L, 38L, 76L,
75L, 60L, 55L, 78L, 89L, 70L, 34L, 76L, 38L, 93L, 84L, 13L, 90L,
95L, 62L, 7L, 70L, 23L, 67L, 32L, 12L, 66L, 8L, 20L, 30L, 79L,
14L, 73L, 89L, 4L, 23L, 15L, 31L, 27L, 28L, 2L)), .Names =
c("ActualDateCreated",
"Business.Sub.Area", "n"), row.names = c(NA, -50L), class = c("tbl_df",
"tbl", "data.frame"))
I have the following plot:
plot_ly(dailycct[dailycct$ActualDateCreated >= "2017-11-01",],
type = 'bar',
x = ~ActualDateCreated,
y = ~n,
color = ~Business.Sub.Area,
text = ~paste('Date: ', ActualDateCreated, '\n',
'Team: ', Business.Sub.Area, '\n',
'Count: ', n, '\n',
'Total: ', sum(n),'\n'),
hoverinfo = 'text'
) %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
As you can see the 'Total' section in the hover text is the total of the entire dataset rather than the total for the date the cursorr is hovering over only. Is there a way to get the total on n for the date that the cursor is hovering over only?
Upvotes: 0
Views: 2551
Reputation: 7704
One way to do is add a column in the dataset such that it gives an additional column with totals in each date. You can do this by using dplyr
. Then using this new column in the plot, something like this:
library(dplyr)
library(plotly)
df <- dailycct[dailycct$ActualDateCreated >= "2016-11-01",]
df <- df %>% group_by(ActualDateCreated) %>% mutate(nTotal = sum(n))
plot_ly(df,
type = 'bar',
x = ~ActualDateCreated,
y = ~n,
color = ~Business.Sub.Area,
text = ~paste('Date: ', ActualDateCreated, '\n',
'Team: ', Business.Sub.Area, '\n',
'Count: ', n, '\n',
'Total: ', nTotal,'\n'),
hoverinfo = 'text'
) %>%
layout(yaxis = list(title = 'Count'), barmode = 'stack')
With this you get a plot that looks like this:
Upvotes: 3