ggplot2中的小时标签不正确

时间:2016-07-26 17:20:24

标签: r ggplot2

请考虑以下事项:

temp <- c("13:30", "09:30", "09:30", "10:00", "13:30", "13:10", "10:00", "13:30", 
          "10:00", "13:30", "13:30", "10:00", "09:30", "13:30", "13:00", "14:00", 
          "09:00", "15:00", "12:00", "13:00", "13:00", "09:00", "09:00", "09:30", 
          "13:00", "13:00", "13:00", "14:00", "09:00", "13:30", "13:00", "13:30", 
          "13:30", "13:30", "13:15", "13:00", "13:30", "14:00", "10:30", "10:00", 
          "14:15", "09:00", "13:30", "13:00", "13:30", "10:30", "13:00", "08:30", 
          "09:00", "09:30", "13:00", "10:00", "10:00", "13:00", "13:30", "09:20", 
          "13:15", "10:30", "14:00", "13:00", "13:00", "13:00", "09:00", "13:30", 
          "13:30", "13:00", "09:30", "09:00", "13:30", "09:30", "13:00", "10:00", 
          "13:00", "13:00", "10:30", "14:45", "13:00", "13:00", "13:00", "13:30", 
          "13:45", "10:00", "13:00", "13:30", "10:15", "13:00", "13:30", "14:00", 
          "09:00", "09:15", "10:30", "14:00")
temp <- strptime(temp, format = "%H:%M")
temp <- as.data.frame(temp)

library(ggplot2)
library(scales)

ggplot(data = temp) + 
  geom_histogram(aes(x = temp), fill = "#228b22", binwidth = 1800, 
                 breaks = seq(min(as.numeric(temp$temp)) - 1800,
                              max(as.numeric(temp$temp)) + 1800, 
                              by = 1800)) + 
  scale_x_datetime(breaks = date_breaks("1 hour"), date_labels = "%r") +
  scale_y_continuous(breaks = seq(from = 0, to = 28, by = 4)) + 
  theme(panel.background = element_rect(fill='white'),
        panel.border=element_rect(fill=NA),
        panel.grid.major=element_line(color="black"),
        panel.grid.minor=element_line(color="black"), 
        axis.text.x = element_text(angle = 90))

上面给出的时间是军事时间。如你所见,它们主要落在早上 - 午后的范围内。因此,这个输出没有任何意义:

enter image description here

请注意,x轴给出的时间是深夜,而早上没有。

0 个答案:

没有答案