如何使用ggplot调整x轴上的时间?
How to adjust the time on x-axis with ggplot?
我正在尝试使用 ggplot 绘制(时间 VS 速度和降水),但我得到了一个混乱的图。数据在 (df) 下面,然后是我试过的代码。
df <- structure(list(Agency.Station.ID = c("MI270N003.6D", "MI270N004.7D",
"MI270N005.7D", "MI270N007.3D", "MI270N008.5D", "MI270N003.6D",
"MI270N004.7D", "MI270N005.7D", "MI270N007.3D", "MI270N008.5D",
"MI270N003.6D", "MI270N004.7D", "MI270N005.7D", "MI270N007.3D",
"MI270N008.5D", "MI270N003.6D", "MI270N004.7D", "MI270N005.7D",
"MI270N007.3D", "MI270N008.5D"), date_time = structure(c(1427846400,
1427846400, 1427846400, 1427846400, 1427846400, 1427846700, 1427846700,
1427846700, 1427846700, 1427846700, 1427847000, 1427847000, 1427847000,
1427847000, 1427847000, 1427847300, 1427847300, 1427847300, 1427847300,
1427847300), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Date = structure(c(16526, 16526, 16526, 16526, 16526, 16526,
16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526,
16526, 16526, 16526, 16526, 16526), class = "Date"), Time = c("00:00:00",
"00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:05:00",
"00:05:00", "00:05:00", "00:05:00", "00:05:00", "00:10:00",
"00:10:00", "00:10:00", "00:10:00", "00:10:00", "00:15:00",
"00:15:00", "00:15:00", "00:15:00", "00:15:00"), Speed = c(59,
34, 46, 61, 46, 58, 39, 51, 36, 52, 62, 47, 49, 57, 50, 59,
50, 35, 54, 44), Precipitation = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 20L
), class = "data.frame")
这是我试过的代码:
df <- df %>% gather(Aspect, Value, -Agency.Station.ID, -Date, -Time)
df$Agency.Station.ID <- factor(df$Agency.Station.ID)
df$Date <- factor(df$Date)
for(i in levels(df$Agency.Station.ID)) {
pdf(paste0(i,'.pdf'))
for(j in levels(df$Date)) {
subset.data <- df[which(df$Agency.Station.ID==i & df$Date==j),]
if (nrow(subset.data)!=0) {
p <- ggplot(data=subset.data, aes(Time, Value, group = 1)) +
geom_line(aes(color=Aspect)) +
labs(title=paste('Station:',i, " Date:",j)) +
theme_bw()
invisible(print(p))
}
}
dev.off()
}
我担心的是时间(我有 288 observations/day),因为它将在 x 轴上被压缩。
不是将 Date
Time
存储在单独的列中,而是可以组合:
df$Datetime <- as.POSIXct(paste0(df$Date, df$Time))
现在 ggplot
打印 x 轴上的所有标签,因为 Time
给出为 character
。 ggplot
调用函数以 prettify
轴标签,即打印信息性的、不重叠的标签。这需要 numeric
变量的变体,例如 R
的日期时间表示 POSIXct
.
PS:你的例子对我不起作用,缺少 Aspect
。
我正在尝试使用 ggplot 绘制(时间 VS 速度和降水),但我得到了一个混乱的图。数据在 (df) 下面,然后是我试过的代码。
df <- structure(list(Agency.Station.ID = c("MI270N003.6D", "MI270N004.7D",
"MI270N005.7D", "MI270N007.3D", "MI270N008.5D", "MI270N003.6D",
"MI270N004.7D", "MI270N005.7D", "MI270N007.3D", "MI270N008.5D",
"MI270N003.6D", "MI270N004.7D", "MI270N005.7D", "MI270N007.3D",
"MI270N008.5D", "MI270N003.6D", "MI270N004.7D", "MI270N005.7D",
"MI270N007.3D", "MI270N008.5D"), date_time = structure(c(1427846400,
1427846400, 1427846400, 1427846400, 1427846400, 1427846700, 1427846700,
1427846700, 1427846700, 1427846700, 1427847000, 1427847000, 1427847000,
1427847000, 1427847000, 1427847300, 1427847300, 1427847300, 1427847300,
1427847300), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Date = structure(c(16526, 16526, 16526, 16526, 16526, 16526,
16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526, 16526,
16526, 16526, 16526, 16526, 16526), class = "Date"), Time = c("00:00:00",
"00:00:00", "00:00:00", "00:00:00", "00:00:00", "00:05:00",
"00:05:00", "00:05:00", "00:05:00", "00:05:00", "00:10:00",
"00:10:00", "00:10:00", "00:10:00", "00:10:00", "00:15:00",
"00:15:00", "00:15:00", "00:15:00", "00:15:00"), Speed = c(59,
34, 46, 61, 46, 58, 39, 51, 36, 52, 62, 47, 49, 57, 50, 59,
50, 35, 54, 44), Precipitation = c(0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA, 20L
), class = "data.frame")
这是我试过的代码:
df <- df %>% gather(Aspect, Value, -Agency.Station.ID, -Date, -Time)
df$Agency.Station.ID <- factor(df$Agency.Station.ID)
df$Date <- factor(df$Date)
for(i in levels(df$Agency.Station.ID)) {
pdf(paste0(i,'.pdf'))
for(j in levels(df$Date)) {
subset.data <- df[which(df$Agency.Station.ID==i & df$Date==j),]
if (nrow(subset.data)!=0) {
p <- ggplot(data=subset.data, aes(Time, Value, group = 1)) +
geom_line(aes(color=Aspect)) +
labs(title=paste('Station:',i, " Date:",j)) +
theme_bw()
invisible(print(p))
}
}
dev.off()
}
我担心的是时间(我有 288 observations/day),因为它将在 x 轴上被压缩。
不是将 Date
Time
存储在单独的列中,而是可以组合:
df$Datetime <- as.POSIXct(paste0(df$Date, df$Time))
现在 ggplot
打印 x 轴上的所有标签,因为 Time
给出为 character
。 ggplot
调用函数以 prettify
轴标签,即打印信息性的、不重叠的标签。这需要 numeric
变量的变体,例如 R
的日期时间表示 POSIXct
.
PS:你的例子对我不起作用,缺少 Aspect
。