Jane
Jane

Reputation: 13

Showing x-labels only at regular interval ggplot2

I have following data. Please note that the date time values are not continuous.

DateTime value
1  X2016.08.10.00.00.00.03.00.00     0
2  X2016.08.10.03.00.00.06.00.00     1
3  X2016.08.10.06.00.00.09.00.00    17
4  X2016.08.10.09.00.00.12.00.00    28
5  X2016.08.10.12.00.00.15.00.00    22
6  X2016.08.10.15.00.00.18.00.00    31
7  X2016.08.10.18.00.00.21.00.00    16
8  X2016.08.10.21.00.00.00.00.00     4
9  X2016.08.11.00.00.00.03.00.00     0
10 X2016.08.11.03.00.00.06.00.00     4
11 X2016.08.11.06.00.00.09.00.00    23
12 X2016.08.11.09.00.00.12.00.00    22
13 X2016.08.11.12.00.00.15.00.00    24
14 X2016.08.11.15.00.00.18.00.00    30
15 X2016.08.11.18.00.00.21.00.00    15
16 X2016.08.11.21.00.00.00.00.00     5
17 X2016.08.12.00.00.00.03.00.00     0
18 X2016.08.12.03.00.00.06.00.00     3
19 X2016.08.12.06.00.00.09.00.00    26
20 X2016.08.12.09.00.00.12.00.00    20
21 X2016.08.12.12.00.00.15.00.00    17
22 X2016.08.12.15.00.00.18.00.00    34
23 X2016.08.12.18.00.00.21.00.00    19
24 X2016.08.12.21.00.00.00.00.00     5
25 X2016.08.15.00.00.00.03.00.00     0
26 X2016.08.15.03.00.00.06.00.00     4
27 X2016.08.15.06.00.00.09.00.00    24
28 X2016.08.15.09.00.00.12.00.00    16
29 X2016.08.15.12.00.00.15.00.00    31
30 X2016.08.15.15.00.00.18.00.00    26
31 X2016.08.15.18.00.00.21.00.00    24
32 X2016.08.15.21.00.00.00.00.00    16
33 X2016.08.16.00.00.00.03.00.00     0
34 X2016.08.16.03.00.00.06.00.00     4
35 X2016.08.16.06.00.00.09.00.00    18
36 X2016.08.16.09.00.00.12.00.00    26
37 X2016.08.16.12.00.00.15.00.00    27
38 X2016.08.16.15.00.00.18.00.00    26
39 X2016.08.16.18.00.00.21.00.00    26
40 X2016.08.16.21.00.00.00.00.00     9
41 X2016.08.17.00.00.00.03.00.00     0
42 X2016.08.17.03.00.00.06.00.00     2
43 X2016.08.17.06.00.00.09.00.00    14
44 X2016.08.17.09.00.00.12.00.00    15
45 X2016.08.17.12.00.00.15.00.00    22
46 X2016.08.17.15.00.00.18.00.00    35
47 X2016.08.17.18.00.00.21.00.00    20
48 X2016.08.17.21.00.00.00.00.00     6
49 X2016.08.18.00.00.00.03.00.00     0
50 X2016.08.18.03.00.00.06.00.00     2

I tried to create the following plot. But I only want to keep x-axis labels at few regular intervals. I tried setting breaks but couldn't figure out.

enter image description here

Following is the code

ggplot(data=mdf, aes(x=DateTime, y=value, group = X)) +   geom_line() +
      geom_point( size=2, shape=21, fill="white") +
      labs(x = "DateTime", y = "") + scale_x_discrete(labels = datetime) +
      theme(axis.text.x = element_text(angle = 90, hjust = 1))

Upvotes: 0

Views: 103

Answers (1)

acylam
acylam

Reputation: 18691

You can first use gsub to format your datetime, then use scale_x_datetime to set date_breaks = "1 days":

library(dplyr)
library(lubridate)
library(ggplot2)

df %>%
  mutate(DateTime = gsub("(X|(\\.\\d{2}){3}$)", "", DateTime),
         DateTime = parse_date_time(DateTime, "ymdHMS")) %>%
  ggplot(aes(x=DateTime, y=value)) +   geom_line() +
    geom_point(size=2, shape=21, fill="white") +
    labs(x = "DateTime", y = "") + scale_x_datetime("DateTime", date_breaks = "1 days") +
    theme(axis.text.x = element_text(angle = 30, hjust = 1))

Result:

> df %>%
+   mutate(DateTime = gsub("(X|(\\.\\d{2}){3}$)", "", DateTime),
+          DateTime = parse_date_time(DateTime, "ymdHMS")) %>% head(10)

              DateTime value
1  2016-08-10 00:00:00     0
2  2016-08-10 03:00:00     1
3  2016-08-10 06:00:00    17
4  2016-08-10 09:00:00    28
5  2016-08-10 12:00:00    22
6  2016-08-10 15:00:00    31
7  2016-08-10 18:00:00    16
8  2016-08-10 21:00:00     4
9  2016-08-11 00:00:00     0
10 2016-08-11 03:00:00     4

enter image description here

Data:

df = structure(list(DateTime = c("X2016.08.10.00.00.00.03.00.00", 
"X2016.08.10.03.00.00.06.00.00", "X2016.08.10.06.00.00.09.00.00", 
"X2016.08.10.09.00.00.12.00.00", "X2016.08.10.12.00.00.15.00.00", 
"X2016.08.10.15.00.00.18.00.00", "X2016.08.10.18.00.00.21.00.00", 
"X2016.08.10.21.00.00.00.00.00", "X2016.08.11.00.00.00.03.00.00", 
"X2016.08.11.03.00.00.06.00.00", "X2016.08.11.06.00.00.09.00.00", 
"X2016.08.11.09.00.00.12.00.00", "X2016.08.11.12.00.00.15.00.00", 
"X2016.08.11.15.00.00.18.00.00", "X2016.08.11.18.00.00.21.00.00", 
"X2016.08.11.21.00.00.00.00.00", "X2016.08.12.00.00.00.03.00.00", 
"X2016.08.12.03.00.00.06.00.00", "X2016.08.12.06.00.00.09.00.00", 
"X2016.08.12.09.00.00.12.00.00", "X2016.08.12.12.00.00.15.00.00", 
"X2016.08.12.15.00.00.18.00.00", "X2016.08.12.18.00.00.21.00.00", 
"X2016.08.12.21.00.00.00.00.00", "X2016.08.15.00.00.00.03.00.00", 
"X2016.08.15.03.00.00.06.00.00", "X2016.08.15.06.00.00.09.00.00", 
"X2016.08.15.09.00.00.12.00.00", "X2016.08.15.12.00.00.15.00.00", 
"X2016.08.15.15.00.00.18.00.00", "X2016.08.15.18.00.00.21.00.00", 
"X2016.08.15.21.00.00.00.00.00", "X2016.08.16.00.00.00.03.00.00", 
"X2016.08.16.03.00.00.06.00.00", "X2016.08.16.06.00.00.09.00.00", 
"X2016.08.16.09.00.00.12.00.00", "X2016.08.16.12.00.00.15.00.00", 
"X2016.08.16.15.00.00.18.00.00", "X2016.08.16.18.00.00.21.00.00", 
"X2016.08.16.21.00.00.00.00.00", "X2016.08.17.00.00.00.03.00.00", 
"X2016.08.17.03.00.00.06.00.00", "X2016.08.17.06.00.00.09.00.00", 
"X2016.08.17.09.00.00.12.00.00", "X2016.08.17.12.00.00.15.00.00", 
"X2016.08.17.15.00.00.18.00.00", "X2016.08.17.18.00.00.21.00.00", 
"X2016.08.17.21.00.00.00.00.00", "X2016.08.18.00.00.00.03.00.00", 
"X2016.08.18.03.00.00.06.00.00"), value = c(0L, 1L, 17L, 28L, 
22L, 31L, 16L, 4L, 0L, 4L, 23L, 22L, 24L, 30L, 15L, 5L, 0L, 3L, 
26L, 20L, 17L, 34L, 19L, 5L, 0L, 4L, 24L, 16L, 31L, 26L, 24L, 
16L, 0L, 4L, 18L, 26L, 27L, 26L, 26L, 9L, 0L, 2L, 14L, 15L, 22L, 
35L, 20L, 6L, 0L, 2L)), .Names = c("DateTime", "value"), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", 
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", 
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", 
"47", "48", "49", "50"))

Upvotes: 2

Related Questions