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)
我希望从00:00到23:29有y轴,增量为2小时。我尝试了scale_y_continous,这是行不通的。有什么建议吗?
答案 0 :(得分: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()