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)
Related
consider plot below ( data is from the useful link : How to create custom date labels using ggplot with scale_x_date )
start_date <- as.Date('2020-08-08')
end_date <- as.Date('2021-05-10')
# tibble with test data
df <- tibble( dates = as.Date(as.character(seq(start_date, end_date, by = 4) ) ),
yval = as.numeric(strftime(dates, format = "%d")),
# following two lines just to give some color to the plot
month = strftime(dates, format = "%m") ) %>%
group_by( month )
df
ggplot(df, aes( x=dates, y=yval, color = month ) ) +
geom_point() +
scale_x_date(date_labels = "%b %d", breaks = "1 month")
what I want is NOT to have the 2nd, the 4th , the 6th,... labels on the xaxis.
I tried "2 month" for breaks but it does the opposite and removes the 1st , 3d, 5th, ... labels.
any ideas how to do this ( preferably without manually specifying the labels one by one )
I tried to get this passing a function to breaks but that didn't work, so I made the sequence directly using start_date and end_date:
ggplot(df, aes( x=dates, y=yval, color = month ) ) +
geom_point() +
scale_x_date(
date_labels = "%b %d",
breaks =
seq(from = floor_date(start_date, "month"), to = end_date, by = "2 months"))
)
Another option is setting limits in scale_x_date where you set the month before your first date because your first date is later and then it will start breaking from the first date instead of the second date. Here is a reproducible example:
library(tibble)
library(dplyr)
start_date <- as.Date('2020-08-08')
end_date <- as.Date('2021-05-10')
# tibble with test data
df <- tibble( dates = as.Date(as.character(seq(start_date, end_date, by = 4) ) ),
yval = as.numeric(strftime(dates, format = "%d")),
# following two lines just to give some color to the plot
month = strftime(dates, format = "%m") ) %>%
group_by( month )
library(ggplot2)
ggplot(df, aes( x=dates, y=yval, color = month ) ) +
geom_point() +
scale_x_date(date_labels = "%b %d", breaks = "2 month", limits = c(as.Date("2020-07-01"), NA))
Created on 2022-08-23 with reprex v2.0.2
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')
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"))
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()
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: