如何悬停每个栏的总数

How to hover the total of each bar

在代码的帮助下,我可以获得总值,但在悬停时显示总值的向量,所以我需要显示每个柱的单独总计,请帮助我实现

提前致谢

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

问题是您将总计的整个向量传递给了文本 aes。相反,我建议将组总计添加到您的数据框中,然后可以轻松地将其映射到 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"
  ))