ggplot中组之间的不需要的行

时间:2016-04-29 13:50:45

标签: r ggplot2

我有几年的气候记录器数据,想要绘制一个月内每天的每日温度循环。我正在使用ggplot并按day

对数据进行分组

当我绘制一年的数据时,一切都很好。当我绘制多年的数据时,我会从23:00回到00:00。如果我使用facet_wrap,它可以工作,但我有多个网站,并希望按网站而不是年份。

enter image description here

clim2 <- structure(list(date = structure(c(1404172980, 1404176580, 1404180180, 
1404183780, 1404187380, 1404190980, 1404194580, 1404198180, 1404201780, 
1404205380, 1404208980, 1404212580, 1404216180, 1404219780, 1404223380, 
1404226980, 1404230580, 1404234180, 1404237780, 1404241380, 1404244980, 
1404248580, 1404252180, 1404255780, 1404259380, 1404262980, 1404266580, 
1404270180, 1404273780, 1404277380, 1404280980, 1404284580, 1404288180, 
1404291780, 1404295380, 1404298980, 1404302580, 1404306180, 1404309780, 
1404313380, 1404316980, 1404320580, 1404324180, 1404327780, 1404331380, 
1404334980, 1404338580, 1404342180, 1435708980, 1435712580, 1435716180, 
1435719780, 1435723380, 1435726980, 1435730580, 1435734180, 1435737780, 
1435741380, 1435744980, 1435748580, 1435752180, 1435755780, 1435759380, 
1435762980, 1435766580, 1435770180, 1435773780, 1435777380, 1435780980, 
1435784580, 1435788180, 1435791780, 1435795380, 1435798980, 1435802580, 
1435806180, 1435809780, 1435813380, 1435816980, 1435820580, 1435824180, 
1435827780, 1435831380, 1435834980, 1435838580, 1435842180, 1435845780, 
1435849380, 1435852980, 1435856580, 1435860180, 1435863780, 1435867380, 
1435870980, 1435874580, 1435878180), class = c("POSIXct", "POSIXt"
), tzone = "NMT"), value = c(-0.1, 0, 0, 0, 0, 0, 0, 0, 0.2, 
0.3, 0.7, 2.2, 2.6, 2.6, 3.3, 3, 1.9, 1.7, 1.1, 2.1, 0.7, 0.3, 
-0.3, -0.4, -0.3, -1, -0.9, -1, -1, -1.1, -1.2, -0.5, -0.6, -1.2, 
1.1, 3, 3.4, 4.5, 1.9, 1.9, 3.8, 3.4, 1.3, -0.1, 0.2, -0.6, -0.8, 
-0.9, -0.4, -0.3, -0.3, -0.3, -0.2, -0.3, -0.6, -0.8, -0.7, -1.1, 
1.2, 2.9, 1.9, 1.4, 1.7, 1.9, 1.6, 1.5, 0.9, 1.1, -0.5, -1.4, 
-1.2, -1.1, -1.6, -1.3, -1.4, -1.4, -1.5, -1.3, -1.3, -1.6, -1.9, 
-1.8, 0.9, 1.4, 0.9, 0.7, 0.4, -0.5, 0.1, 0.2, 0.1, -0.1, -0.6, 
-0.9, -0.9, -0.7)), .Names = c("date", "value"), row.names = c(NA, 
-96L), class = "data.frame")

library(ggplot2)
library(lubridate)
g <-ggplot(clim2, aes(x = hour(date) + minute(date)/60, y = value, colour = factor(year(date)), group = factor(day(date)))) + 
  geom_path() + 
  xlab("Time")
print(g)

1 个答案:

答案 0 :(得分:5)

如果你想删除这些行,你必须确保group包含一条路径的唯一值(粗略地说,某种非重叠的id),例如

clim2$year <- year(clim2$date)
clim2$day_id <- paste0(day(clim2$date), "_", clim2$year)
ggplot(clim2, aes(x = hour(date) + minute(date)/60, 
                  y = value, colour = factor(year), group = day_id)) + 
    geom_path() + 
    xlab("Time")

enter image description here