简体   繁体   中英

Unwanted lines between groups in ggplot

I have climate logger data for several years and want to plot the daily temperature cycle for each day in one month. I am using ggplot and and grouping the data by day

When I plot data from a single year, everything is fine. When I plot data from multiple years, I get lines from 23:00 back to 00:00. If I use facet_wrap , it works, but I have multiple sites and want to facet by site not year.

在此输入图像描述

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)

If you want to remove these lines, you have to make sure that group contains unique value for one path (roughly speaking, some sort of non-overlapping id), eg

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")

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM