使用 R 中 ggplot2 中的 scale_x_datetime 函数处理标签

dealing with the Lables using the function of scale_x_datetime in ggolot2 in R

我有一些问题,看起来很简单,但是我在使用scale_x_datetime的函数时想不通。请看下图:

我想在图中得到什么:

1) x 标签只带时间(例如:00:00:00) 2)x标签的第一个值应该是00:00:00

对于第一点,我使用 scale_x_datetime(date_labels = "%X") 仅从 date_time.但后来我得到了下面的图表:

该线看起来与第一张图一样正确,但响应的 x 轴值是错误的。

我想得到的第二点,我尝试使用scale_x_datetime中的函数限制,但它根本不起作用

有人对此有提示吗?

非常感谢

注:

生成数据table:

dd <- data.table(date = c("2015-07-01 00:15:00", "2015-07-01 00:30:00", "2015-07-01 00:45:00","2015-07-01 01:00:00", "2015-07-01 01:15:00","2015-07-01 01:30:00","2015-07-01 01:45:00","2015-07-01 02:00:00","2015-07-01 02:15:00", "2015-07-01 02:30:00"),value = c(1.83,1.68,1.29,14.23,0.96, 1.29,10.4,8.25,6.77,7.66))

dd$date<-as.POSIXct(dd$date)

对于第一张图:我使用了以下代码:

p22 <- ggplot(dd, aes(dd$date, dd$value))+labs(x="Time", y="seconds")
p22 <-p22 + geom_line(colour="dark blue",size=1) 
p22<- p22+scale_x_datetime(date_breaks  = "15 mins") + theme(axis.text.x = element_text(angle = 90, hjust = 1))

看起来不错,但我只想显示时间,所以我添加了代码 date_labels = "%X" 以从中获取时间值date_time : 下面:

p22 <- ggplot(dd, aes(dd$date, dd$value))+labs(x="Time", y="seconds")
p22 <-p22 + geom_line(colour="dark blue",size=1) 
p22<- p22+scale_x_datetime(date_breaks  = "15 mins",date_labels = "%X") + theme(axis.text.x = element_text(angle = 90, hjust = 1))

然后,我得到了错误,因为第二张图中的响应 x 值是错误的。这可能是时区的问题,但不确定。

您可以使用 scale_x_date() 相应地缩放日期轴。

例如

scale_x_date(date_breaks = "1 week", date_minor_breaks = "1 day", date_labels = "%b %d")

将显示每周的标签,而每天显示为次要点。

limits 参数可用于定义日期范围的限制。

注意:由于示例代码在原始问题中不可用,所以我没有尝试获得您想要的结果。不过用这个方法应该可以得到想要的结果。

确保您的日期时间值具有明确的时区:

dd <- data.frame(date = c("2015-07-01 00:15:00", "2015-07-01 00:30:00", 
                          "2015-07-01 00:45:00","2015-07-01 01:00:00", 
                          "2015-07-01 01:15:00","2015-07-01 01:30:00",
                          "2015-07-01 01:45:00","2015-07-01 02:00:00",
                          "2015-07-01 02:15:00", "2015-07-01 02:30:00"),
                 value = c(1.83,1.68,1.29,14.23,0.96, 1.29,10.4,8.25,6.77,7.66))

#you should always define the time zone    
dd$date <- as.POSIXct(dd$date, tz = "GMT")

ggplot(dd, aes(date, value))+
  labs(x="Time", y="seconds") +
  geom_line(colour="dark blue",size=1) +
  scale_x_datetime(date_breaks  = "15 mins",date_labels = "%X", 
                   limits = as.POSIXct(c("2015-07-01 00:00:00", 
                                         "2015-07-01 03:00:00"), 
                                       tz = "GMT")) + 
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

PS:切勿在 aes 中使用 $。 ggplot2 在传递给其 data 参数的 data.frame 内部查找变量。由于它有时会重新排序 data.frame 如果您在 aes.

中引用 ggplot2 外部的数据,您可能会得到意想不到的结果

添加 vjust = 0.5 以在任何一种情况下获得您想要的。

library(dplyr)
library(ggplot2)

dd = data_frame(
  date = c("2015-07-01 00:15:00", "2015-07-01 00:30:00", "2015-07-01 00:45:00","2015-07-01 01:00:00", "2015-07-01 01:15:00","2015-07-01 01:30:00","2015-07-01 01:45:00","2015-07-01 02:00:00","2015-07-01 02:15:00", "2015-07-01 02:30:00"),
  value = c(1.83,1.68,1.29,14.23,0.96, 1.29,10.4,8.25,6.77,7.66)
  ) %>% 
  mutate(
    as.POSIXct(date)
  ) %>% 
  ggplot(aes(x = date, y = value)) + 
  labs(x = "Time", y = "seconds") +
  geom_line(colour = "dark blue", size = 1) + 
  scale_x_datetime(date_breaks  = "15 mins") + 
  theme_bw() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5))