0

I have a chart that shows a week's worth of data.

The trouble is that it doesn't show all of the date values on the axis (code is below):

enter image description here

How can I fix this to show all 7 date values?

Code:

Dataset:

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

Chart code:

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
Mus
  • 7,290
  • 24
  • 86
  • 130
  • Your example isn't reproducible (we don't have `us_chats`), but the usual reason for this is that R thinks it doesn't have enough space for the labels. It might have made this decision before flipping the axes; perhaps you will get better results by moving `coord_flip()` earlier. – user2554330 May 11 '18 at 13:13
  • @user2554330 Dataset now added. Moving `coord_flip()` didn't change anything. Also, I thought it was to do with available space but I tested it to the extreme (dragging it down into my second monitor) and it remained the same. – Mus May 11 '18 at 13:16
  • `scale_x_date(date_breaks = "1 day")` http://ggplot2.tidyverse.org/reference/scale_date.html – Jack Brookes May 11 '18 at 13:30
  • @JackBrookes Thanks, but it give me this error: `Error: Invalid input: date_trans works with objects of class Date only`. – Mus May 11 '18 at 13:30
  • Use `as.Date(date)` instead of just `date`. – user2554330 May 11 '18 at 13:31
  • @user2554330 SO close - the tick marks are now 1 day less than their actual values (3rd May 2018 should be 4th May 2018 and so on). – Mus May 11 '18 at 13:33
  • They are fine for me; I think we are in different time zones, so your earliest value `"2018-04-26 19:00:00 EDT"` shows up for me as `"2018-04-26"`. – user2554330 May 11 '18 at 13:36
  • @user2554330 I see. In fact, I hacked it slightly by using `as.Date(date) + 1` and that worked. – Mus May 11 '18 at 13:37

1 Answers1

1

Jack Brookes suggested using scale_x_date(date_breaks = "1 day"). That doesn't quite work because it requires the data to be of class Date. Unfortunately, that is a little ambiguous, because the date corresponding to a time depends on your time zone. If you happen to be in timezone NZ, then this may give you what you want:

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(as.Date(date, tz = "NZ"), 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
  scale_x_date(date_breaks = "1 day", name = "Date") +
  coord_flip()

You can use OlsonNames() to see a list of timezone names that R will recognize. Your system may accept some others, too.

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • This is a good approach. I am based in London, England, so using +1 worked the same as using `tz = "NZ"` / a different `tz`, but it's an elegant solution nonetheless. The strange thing about this whole problem is that it was working fine up until last week (the last time it showed all tick marks). Since this week, this problem arose and nothing changed which would have caused it to, either. – Mus May 11 '18 at 13:51