Plotting date vs time using plotly in Jupyter-notebook - datetime

I have a data set whose format is given below
Date
Time
2-May-22
02:42:33 AM
3-May-22
01:05:41 AM
7-May-22
03:03:29 AM
Now I am plotting a line considering Date as x-axis and Time as y-axis
But when I'm using Time as string it is not being plotted as it should be. When I'm trying to convert it into datetime.time while plotting a random date (Jan 1,1900) is also being combined with it. What should I do.
For converting string to datetime.time I'm using this code:
time = datetime.datetime.strptime(datetime_string , "%H:%M:%S")

Related

How do I subset and manipulate time series data with lubridate and dplyr in Rstudio?

I have loaded in time series data of creek depth and am needing to calculate total annual values (Oct-April) for each year. The following is what I have tried thus far:
depth <- read_csv("Depth.Water_Depth-_All_Instruments#GCNP_-_Robber's_Roost_Spring.EntireRecord .csv")
The following is a screenshot of the resulting data frame
enter image description here
These are my attempts at making the timestamp column (ISO_UTC) into a date class. Although, each attempt makes all values for ISO_UTC into N/A values instead of dates / times.
ymd_hm(depth$ISO_UTC)
depth$ISO_UTC<- as.Date(depth$ISO_UTC, format = "%Y-%m-%dT%H:%M")
depth$ISO_UTC<- as.POSIXct(depth$ISO_UTC, format = "%Y-%m-%d %H:%M", tz = "US/Pacific")
Please help me to put these data into usable datetime values.
Please see the above details.

Weekly time series plot in R

I am trying to create a plot of weekly data. Though this is not the exact problem I am having it illustrates it well. Basically imagine you want to make a plot of 1,2,....,7 for for 7 weeks from Jan 1 2015. So basically my plot should just be a line that trends upward but instead I get 7 different lines. I tried the code (and some other to no avail). Help would be greatly appreciated.
startDate = "2015-01-01"
endDate = "2015-02-19"
y=c(1,2,3,4,5,6,7)
tsy=ts(y,start=as.Date(startDate),end=as.Date(endDate))
plot(tsy)
You are plotting both the time and y together as individual plots.
Instead use:
plot(y)
lines(y)
Also, create a date column based on the specifics you gave which will be a time series. From here you can add the date on the x-axis to easily see how your variable changes over time.
To make your life easier I think your first step should be to create a (xts) time series object (install/load the xts-package), then it is a piece of cake to plot, subset or do whatever you like with the series.
Build your vector of dates as a sequence with start/end date:
seq( as.Date("2011-07-01"), by=1, len=7)
and your data vector: 1:7
a one-liner builds and plots the above time series object:
plot(as.xts(1:7,order.by=seq( as.Date("2011-07-01"), by=1, len=7)))

How to produce a scatter plot of dates vs magnitudes in R?

This is what i have done so far but its wrong.
earthquakes<- c(6.6,6.8,8.4)
dates <- (13/02/2001 ,28/02/2001,23/06/2001)
plot(earthquakes,dates)
I have only started learning R. Please help.
earthquakes<- c(6.6,6.8,8.4)
dates <- as.Date(c("13/02/2001", "28/02/2001", "23/06/2001"), format="%d/%m/%Y")
plot(dates, earthquakes)
You had a few issues:
Dates should be in quotes (otherwise R will think you're trying to do arithmetic (i.e. 13 divided by 02 divied by 2001)
To convert dates to actual date objects, use as.Date, pass a vector of dates (this is the c(... part), and then specify the format that they are in so that R knows what to do with the strings
you had x and y swapped
Note, the as.Date step is not strictly necessary, but if you don't do that, then the x axis of the plot will plot every item equidistant, irrespective of how far apart the dates actually are in time.

R graphics plotting a linegraph with date/time horizontally along x-axis

I want to get a linegraph in R which has Time along x and temperature along y.
Originally I had the data in dd/mm/yyyy hh:mm format, with a time point every 30 minutes.
https://www.dropbox.com/s/q35y1rfila0va1h/Data_logger_S65a_Ania.csv
Since I couldn't find a way of reading this into R, I formatted the data to make it into dd/mm/yyyy and added a column 'time' with 1-48 for all the time points for each day
https://www.dropbox.com/s/65ogxzyvuzteqxv/temp.csv
This is what I have so far:
temp<-read.csv("temp.csv",as.is=T)
temp$date<-as.Date(temp$date, format="%d/%m/%Y")
#inputting date in correct format
plot(temperature ~ date, temp, type="n")
#drawing a blank plot with axes, but without data
lines(temp$date, temp$temperature,type="o")
#type o is a line overlaid on top of points.
This stacks the points up vertically, which is not what I want, and stacks all the time points (1-48) for each day all together on the same date.
Any advice would be much appreciated on how to get this horizontal, and ordered by time as well as date.

Plotting truncated times from zoo time series

Let's say I have a data frame with lots of values under these headers:
df <- data.frame(c("Tid", "Value"))
#Tid.format = %Y-%m-%d %H:%M
Then I turn that data frame over to zoo, because I want to handle it as a time series:
library("zoo")
df <- zoo(df$Value, df$Tid)
Now I want to produce a smooth scatter plot over which time of day each measurement was taken (i.e. discard date information and only keep time) which supposedly should be done something like this: https://stat.ethz.ch/pipermail/r-help/2009-March/191302.html
But it seems the time() function doesn't produce any time at all; instead it just produces a number sequence. Whatever I do from that link, I can't get a scatter plot of values over an average day. The data.frame code that actually does work (without using zoo time series) looks like this (i.e. extracting the hour from the time and converting it to numeric):
smoothScatter(data.frame(as.numeric(format(df$Tid,"%H")),df$Value)
Another thing I want to do is produce a density plot of how many measurements I have per hour. I have plotted on hours using a regular data.frame with no problems, so the data I have is fine. But when I try to do it using zoo then I either get errors or I get the wrong results when trying what I have found through Google.
I did manage to get something plotted through this line:
plot(density(as.numeric(trunc(time(df),"01:00:00"))))
But it is not correct. It seems again that it is just producing a sequence from 1 to 217, where I wanted it to be truncating any date information and just keep the time rounded off to hours.
I am able to plot this:
plot(density(df))
Which produces a density plot of the Values. But I want a density plot over how many values were recorded per hour of the day.
So, if someone could please help me sort this out, that would be great. In short, what I want to do is:
1) smoothScatter(x-axis: time of day (0-24), y-axis: value)
2) plot(density(x-axis: time of day (0-24)))
EDIT:
library("zoo")
df <- data.frame(Tid=strptime(c("2011-01-14 12:00:00","2011-01-31 07:00:00","2011-02-05 09:36:00","2011-02-27 10:19:00"),"%Y-%m-%d %H:%M"),Values=c(50,52,51,52))
df <- zoo(df$Values,df$Tid)
summary(df)
df.hr <- aggregate(df, trunc(df, "hours"), mean)
summary(df.hr)
png("temp.png")
plot(df.hr)
dev.off()
This code is some actual values that I have. I would have expected the plot of "df.hr" to be an hourly average, but instead I get some weird new index that is not time at all...
There are three problems with the aggregate statement in the question:
We wish to truncate the times not df.
trunc.POSIXt unfortunately returns a POSIXlt result so it needs to be converted back to POSIXct
It seems you did not intend to truncate to the hour in the first place but wanted to extract the hours.
To address the first two points the aggregate statement needs to be changed to:
tt <- as.POSIXct(trunc(time(df), "hours"))
aggregate(df, tt, mean)
but to address the last point it needs to be changed entirely to
tt <- as.POSIXlt(time(df))$hour
aggregate(df, tt, mean)

Resources