Adding x-axis labels to a time series plot

Hi, I have plotted a time series in R studio using the code:

PrecipDischarge.dat <- read.csv("PrecipDischargePreAndPost.csv") # precipitation and discharge time series from 2010 to 2022
precip <- PrecipDischarge.dat$Precipitation
precip
precip.ts <- ts(precip) 
plot(precip.ts)

The above code produces the correct time series plot, but has x-axis labels as "0", "200", "400", "600", "800", 1000" (as shown in the attached figure) instead of "2010", "2011", "2012", "2013", "2014", 2015", "2016", "2017", "2018", "2019", "2020", "2021", and "2022".

I then removed the x-axis labels using the code:

plot(precip.ts, xaxt = 'n')

I am now trying to add axis labels to show the years "2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022", using the code:

axis(1:12, at = c("2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"))

However, this code isn't adding any axis labels. Does anyone know what is going on here? How do I get the correct dates to display on my x-axis?

TIA

The at argument should be the numeric location of the labels, e.g. 200. Use the labels argument to set the text that you want displayed at each location.

I tried the labels argument to set the text "2010", "2011", "2012", .... "2022" at 12 locations along the x-axis using the code:

labels (1:12, at = c("2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"))

However, nothing is working, what am I doing wrong?

Try something like

axis(side = 1, at = seq(0, 1200, 100), labels = c("2010", "2011", "2012", "2013", "2014", "2015", "2016", "2017", "2018", "2019", "2020", "2021", "2022"))

The at values will have to be adjusted since I am not sure of the mapping between your x values and the date labels.

Thank you. That worked.

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.