Plotting a line graph showing date by time in R - r

I have data that looks as follows:
Date Time_finished
4/3/2020 16:30:21
4/6/2020 16:43:29
4/7/2020 16:28:47
4/8/2020 16:30:38
4/9/2020 16:50:01
I would like to plot a line chart showing date across the x axis and then the time finished on the y axis, to show a time series graph. For some reason this does not seem to be working, the Date is saved as Date but time as a factor, does this also need to be a date?
I have tried normal plot but having no luck.
Thanks

Like this?
df <- tibble::tribble(
~Date, ~Time_finished,
"4/3/2020", "16:30:21",
"4/6/2020", "16:43:29",
"4/7/2020", "16:28:47",
"4/8/2020", "16:30:38",
"4/9/2020", "16:50:01"
)
library(tidyverse)
df %>%
mutate(Date = as.POSIXct(Date, format = "%m/%d/%y"),
Time_finished = as.POSIXct(Time_finished, format = "%H:%M:%S")) %>%
ggplot(aes(x = Date, y = Time_finished, group = 1)) +
geom_line() + scale_y_datetime(breaks = date_breaks("10 min"),
minor_breaks = date_breaks("2 min"),
labels = date_format("%Hh %Mm %Ss"))

Related

R ggplot geom_tile x axis year quarter labeling

I have some time series data with quarterly frequency, as below.
I'm using geom_tile to create a heatmap of these time series data, but the issue I have now is that the labeling on the x axis is defaulted to year eventhough the data is on quarterly.
My expectation was something like 2014 Q1, 2020 Q4 as in the dataset.
set.seed(1990)
ID <- rep(c('A','B','C'),each = 84)
n <- rep(round(runif(84,1,4)), 3)
datetime <- rep(seq(as.POSIXct("2014-01-01"), as.POSIXct("2020-12-01"), by="month"), 3)
df <- tibble(ID,n, datetime)
df <- df %>%
#mutate(yearweek = tsibble::yearweek(datetime)) %>%
mutate(yearquarter = zoo::as.yearqtr(datetime)) %>%
#group_by(ID, yearweek) %>%
group_by(ID, yearquarter) %>%
summarise(n = sum(n))
df
ggplot(df
,
aes(y=ID,x= yearquarter,fill=n))+
geom_tile(color = 'gray')
Normally I can easily control the monthly level dataset with scale_x_date as below but using it with quarterly data throws Error: Invalid input: date_trans works with objects of class Date only.
I'm using tsibble::yearweek to get weekly aggregation and zoo::as.yearqtr for quarterly aggregation.
But the issue is when it comes to plotting, ggplot may not support them. So is there a more consistent approach to dealing with time series data with multiple frequencies in R/ggplot?
scale_x_date(expand = c(0,0),breaks = seq(as.Date("2014-07-01"), as.Date("2020-12-01"), by = "1 month"), date_labels = "%Y %b", name = 'Monthly')
Since you have zoo's as.yearqtr variable use zoo's scale_x_yearqtr to format the x-axis.
library(ggplot2)
ggplot(df,aes(y=ID,x= yearquarter,fill=n))+
geom_tile(color = 'gray') +
zoo::scale_x_yearqtr(format = '%Y Q%q')

Change Labels of Time Axis using ggplot in R

I'm sure this is a relatively simple fix, but I can't figure it out for the life of me. I'm trying to plot a scatter plot for date and time information. Here is some sample code:
library(tidyverse)
library(lubridate)
library(hms)
time <- c("19:36:00", "18:20:00", "17:59:00", "17:22:00", "17:23:00")
date <- c("10-05-2019", "25-01-2019", "13-04-2019", "22-07-2019", "05-12-2019")
data <- data.frame(time = as_hms(as_datetime(time, format = "%H:%M:%S", tz = "America/Los_Angeles")), date = parse_date_time(date, "dmy", tz = "America/Los_Angeles"))
data %>%
mutate(time = as.POSIXct(time)) %>%
ggplot() +
geom_point(aes(x = date, y = time)) +
scale_y_datetime(
breaks = scales::date_breaks("1 hour"),
date_labels = "%l %p"
)
The result of this plot is a y-axis that corresponds to time in AM/PM format. The default here is about 4:30 PM to 8:30 PM. But, what if I wanted to change the limits of the y-axis to 4 PM to 10 PM? I've been combing through forums but I can't find anything that explicitly details this situation and the documentation only provides examples for doing this with date information.
Any help would be much appreciated!
You can set limits in scale_y_datetime :
library(dplyr)
library(ggplot2)
data %>%
mutate(time = as.POSIXct(time, format = "%T"),
date = as.Date(date, "%d-%m-%Y")) %>%
ggplot() +
geom_point(aes(x = date, y = time)) +
scale_y_datetime(
breaks = scales::date_breaks("1 hour"),
date_labels = "%l %p",
limits = c(as.POSIXct("16:00:00", format = "%T"),
as.POSIXct("22:00:00", format = "%T")))
data
time <- c("19:36:00", "18:20:00", "17:59:00", "17:22:00", "17:23:00")
date <- c("10-05-2019", "25-01-2019", "13-04-2019", "22-07-2019", "05-12-2019")
data <- data.frame(time, date)

R Time series line chart

I would like a record of coffee dispenses a line chart from the beginning of the recording to the end. I would like to determine the beverages purchased per day. The respective number per day should be displayed over the complete time span.
After I read in the data set I had first problems with the Date/Time format.
I suspect that I do not bring them into the correct format.
Then I tried to calculate the coffee consumption per day. Then I found out that with my formula it always uses the year 2020, although it goes back to 2019.
So my plot looks quite funny, and there is no year to be found.
Can someone please help me?
Thanks a lot!
Link to the Dataset: https://github.com/Skruff80/Getting-started/blob/master/ProductList.csv
coffeedata <- fread("C:/temp/ProductList.csv")
str(coffeedata)
head(coffeedata)
coffeedata$Date = as.Date(coffeedata$Date, "%d.%m.%y")
head(coffeedata)
countcoffee <- function(timeStamps) {
Dates <- as.Date(strftime(coffeedata$Date, "%Y-%m-%d"))
allDates <- seq(from = min(Dates), to = max(Dates), by = "day")
coffee.count <- sapply(allDates, FUN = function(X) sum(Dates == X))
data.frame(day = allDates, coffee.count = coffee.count)}
daylicounter = countcoffee(df$message.date)
lines(daylicounter)
The plot should look something like the picture in the link.
https://github.com/Skruff80/Getting-started/blob/master/example.png
Appreciate your help.
Use ggplot2
You can do that by using the ggplot2 library, like in this code:
library(ggplot2)
ggplot(daylicounter, aes(day, coffee.count)) +
geom_line(color = "orange", size = 1) +
scale_x_date(breaks = "1 month", date_labels = "%Y-%m-%d")
This is the output graph:
Edit
If you need a custom break vector, you can use a code like this:
library(ggplot2)
break.vec <- seq(from = min(daylicounter$day), to = max(daylicounter$day),
by = "month")
ggplot(daylicounter, aes(day, coffee.count)) +
geom_line(color = "orange", size = 1) +
scale_x_date(breaks = break.vec, date_labels = "%d-%m-%Y")
Hope this help.
Could fix the error with the X-axis. As I already suspected, reformatting the date was the error.
I had used
%y
instead of
%Y
coffeedata$Date = as.Date(coffeedata$Date, "%d.%m.%Y")

plotting x-axis by days in R

I have a sequence as follows
ts <- data.frame(seq.POSIXt(as.POSIXlt("2018-07-14 00:00"), as.POSIXlt("2018-07-16 13:52"), by="min"))
names(ts)[1]="Timestamp"
ts$Timestamp=format(ts$Timestamp, "%Y-%m-%d %H:%M")
values=rnorm(3713)
I am trying to generate a graph in r-bokeh such that the xaxis only displays the days (not the hours/minutes).
I have tried
figure() %>% ly_lines(ts, values) %>% x_axis(label = "Date", format = list(months = "%Y-%m", days = "%d"))
But it hangs. I have also tried days="%Y-%m-%d" but no sucess either.
Any thoughts on how I can generate a line plot for a time series, such that for the x-axis the formatting shows only the days rather than each minute.
I am open to a ggplot solution as well.
Thanks!
Here you go!
library(tidyverse)
ts <- data.frame(seq.POSIXt(as.POSIXlt("2018-07-14 00:00"), as.POSIXlt("2018-07-16 13:52"), by="min"))
names(ts)[1]="Timestamp"
ts$Timestamp=format(ts$Timestamp, "%Y-%m-%d %H:%M")
values=rnorm(3713)
plot_df <- cbind(ts, values) %>%
mutate(time = as.POSIXct(Timestamp, format = "%Y-%m-%d %H:%M"))
plot_df %>%
ggplot(aes(x = time, y = values)) +
geom_line()

How to creat a time vector excluding the system date when using R?

I want to creat a time vector which starts at 0:05:00 A.M and ends at 0:00:00 A.M the next day.The interval between each time spot is 5 minutes;
Then I want a y-t line plot with qplot().
Here is my R code:
t<-strptime('0:05:00','%H:%M:%S')+(0:287)*300
y<-rnorm(288,5,1)
qplot(t,y,geom = 'line')
the outcome is like this:
As you can see, the 't' is added with system date 'Aug 05'.What I want is 'hour : minute' only.
What should I do with my code?
Here is a solution using ggplot2 and POSIX formatting for dates which is easy to manipulate with ggplot:
df = data.frame(
t = seq(as.POSIXct("2016-01-01 05:00:00"), as.POSIXct("2016-01-02 00:00:00"), by = '5 min', tz = "Europe"),
y = rnorm(229,5,1))
ggplot(df, aes(t, y)) + geom_line() +
scale_x_datetime(labels = date_format('%H:%M', tz = "GMT"), breaks = date_breaks('2 hours'))
One suggestion is to manually set the tick labels. Note that in the snippet below, I amended slightly your code for t and y, so that they start and end at 0:00:00 (instead of starting at 0:05:00).
t <- strptime('0:00:00','%H:%M:%S')+(0:288)*300
y <- c(NA, rnorm(288,5,1))
tlabs <- format(t, "%H:%M")
breaks <- seq(1, 289, 72)
qplot(as.numeric(t),y,geom = 'line') +
scale_x_continuous(labels=tlabs[breaks], breaks=as.numeric(t)[breaks]) +
xlab("t")
Output:

Resources