0

Hello I have some issues with plotting weekdays under the hours on x axis.

Here is an example of what I want to reproduce:

enter image description here

Q1 and Q3 are the confidence levels and the blue line is the average of ED occupancy which I need them on the graph. But do not need the max on the chart.

And here is the reproducible example, with data being modified due to confidentiality reasons:

dput(tbl_avg_occ_2)
structure(list(hour = c(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 
23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 
17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 
20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L), day = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L), .Label = c("Mon", "Tue", "Wed", "Thu", "Fri", 
"Sat", "Sun"), class = c("ordered", "factor")), Average_occ = c(36, 
35.8333333333333, 27.5, 22.5, 21.8333333333333, 17.8333333333333, 
14.3333333333333, 17, 23.5, 46.6666666666667, 60.8333333333333, 
72.5, 69.6666666666667, 76, 75.6666666666667, 73.8333333333333, 
75, 70.8333333333333, 71.5, 70.8333333333333, 58.6666666666667, 
59.8333333333333, 57, 51.1666666666667, 38.6666666666667, 31.3333333333333, 
28, 21.8333333333333, 21.1666666666667, 17.5, 13.8333333333333, 
14.6666666666667, 19.5, 38.1666666666667, 48.6666666666667, 55, 
60.5, 58.6666666666667, 61, 69.3333333333333, 61.1666666666667, 
68.6666666666667, 63.6666666666667, 65.6666666666667, 58.6666666666667, 
57.6666666666667, 60.3333333333333, 49.3333333333333, 31.1428571428571, 
27.4285714285714, 21.1428571428571, 18.5714285714286, 21.6666666666667, 
16.5, 12.6666666666667, 14.3333333333333, 19.3333333333333, 39, 
51.6666666666667, 62, 61.5, 53.1428571428571, 63.3333333333333, 
51.5714285714286, 68.6666666666667, 66.3333333333333, 52.7142857142857, 
56.5714285714286, 52.8571428571429, 55, 46.5714285714286, 45.1428571428571, 
43.1666666666667, 34.6666666666667, 29.1666666666667, 24.5, 23, 
14.8333333333333, 14.1666666666667, 18.6666666666667, 21.6666666666667, 
42.5, 50.1666666666667, 62.5, 66.6666666666667, 71, 72.5, 67.8333333333333, 
70.3333333333333, 68.6666666666667, 69.8333333333333, 67.5, 59.5, 
56.8333333333333, 54, 44.5, 40, 33, 31, 25, 20.3333333333333, 
17.1666666666667, 14.8333333333333, 15.1666666666667, 22.8333333333333, 
43.5, 50.3333333333333, 62.3333333333333, 65.6666666666667, 70, 
65.3333333333333, 65.1666666666667, 70.3333333333333, 76.1666666666667, 
65, 68.6666666666667, 60.8333333333333, 61.1666666666667, 55.6666666666667, 
52.1666666666667, 45, 40.5, 31, 27.3333333333333, 25.5, 18.8333333333333, 
17.5, 20.8333333333333, 17.8333333333333, 33.5, 39.3333333333333, 
44.8333333333333, 57.6666666666667, 55, 55.1666666666667, 63, 
62.5, 62.3333333333333, 58.6666666666667, 55, 61.5, 52.6666666666667, 
50.6666666666667, 45, 43.1666666666667, 35.8333333333333, 33.6666666666667, 
27.3333333333333, 24.3333333333333, 21.3333333333333, 18.5, 18, 
25.1666666666667, 37.1666666666667, 45.5, 52.8333333333333, 53, 
61.1666666666667, 66.8333333333333, 59.1666666666667, 59.5, 59.3333333333333, 
58.5, 64, 46.8333333333333, 53.1666666666667, 49.6666666666667, 
47.8333333333333), Q1 = c(35, 34.8333333333333, 26.5, 21.5, 20.8333333333333, 
16.8333333333333, 14.3333333333333, 17, 23.5, 46.6666666666667, 
60.8333333333333, 71.5, 69.6666666666667, 76, 74.6666666666667, 
72.8333333333333, 75, 70.8333333333333, 70.5, 70.8333333333333, 
57.6666666666667, 59.8333333333333, 57, 51.1666666666667, 38.6666666666667, 
31.3333333333333, 28, 21.8333333333333, 21.1666666666667, 18.5, 
13.8333333333333, 14.6666666666667, 19.5, 37.1666666666667, 49.6666666666667, 
55, 60.5, 58.6666666666667, 61, 69.3333333333333, 61.1666666666667, 
68.6666666666667, 63.6666666666667, 65.6666666666667, 58.6666666666667, 
57.6666666666667, 60.3333333333333, 49.3333333333333, 31.1428571428571, 
27.4285714285714, 21.1428571428571, 18.5714285714286, 21.6666666666667, 
16.5, 12.6666666666667, 14.3333333333333, 19.3333333333333, 39, 
50.6666666666667, 62, 61.5, 52.1428571428571, 62.3333333333333, 
51.5714285714286, 68.6666666666667, 66.3333333333333, 52.7142857142857, 
55.5714285714286, 51.8571428571429, 55, 46.5714285714286, 44.1428571428571, 
43.1666666666667, 33.6666666666667, 29.1666666666667, 24.5, 23, 
14.8333333333333, 13.1666666666667, 17.6666666666667, 21.6666666666667, 
42.5, 50.1666666666667, 62.5, 66.6666666666667, 71, 72.5, 67.8333333333333, 
70.3333333333333, 68.6666666666667, 69.8333333333333, 67.5, 59.5, 
56.8333333333333, 54, 44.5, 40, 33, 31, 25, 20.3333333333333, 
17.1666666666667, 14.8333333333333, 15.1666666666667, 22.8333333333333, 
43.5, 50.3333333333333, 62.3333333333333, 65.6666666666667, 70, 
65.3333333333333, 65.1666666666667, 70.3333333333333, 76.1666666666667, 
65, 68.6666666666667, 60.8333333333333, 61.1666666666667, 55.6666666666667, 
52.1666666666667, 45, 40.5, 31, 27.3333333333333, 25.5, 18.8333333333333, 
17.5, 20.8333333333333, 17.8333333333333, 33.5, 39.3333333333333, 
44.8333333333333, 57.6666666666667, 55, 55.1666666666667, 63, 
62.5, 62.3333333333333, 58.6666666666667, 55, 61.5, 52.6666666666667, 
50.6666666666667, 45, 43.1666666666667, 35.8333333333333, 33.6666666666667, 
27.3333333333333, 24.3333333333333, 21.3333333333333, 18.5, 18, 
25.1666666666667, 37.1666666666667, 45.5, 52.8333333333333, 53, 
61.1666666666667, 66.8333333333333, 59.1666666666667, 59.5, 59.3333333333333, 
58.5, 64, 46.8333333333333, 53.1666666666667, 49.6666666666667, 
47.8333333333333), Q3 = c(35, 34.8333333333333, 26.5, 21.5, 20.8333333333333, 
16.8333333333333, 14.3333333333333, 17, 23.5, 46.6666666666667, 
60.8333333333333, 71.5, 69.6666666666667, 76, 74.6666666666667, 
72.8333333333333, 75, 70.8333333333333, 70.5, 70.8333333333333, 
57.6666666666667, 59.8333333333333, 57, 51.1666666666667, 38.6666666666667, 
31.3333333333333, 28, 21.8333333333333, 21.1666666666667, 18.5, 
13.8333333333333, 14.6666666666667, 19.5, 37.1666666666667, 49.6666666666667, 
55, 60.5, 58.6666666666667, 61, 69.3333333333333, 61.1666666666667, 
68.6666666666667, 63.6666666666667, 65.6666666666667, 58.6666666666667, 
57.6666666666667, 60.3333333333333, 49.3333333333333, 31.1428571428571, 
27.4285714285714, 21.1428571428571, 18.5714285714286, 21.6666666666667, 
16.5, 12.6666666666667, 14.3333333333333, 19.3333333333333, 39, 
50.6666666666667, 62, 61.5, 52.1428571428571, 62.3333333333333, 
51.5714285714286, 68.6666666666667, 66.3333333333333, 52.7142857142857, 
55.5714285714286, 51.8571428571429, 55, 46.5714285714286, 44.1428571428571, 
43.1666666666667, 33.6666666666667, 29.1666666666667, 24.5, 23, 
14.8333333333333, 13.1666666666667, 17.6666666666667, 21.6666666666667, 
42.5, 50.1666666666667, 62.5, 66.6666666666667, 71, 72.5, 67.8333333333333, 
70.3333333333333, 68.6666666666667, 69.8333333333333, 67.5, 59.5, 
56.8333333333333, 54, 44.5, 40, 33, 31, 25, 20.3333333333333, 
17.1666666666667, 14.8333333333333, 15.1666666666667, 22.8333333333333, 
43.5, 50.3333333333333, 62.3333333333333, 65.6666666666667, 70, 
65.3333333333333, 65.1666666666667, 70.3333333333333, 76.1666666666667, 
65, 68.6666666666667, 60.8333333333333, 61.1666666666667, 55.6666666666667, 
52.1666666666667, 45, 40.5, 31, 27.3333333333333, 25.5, 18.8333333333333, 
17.5, 20.8333333333333, 17.8333333333333, 33.5, 39.3333333333333, 
44.8333333333333, 57.6666666666667, 55, 55.1666666666667, 63, 
62.5, 62.3333333333333, 58.6666666666667, 55, 61.5, 52.6666666666667, 
50.6666666666667, 45, 43.1666666666667, 35.8333333333333, 33.6666666666667, 
27.3333333333333, 24.3333333333333, 21.3333333333333, 18.5, 18, 
25.1666666666667, 37.1666666666667, 45.5, 52.8333333333333, 53, 
61.1666666666667, 66.8333333333333, 59.1666666666667, 59.5, 59.3333333333333, 
58.5, 64, 46.8333333333333, 53.1666666666667, 49.6666666666667, 
47.8333333333333)), row.names = c(NA, -168L), class = "data.frame")

I've tried to plot hours first:

g1 <- tbl_avg_occ_2 %>%
  ggplot(aes(hour, Average_occ))+
  geom_line(colour = "blue")

g1

and got this: enter image description here

I have tried another code:

plt <-  tbl_avg_occ_2 %>%
    ggplot(aes(x = hour, y = Average_occ, group = day, colour = day)) +
    geom_point(size = 1) +
    geom_line(size = 0.5) +
    facet_wrap( ~ day, nrow = 1) +
    scale_x_continuous(limits = c(0, 23)) +
    scale_y_continuous(limits = c(0, 80)) +
    theme_bw() 

enter image description here

Yet, facet_wrap isn't really what I want as obviously it is a different from the picture provided.

Yet tried to add date_breaks on scale_x_continuous and did not manage since I believe I need the hours and weeks in a date format. Yet, the type of manipulation used required me to keep the dataframe as above (as it is in reproducible example). Is there a way to plot it with the type of data I have?

GaB
  • 1,076
  • 2
  • 16
  • 29
  • 2
    What did you try so far? – Adela Oct 02 '18 at 11:54
  • 3
    Possible duplicate of [Multi-row x-axis labels in ggplot line chart](https://stackoverflow.com/questions/20571306/multi-row-x-axis-labels-in-ggplot-line-chart) – markus Oct 02 '18 at 12:00
  • Try adding `facet_grid(facets = ~day)` to your ggplot. – jdobres Oct 02 '18 at 12:06
  • @markus - it is not a duplicate since my type of data I have given in reproducible example has no dates and the separating lines do not appear on the actual plot but rather on the x axis. In addition to that, markus, it does not have the confidence levels plotted. – GaB Oct 02 '18 at 12:07
  • your problem: your have 7 values for each hour (one for every day)... set your timestamp to POSIXct, and ggplot2 will help you by adding scale_x_datetime to the x-axis. – Wimpel Oct 02 '18 at 12:12
  • @jdobres - tried it and re-edited my post. This is not really what I want. – GaB Oct 02 '18 at 12:14
  • @Wimpel - yes, indeed, yet, if you look once again on what I have posted (as I re-edited), it may be possible to achieve what I am intending? – GaB Oct 02 '18 at 12:15
  • @Adela, tank you for prompting me to re-edit the question I am seeking to solve. – GaB Oct 02 '18 at 12:17
  • @Wimpel - the manipulation of the date requires me to get to that type of data. Otherwise will end up having more rows than I need. And somehow, as the third picture is provided in my trials, I might guess it is possible to achieve the first outcome (first pic) – GaB Oct 02 '18 at 12:19

0 Answers0