Plotting a day (no date) in ggplot2
I want to calculate a numeric vector against daytime (% H:% M) in ggplot2.
I understand that
as.POSIXct(dat$daytime, format = "%H:%M")
is the way to format my timedata, but the output vector will still contain the date (today's date). Therefore, the ticks of the axis will include the date (March 22).
ggplot(dat, aes(x=as.POSIXct(dat$daytime, format = "%H:%M"), y=y, color=sex)) +
geom_point(shape=15,
position=position_jitter(width=0.5,height=0.5))
Is there a way to get rid of the date alltogether, especially on the axis of the graph? (All the information I found on the message boards seems to refer to older versions of ggplot with now defunct date_format arguments)
source to share
You can provide a function to a parameter, labels
scale_x_datetime()
or use a parameter date_label
:
# create dummy data as OP hasn't provided a reproducible example
dat <- data.frame(daytime = as.POSIXct(sprintf("%02i:%02i", 1:23, 2 * (1:23)), format = "%H:%M"),
y = 1:23)
# plot
library(ggplot2)
ggplot(dat, aes(daytime, y)) + geom_point() +
scale_x_datetime(labels = function(x) format(x, format = "%H:%M"))
EDIT . Or, even more concise, you can use a parameter date_label
(thanks to aosmith for the suggestion ).
ggplot(dat, aes(daytime, y)) + geom_point() +
scale_x_datetime(date_label = "%H:%M")
source to share