[英]Adding secondary x-axis label in ggplot2
I have produced this graph:我制作了这张图:
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:我想要做的是按原样布置时间,但将日期作为次要的拱形标签,从 00:00 到 23:00,如下所示:
Is it possible to do this in ggplot2?可以在 ggplot2 中做到这一点吗?
Thanks in advance for the help在此先感谢您的帮助
You can extract hours from Date
column by hours
function of lubridate
package.您可以通过
lubridate
包的hours
函数从Date
列中提取小时hours
。 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.要将小时数据按所需顺序排列,您可以使用带有自定义
levels
参数的factor
函数。 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 = " - "))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.