У меня есть диаграмма, которая показывает данные за неделю.
Проблема в том, что она не показывает все значения даты на оси (код ниже):
Как это исправить, чтобы показать все 7 значений даты?
Код:
Набор данных:
us_chats <- structure(list(date = structure(c(1524783600, 1524870000, 1524870000,
1524956400, 1525042800, 1525042800, 1525129200, 1525129200, 1525215600,
1525215600, 1525302000), class = c("POSIXct", "POSIXt"), tzone = ""),
type = c("Completed", "Completed", "Missed", "Completed",
"Completed", "Missed", "Completed", "Missed", "Completed",
"Missed", "Completed"), count = c(2L, 2L, 1L, 1L, 7L, 2L,
2L, 1L, 5L, 4L, 4L)), row.names = c(NA, -11L), class = "data.frame")
Код диаграммы:
us_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, "(", round(Missed/Total*100, 2), "%)")), # 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")) +
scale_y_continuous(limits = c(0, max(us_chats$count) * 3)) + # Make labels visible
coord_flip() # switch x and y axes