With the help of the code i can get the total value but while hovering its showing the vector of the total value so i need to show the separate total for each bar kindly help me to achieve
Thanks in Advance
structure(list(Sector = c("Agri-business", "Agri-business", "Agri-business",
"Agri-business", "Agri-business", "Education & Employability",
"Education & Employability", "Education & Employability", "Education & Employability",
"Education & Employability", "Energy & CleanTech", "Energy & CleanTech",
"Energy & CleanTech", "Health", "Health", "Health", "Health",
"Health"), year_range = c("2017-2018", "2018-2019", "2019-2020",
"2020-2021", "2021-2022", "2017-2018", "2018-2019", "2019-2020",
"2020-2021", "2021-2022", "2019-2020", "2020-2021", "2021-2022",
"2017-2018", "2018-2019", "2019-2020", "2020-2021", "2021-2022"
), Month = structure(c(12L, 12L, 12L, 12L, 4L, 12L, 12L, 12L,
12L, 4L, 12L, 12L, 4L, 12L, 12L, 12L, 12L, 4L), .Label = c("Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan",
"Feb", "Mar"), class = "factor"), total = c(1, 3, 9, 13, 13,
1, 6, 3, 1, 3, 1, 6, 9, 9, 12, 10, 4, 4)), row.names = c(NA,
-18L), class = c("data.table", "data.frame"), sorted = c("Sector",
"year_range")
sum_total<-aggregate(total~year_range,df1,FUN = sum)
d<-sum_total%>%select(-year_range)
Removestring(ggplotly(
df1%>%
ggplot(aes(x = as.character(year_range), y = total,text = paste(d))) +
geom_col(aes(fill = Sector))+theme_classic()+
theme(axis.line.y = element_blank(),axis.ticks = element_blank(),legend.position = "bottom")+
labs(x="", y="No.of total companies", fill="")+
theme(axis.title.y =element_text(size=8))+
scale_fill_manual(values=c("#1F7A3F","#0AAA4D","#70B821","#9BDFAF"))+
scale_y_continuous(labels = function(x) format(x, scientific = FALSE),expand = expansion(mult = c(0,.3)),breaks = integer_breaks()),tooltip = c("text"))%>%
layout(legend = list(orientation = "h", x = 0.1, y = -0.2,font=list( family='Arial', size=10, color='black')),xaxis = x_labels,yaxis = y_labels)%>%
config(displaylogo = FALSE,modeBarButtonsToRemove = list('sendDataToCloud', 'autoScale2d', 'resetScale2d', 'toggleSpikelines','hoverClosestCartesian',
'hoverCompareCartesian','zoom2d','pan2d','select2d','lasso2d','zoomIn2d','zoomOut2d')))
The issue is that you pass the whole vector of totals to the text aes. Instead I would suggest to add the group totals to your dataframe which could then be easily mapped on the text
aes:
library(plotly)
df1 %>%
group_by(year_range) %>%
mutate(total_year_range = sum(total)) %>%
ungroup() %>%
ggplot(aes(x = as.character(year_range), y = total, text = total_year_range)) +
geom_col(aes(fill = Sector)) +
theme_classic() +
theme(axis.line.y = element_blank(), axis.ticks = element_blank(), legend.position = "bottom") +
labs(x = "", y = "No.of total companies", fill = "") +
theme(axis.title.y = element_text(size = 8)) +
scale_fill_manual(values = c("#1F7A3F", "#0AAA4D", "#70B821", "#9BDFAF")) +
scale_y_continuous(
labels = function(x) format(x, scientific = FALSE),
expand = expansion(mult = c(0, .3))
#,breaks = integer_breaks()
)
ggplotly(tooltip = c("text")) %>%
layout(legend = list(
orientation = "h", x = 0.1, y = -0.2,
font = list(family = "Arial", size = 10, color = "black")
#,xaxis = x_labels,yaxis = y_labels
)) %>%
config(displaylogo = FALSE, modeBarButtonsToRemove = list(
"sendDataToCloud", "autoScale2d", "resetScale2d", "toggleSpikelines", "hoverClosestCartesian",
"hoverCompareCartesian", "zoom2d", "pan2d", "select2d", "lasso2d", "zoomIn2d", "zoomOut2d"
))