您可以在數據中為每個class創建一個sum為amount的新列: library(dplyr)library(ggplot2)library(plotly)plot1 <- hp %>% group_by(class, year) %>% summarise(amount = sum(amount)) %>% mutate(total_sum = sum(amount)) %>% ggplot(aes(class, amount, fill = year, text = total_sum)) + geom_col() + geom_text(aes(label = amount), vjust = -1, position = position_stack(vjust = .5))ggplotly(plot1, tooltip = 'text')