r - Setting limits with scale_x_datetime and time data -
i want set bounds x-axis plot of time-series data features time (no dates). limits are:
lims <- strptime(c("03:00","16:00"), format = "%h:%m")
and ggplot prints fine, when add scale_x_datetime
scale_x_datetime(limits = lims)
i error: invalid input: time_trans works objects of class posixct only
fully reproducible example courtesy of how create time scatterplot r?
dates <- as.posixct(as.date("2011/01/01") + sample(0:365, 100, replace=true)) times <- as.posixct(runif(100, 0, 24*60*60), origin="2011/01/01") df <- data.frame( dates = dates, times = times ) lims <- strptime(c("04:00","16:00"), format = "%h:%m") library(scales) library(ggplot2) ggplot(df, aes(x=dates, y=times)) + geom_point() + scale_y_datetime(limits = lims, breaks=date_breaks("4 hour"), labels=date_format("%h:%m")) + theme(axis.text.x=element_text(angle=90))
the error message says should use as.posixct
on lims
. need add date (year, month , day) in lims
, because default `2015, off limits.
lims <- as.posixct(strptime(c("2011-01-01 03:00","2011-01-01 16:00"), format = "%y-%m-%d %h:%m")) ggplot(df, aes(x=dates, y=times)) + geom_point() + scale_y_datetime(limits =lims, breaks=date_breaks("4 hour"), labels=date_format("%h:%m"))+ theme(axis.text.x=element_text(angle=90))
Comments
Post a Comment