在ggplot2中添加辅助x轴标签

时间:2018-02-21 00:45:29

标签: r ggplot2

我制作了这张图:

enter image description here

使用以下代码生成:

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

数据样本如下:

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

我想做的是将时间安排在原样,但将日期作为次要的拱形标签,从00:00到23:00,如下:

enter image description here

是否可以在ggplot2中执行此操作?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:1)

您可以通过Date软件包的hours函数从lubridate列中提取小时数。初步的日期时间转换也可以使用此程序包完成。要将小时数据按所需顺序排列,可以使用带有自定义factor参数的levels函数。请参见下面的代码:

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

输出: Species