Mus
Mus

Reputation: 7540

How can I get all 30-minute label breaks to show on the x-axis?

I have some data which is recorded at 30-minute intervals:

df <- structure(list(from = c("2019-11-24T23:30Z", "2019-11-25T00:00Z", 
"2019-11-25T00:30Z", "2019-11-25T01:00Z", "2019-11-25T01:30Z", 
"2019-11-25T02:00Z", "2019-11-25T02:30Z", "2019-11-25T03:00Z", 
"2019-11-25T03:30Z", "2019-11-25T04:00Z", "2019-11-25T04:30Z", 
"2019-11-25T05:00Z", "2019-11-25T05:30Z", "2019-11-25T06:00Z", 
"2019-11-25T06:30Z", "2019-11-25T07:00Z", "2019-11-25T07:30Z", 
"2019-11-25T08:00Z", "2019-11-25T08:30Z", "2019-11-25T09:00Z", 
"2019-11-25T09:30Z", "2019-11-25T10:00Z", "2019-11-25T10:30Z", 
"2019-11-25T11:00Z", "2019-11-25T11:30Z"), to = c("2019-11-25T00:00Z", 
"2019-11-25T00:30Z", "2019-11-25T01:00Z", "2019-11-25T01:30Z", 
"2019-11-25T02:00Z", "2019-11-25T02:30Z", "2019-11-25T03:00Z", 
"2019-11-25T03:30Z", "2019-11-25T04:00Z", "2019-11-25T04:30Z", 
"2019-11-25T05:00Z", "2019-11-25T05:30Z", "2019-11-25T06:00Z", 
"2019-11-25T06:30Z", "2019-11-25T07:00Z", "2019-11-25T07:30Z", 
"2019-11-25T08:00Z", "2019-11-25T08:30Z", "2019-11-25T09:00Z", 
"2019-11-25T09:30Z", "2019-11-25T10:00Z", "2019-11-25T10:30Z", 
"2019-11-25T11:00Z", "2019-11-25T11:30Z", "2019-11-25T12:00Z"
), forecast = c(210L, 199L, 200L, 204L, 199L, 192L, 191L, 194L, 
197L, 192L, 193L, 194L, 204L, 210L, 242L, 272L, 293L, 290L, 275L, 
270L, 272L, 272L, 269L, 268L, 266L), actual = c(200L, 200L, 198L, 
189L, 191L, 193L, 197L, 193L, 193L, 194L, 199L, 207L, 230L, 255L, 
272L, 283L, 283L, 278L, 277L, 278L, 273L, 270L, 270L, 272L, 283L
), index = c("moderate", "moderate", "moderate", "moderate", 
"moderate", "moderate", "moderate", "moderate", "moderate", "moderate", 
"moderate", "moderate", "moderate", "moderate", "high", "high", 
"high", "high", "high", "high", "high", "high", "high", "high", 
"high")), row.names = c(NA, 25L), class = "data.frame")

Produces:

> df
                from                to forecast actual    index
1  2019-11-24T23:30Z 2019-11-25T00:00Z      210    200 moderate
2  2019-11-25T00:00Z 2019-11-25T00:30Z      199    200 moderate
3  2019-11-25T00:30Z 2019-11-25T01:00Z      200    198 moderate
4  2019-11-25T01:00Z 2019-11-25T01:30Z      204    189 moderate
5  2019-11-25T01:30Z 2019-11-25T02:00Z      199    191 moderate
6  2019-11-25T02:00Z 2019-11-25T02:30Z      192    193 moderate
7  2019-11-25T02:30Z 2019-11-25T03:00Z      191    197 moderate
8  2019-11-25T03:00Z 2019-11-25T03:30Z      194    193 moderate
9  2019-11-25T03:30Z 2019-11-25T04:00Z      197    193 moderate
10 2019-11-25T04:00Z 2019-11-25T04:30Z      192    194 moderate
11 2019-11-25T04:30Z 2019-11-25T05:00Z      193    199 moderate
12 2019-11-25T05:00Z 2019-11-25T05:30Z      194    207 moderate
13 2019-11-25T05:30Z 2019-11-25T06:00Z      204    230 moderate
14 2019-11-25T06:00Z 2019-11-25T06:30Z      210    255 moderate
15 2019-11-25T06:30Z 2019-11-25T07:00Z      242    272     high
16 2019-11-25T07:00Z 2019-11-25T07:30Z      272    283     high
17 2019-11-25T07:30Z 2019-11-25T08:00Z      293    283     high
18 2019-11-25T08:00Z 2019-11-25T08:30Z      290    278     high
19 2019-11-25T08:30Z 2019-11-25T09:00Z      275    277     high
20 2019-11-25T09:00Z 2019-11-25T09:30Z      270    278     high
21 2019-11-25T09:30Z 2019-11-25T10:00Z      272    273     high
22 2019-11-25T10:00Z 2019-11-25T10:30Z      272    270     high
23 2019-11-25T10:30Z 2019-11-25T11:00Z      269    270     high
24 2019-11-25T11:00Z 2019-11-25T11:30Z      268    272     high
25 2019-11-25T11:30Z 2019-11-25T12:00Z      266    283     high

Once plotted, how can I show every 30-minute interval on the x-axis instead of the default breaks?

library(ggplot2)

ggplot(df) +
  geom_line(aes(from, forecast),
             colour = "grey",
            linetype = "dashed") +
  geom_line(aes(from, actual),
             colour = "blue") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0))

Produces:

enter image description here

Upvotes: 1

Views: 83

Answers (1)

lroha
lroha

Reputation: 34586

Make sure that the x axis has the appropriate format then you can use scale_x_datetime(date_breaks = "30 min") to set the breaks.

df %>%
  mutate(from = as.POSIXct(from, format="%Y-%m-%dT%H:%M")) %>%
  ggplot() +
  geom_line(aes(from, forecast),
            colour = "grey",
            linetype = "dashed") +
  geom_line(aes(from, actual),
            colour = "blue") +
  theme(axis.text.x = element_text(angle = 90, hjust = 0)) +
  scale_x_datetime(date_breaks = "30 min")

enter image description here

Upvotes: 1

Related Questions