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()
Related
I have a dataframe with timestamp column and one integer column with range 0-8.
I'd like to plot a marker for every data point, but plotly shows me a plot with x-axis starting in 1970.
When I convert integer column to factor, x-axis range is set correctly.
Any idea, how to set marker color using integer column and get proper range for x-axis?
Bad range:
library(plotly)
library(tidyverse)
library(lubridate)
set.seed(99)
tsRange = seq(ymd_hm("2021-01-01 00:00", tz = "CET"), ymd_hm("2021-01-08 23:59", tz = "CET"), by = "60 secs")
nRows <- 20
df <- tibble(
ts = sample(tsRange, nRows),
point = sample(1:8, nRows, replace = TRUE)
) %>%
mutate(point_fac = as.factor(point))
plot_ly(df, x = ~ts, y = "something", color = ~point) %>% add_markers()
good range
plot_ly(df, x = ~ts, y = "something", color = ~point_fac) %>% add_markers()
Regards
Paweł
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'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)
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 plot of daily volatility in R
The dates range from "2009-04-01" to "2010-07-01", and it is kept in that format in R.
When I plot it,
plot(d, vol1, type="l")
I only get the year 'labels' on the x axis. However, it would be helpful to have the month 'labels' as well. Can anyone help me with this?
Use the axis command to draw it manually.
Example 1
# test data
d <- seq(as.Date("2009-04-01"), as.Date("2010-07-01"), "day")
v <- seq_along(d)
plot(v ~ d, xaxt = "n")
# draw X axis
months <- seq(min(d), max(d), "month")
axis(1, months, format(months, "%Y\n%b"))
giving:
Example 2
# test data
d <- seq(as.Date("2009-04-01"), as.Date("2010-07-01"), "day")
v <- seq_along(d)
plot(v ~ d, xaxt = "n")
# draw X axis
months <- seq(min(d), max(d), "month")
lab <- format(months, "%b")
lab[lab == "Jan"] <- format(months, "%Y")[lab == "Jan"]
axis(1, months, lab)
giving:
You can use tidyverse tools to do this pretty simply, particularly scale_x_date option date_breaks. You can also look at other answers on here or web resources if you want to do additional customisation.
library(tidyverse)
library(lubridate)
df = tibble(
days = seq.Date(from = ymd("2009-04-01"), to = ymd("2010-07-01"), by = "day"),
value = rnorm(457)
)
ggplot(data = df) +
geom_line(aes(x = days, y = value)) +
scale_x_date(date_breaks = "1 month", date_labels = "%m-%y")