如何对geom_text中包含在其他文本中的值进行舍入?

时间:2018-05-03 11:26:58

标签: r ggplot2 label rounding geom-text

我有一些代码:

library(dplyr)
library(tidyr)
library(ggplot2)

eu_chats %>%
  spread(type, count, fill = 0) %>%   # Spread the count column in missed and completed
  mutate(Total = Completed + Missed) %>%   # Create the Total column
  ggplot(aes(date, Total)) + 
  geom_col(aes(fill = "Total"),
           colour = "black") + # total bar (with stat = "identity")
  geom_col(aes(y = Missed, fill = "Missed"),
           colour = "black") + # missed bar
  geom_text(aes(label = paste("Total chats:", Total)), # add total label
            hjust = -0.05, vjust = 1) + 
  geom_text(aes(label = paste("Missed chats:", Missed, "(", Missed/Total*100, "%)")), # add missed label and calculate percentage
            hjust = -0.05, vjust = -0.5, color = "red") + 
  scale_fill_manual(name = "",  # Manual fill scale
                    values = c("Total" = "forestgreen", "Missed" = "red")) +
  facet_grid(retailer~.) +  # Displayed per retailer
  scale_y_continuous(limits = c(0, max(eu_chats$count) * 2))

生成此图表:

enter image description here

如何将Missed chats:标签中的值四舍五入到小数点后两位?

其他解决方案似乎只关注那些除了价值本身之外不存在其他文本的情况。

这可以在我的情况下实现,同时仍然保留标签文本吗?

0 个答案:

没有答案