0
structure(list(Team = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "Union", class = "factor"), Date = structure(c(1L, 
1L, 1L, 2L, 2L, 2L, 4L, 3L, 3L, 4L, 3L, 3L, 5L, 3L, 3L, 6L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 6L, 3L, 3L, 3L, 3L, 3L, 3L, 6L, 6L, 6L, 
6L, 3L, 7L, 8L, 9L, 10L, 10L), .Label = c("2012-01-06", "2012-02-06", 
"2012-03-06", "2012-04-06", "2012-05-06", "2012-07-06", "2012-09-06", 
"2012-10-06", "2012-11-06", "2012-12-06"), class = "factor"), 
    STime = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L
    ), .Label = "07:03", class = "factor"), ETime = structure(c(6L, 
    7L, 8L, 5L, 5L, 1L, 2L, 3L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 11L, 
    10L, 9L, 8L, 10L, 7L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 
    5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L), .Label = c("01:13", 
    "03:13", "06:13", "09:13", "10:13", "11:13", "12:13", "13:13", 
    "15:13", "16:13", "18:13"), class = "factor")), .Names = c("Team", 
"Date", "STime", "ETime"), class = "data.frame", row.names = c(NA, 
-40L))

我正在这样做:

ggplot(df, aes(Date, ETime, group="Team")) + geom_point(size=0.3) + facet_wrap(~ Team)

我希望 y 轴从 00:00 到 23:29 以 2 小时为增量。我尝试了 scale_y_continous,但它不起作用。有什么建议么?

4

1 回答 1

2

我建议将您的日期和时间列更改为 POSIXt 格式的数据。然后更改轴中断和标记变得更容易。目前,您的日期和时间被存储为因素。

library(ggplot2)

# Change relevant columns from 'factor' to 'POSIXt'.
df$ETime = strptime(as.character(df$ETime), "%H:%M")
df$Date = strptime(as.character(df$Date), "%Y-%m-%d")

plot_1 = ggplot(df, aes(x=Date, y=ETime)) +
         geom_point() +
         labs(title="Plot 1")

# Manually set datetime limits and breaks.
y_limits = as.POSIXct(c(strptime("00:00", "%H:%M"), strptime("23:29", "%H:%M")))
y_breaks = seq(from=strptime("00:00", "%H:%M"), 
                 to=strptime("23:29", "%H:%M"), by="2 hours")
y_labels = format(y_breaks, "%H:%M")

plot_2 = ggplot(df, aes(x=Date, y=ETime)) + 
         geom_point() + 
         scale_y_datetime(limits=y_limits, breaks=y_breaks, labels=y_labels) +
         labs(title="Plot 2")

library(gridExtra)

png("plots.png", width=8, height=4, units="in", res=120)
grid.arrange(plot_1, plot_2, nrow=1)
dev.off()

在此处输入图像描述

于 2013-01-09T03:04:45.943 回答