【发布时间】:2020-08-31 06:55:23
【问题描述】:
我正在尝试使用 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()
}
【问题讨论】:
-
我们可以将 x 轴上的标签更改为 (1:24) 之间的特定数字,即一天中的小时数吗?
-
您在这里没有提供完全可重复的示例,所以我无法测试答案,但我会考虑使用
scale_x_date或scale_x_datetime自定义刻度位置和标签地块。
标签: r ggplot2 plot time visualization