squishy
squishy

Reputation: 489

Controlling x-axis label format for time series plots - R

Is there a way force R to print time stamps as %m/%d/%Y on graphs? When I have short (<7 days) of data it prints as days of the week as below?

plot(station_171$datetime, station_171$stageheight, pch=ifelse(station_171$outcode ==122,24,21), col="black", bg=ifelse(station_171$outcode ==122,"red","NA")
     , xlab = "",xlim = c(min(station_101_7$datetime),max(station_101_7$datetime)), ylab = "")
legend("topright","171", bty = "n", cex=2)

enter image description here

Upvotes: 1

Views: 143

Answers (1)

squishy
squishy

Reputation: 489

You can format the axis using axis.POSIXct as seen below:

axis.POSIXct(1,station_171$datetime, format = '%Y-%m-%d %H:%S')

One should also make sure to include the xaxt='n' in the plot function to avoid double printing the x-axis labels.

Upvotes: 1

Related Questions