tom91
tom91

Reputation: 685

Adding secondary x-axis label in ggplot2

I have produced this graph:

enter image description here

It is produced using this code:

ggplot(June, aes(x=Date, y=Count, fill=Species)) + 
  geom_bar(position='dodge', stat='identity') +
  theme(axis.text.x = element_text(angle=90, vjust=0.5))

A sample of the data is here:

structure(list(Day = c("01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", 
"02/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", 
"02/06/17", "02/06/17", "02/06/17", "02/06/17"), Time = c("20:00", 
"20:00", "20:00", "21:00", "21:00", "21:00", "22:00", "22:00", 
"22:00", "23:00", "23:00", "23:00", "00:00", "00:00", "00:00", 
"01:00", "01:00", "01:00", "02:00", "02:00", "02:00", "03:00", 
"03:00", "03:00", "04:00", "04:00", "04:00", "05:00", "05:00", 
"05:00", "20:00", "20:00", "20:00", "21:00", "21:00", "21:00", 
"22:00", "22:00", "22:00", "23:00", "23:00", "23:00", "00:00", 
"00:00"), Date = c("01/06/17 20:00", "01/06/17 20:00", "01/06/17 20:00", 
"01/06/17 21:00", "01/06/17 21:00", "01/06/17 21:00", "01/06/17 22:00", 
"01/06/17 22:00", "01/06/17 22:00", "01/06/17 23:00", "01/06/17 23:00", 
"01/06/17 23:00", "01/06/17 00:00", "01/06/17 00:00", "01/06/17 00:00", 
"01/06/17 01:00", "01/06/17 01:00", "01/06/17 01:00", "01/06/17 02:00", 
"01/06/17 02:00", "01/06/17 02:00", "01/06/17 03:00", "01/06/17 03:00", 
"01/06/17 03:00", "01/06/17 04:00", "01/06/17 04:00", "01/06/17 04:00", 
"01/06/17 05:00", "01/06/17 05:00", "01/06/17 05:00", "02/06/17 20:00", 
"02/06/17 20:00", "02/06/17 20:00", "02/06/17 21:00", "02/06/17 21:00", 
"02/06/17 21:00", "02/06/17 22:00", "02/06/17 22:00", "02/06/17 22:00", 
"02/06/17 23:00", "02/06/17 23:00", "02/06/17 23:00", "02/06/17 00:00", 
"02/06/17 00:00"), Species = c("Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle"), Count = c(0L, 0L, 
0L, 20L, 0L, 0L, 85L, 0L, 1L, 9L, 0L, 0L, 7L, 0L, 0L, 2L, 0L, 
0L, 1L, 0L, 0L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
22L, 0L, 0L, 38L, 0L, 0L, 162L, 2L, 0L, 146L, 5L)), row.names = c(NA, 
-44L), class = "data.frame")

What I would like to do would be to have the hours laid out as they are but with the date as a secondary over arching label which which goes from 00:00 to 23:00 like so:

enter image description here

Is it possible to do this in ggplot2?

Thanks in advance for the help

Upvotes: 2

Views: 900

Answers (1)

Artem
Artem

Reputation: 3414

You can extract hours from Date column by hours function of lubridate package. Prelimenary datetime transformations could be done with this package as well. To put the hours data in a desired order you can use factor function with customized levels argument. Please see the code below:

library(lubridate)    
library(ggplot2)

June <- structure(list(Day = c("01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", "01/06/17", 
"01/06/17", "01/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", 
"02/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", "02/06/17", 
"02/06/17", "02/06/17", "02/06/17", "02/06/17"), Time = c("20:00", 
"20:00", "20:00", "21:00", "21:00", "21:00", "22:00", "22:00", 
"22:00", "23:00", "23:00", "23:00", "00:00", "00:00", "00:00", 
"01:00", "01:00", "01:00", "02:00", "02:00", "02:00", "03:00", 
"03:00", "03:00", "04:00", "04:00", "04:00", "05:00", "05:00", 
"05:00", "20:00", "20:00", "20:00", "21:00", "21:00", "21:00", 
"22:00", "22:00", "22:00", "23:00", "23:00", "23:00", "00:00", 
"00:00"), Date = c("01/06/17 20:00", "01/06/17 20:00", "01/06/17 20:00", 
"01/06/17 21:00", "01/06/17 21:00", "01/06/17 21:00", "01/06/17 22:00", 
"01/06/17 22:00", "01/06/17 22:00", "01/06/17 23:00", "01/06/17 23:00", 
"01/06/17 23:00", "01/06/17 00:00", "01/06/17 00:00", "01/06/17 00:00", 
"01/06/17 01:00", "01/06/17 01:00", "01/06/17 01:00", "01/06/17 02:00", 
"01/06/17 02:00", "01/06/17 02:00", "01/06/17 03:00", "01/06/17 03:00", 
"01/06/17 03:00", "01/06/17 04:00", "01/06/17 04:00", "01/06/17 04:00", 
"01/06/17 05:00", "01/06/17 05:00", "01/06/17 05:00", "02/06/17 20:00", 
"02/06/17 20:00", "02/06/17 20:00", "02/06/17 21:00", "02/06/17 21:00", 
"02/06/17 21:00", "02/06/17 22:00", "02/06/17 22:00", "02/06/17 22:00", 
"02/06/17 23:00", "02/06/17 23:00", "02/06/17 23:00", "02/06/17 00:00", 
"02/06/17 00:00"), Species = c("Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", 
"Soprano Pipistrelle", "Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", 
"Big bat sp.", "Common Pipistrelle", "Soprano Pipistrelle", "Big bat sp.", 
"Common Pipistrelle", "Soprano Pipistrelle"), Count = c(0L, 0L, 
0L, 20L, 0L, 0L, 85L, 0L, 1L, 9L, 0L, 0L, 7L, 0L, 0L, 2L, 0L, 
0L, 1L, 0L, 0L, 5L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
22L, 0L, 0L, 38L, 0L, 0L, 162L, 2L, 0L, 146L, 5L)), row.names = c(NA, 
-44L), class = "data.frame")

June$posix <- dmy_hm(June$Date)
June$Day_2 <- dmy(June$Day)
June$hour <- factor(hour(June$posix), levels = unique(hour(June$posix)))

ggplot(June, aes(x=hour, y=Count, fill=Species)) + 
  geom_bar(position='dodge', stat='identity') +
  theme(axis.text.x = element_text(vjust=0.5)) +
  xlab(paste0(range(June$Day_2), collapse = " - "))

Output: Species

Upvotes: 1

Related Questions