0

I wanna plot my data in an interval between 18:00 pm and 7:00 am ( date as x axis) during a week.

I wanna plot my data along the intervals 18:00 pm and 7:00 am. I've got data gathered during a week every 5 minutes and I've tried to filter out the desired interval but I can't come out with the expected outcome. This is what I've done so far: (I'm new to R by the way)

class(Marzo$date)
Marzo$hms <- format(as.POSIXct(Marzo$date), "%H:%M:%S")
data1 <- Marzo %>%  filter(hms >="18:00:00" & hms <="7:00:00") %>% print(n=250) 
ggplot() +
  geom_line(data= data1, aes(x= date, y= RH3000, colour= "RH3000"), 
                             lwd=1) +
  geom_line(data= data1, aes(x= date, y= RH4000, colour= "RH4000"),
                             lwd=1) +
  scale_colour_manual("", breaks=c("RH3000","RH4000"), 
                      values=c("blue4","turquoise1")) +
  scale_x_datetime(labels= date_format("%m-%d %H:%M"), 
                   date_breaks= "12 hours")

When I apply that filter this is what I get:

 2023-03-13 23:45:00          
 2023-03-13 23:50:01       
 2023-03-13 23:55:00        
 2023-03-14 18:00:00        
 2023-03-14 18:05:01         

Once it gets to 00:00 pm it starts again with the hour 18:00 pm of the next day instead of carrying out till getting till 7:00 am of the next day. Sorry if I was a bit misleading buy Im not English. Any kind of help would come in handy.

Also it would be great to get rid of the straight lines that show up between the non-selected intervals. Graphic:

enter image description here

benson23
  • 16,369
  • 9
  • 19
  • 38

1 Answers1

0

The issue with your code may be that you are treating the date and time separately, which can lead to unexpected results when filtering or plotting. Instead, you can combine the date and time into a single datetime variable using the as.POSIXct() function, which will allow you to filter and plot the data more accurately.

Here's an updated version of your code that may work: (i coudnt try because i dont have any data...) next time please paste a minimal reproducable sample.

# Convert date to datetime format
Marzo$date <- as.POSIXct(Marzo$date)

# Filter data for desired time interval
data1 <- Marzo %>% filter(format(date, "%H:%M:%S") >= "18:00:00" | format(date, "%H:%M:%S") <= "07:00:00")

# Plot data using datetime variable
ggplot(data1, aes(x = date, group = 1)) +
  geom_line(aes(y = RH3000, color = "RH3000"), size = 1) +
  geom_line(aes(y = RH4000, color = "RH4000"), size = 1) +
  scale_color_manual("", breaks=c("RH3000", "RH4000"), values=c("blue4", "turquoise1")) +
  scale_x_datetime(labels = date_format("%m-%d %H:%M"), date_breaks = "12 hours")

In this code, we first convert the date column to a datetime format using as.POSIXct(). Then, we use filter() to select the data between 7:00 pm and 7:00 am the next day. Note that we use the | operator instead of & to account for times after midnight. Finally, we plot the data using the date variable and group by a single group to avoid issues with the x-axis scale.

Edit: (to answer your comment :) The group parameter in the aes() function is used to group data points together based on a categorical variable. In this case, group = 1 is used to group all the data points together, as there is no categorical variable specified. This is important when using the geom_line() function to plot a line graph, as it tells the function which points to connect with lines.

Without specifying group = 1, ggplot may try to group the data points differently, leading to unexpected behavior in the plot.

The group parameter does not affect the x-axis, and should not cause any problems with it. The x-axis is controlled by the scale_x_datetime() function, which sets the date format and breaks for the axis.

Eirik Fesker
  • 328
  • 1
  • 12
  • Thank you so much for taking your time to help me. I appreciate that. I did come up with a solution using the package lubridate, I created another column called hour and I used the command mutate(hour= hour(date)), then I filter the hours that I wanted. I'll take into account your great solution. But I still can't quite understand the function of group=1. What does it exactly do? Why can it cause problems to the x-axis? Thank you. – Gabriel Bernabé García Mar 27 '23 at 22:17
  • i edited my post. of course - lubridate is a very good extension of posixct. feel free to use the packages which make your coding the most comfortable :) – Eirik Fesker Mar 28 '23 at 19:50
  • Thanks for the added explanation. Best of wishes. – Gabriel Bernabé García Mar 29 '23 at 20:06