Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in R Programming by (50.2k points)

I have used the following code in my program and I just want to get a plot but I am getting a plot with values 1 to 30 below as I have given in the line but I want to replace them with Jan, Feb, Mar,.... How can I do that?

The code is:

plot(NULL,xlim=c(0,30),ylim = c(0,350),xlab = "months",ylab = "values in mm",main = "procedure") 

1 Answer

0 votes
by (107k points)

In R programming you can simply use the built-in vector that the RStudio provides for abbreviated months, month.abb and then after that just perform the indexing on these with the sequence of your x-axis tick labels, using modulus 12, %% 12. First, turn the x-axis off (xaxt="n").

plot(NULL,xlim=c(0,30), ylim=c(0,350), xlab="months", ylab="values in mm", main="procedure", xaxt="n")

And then create the desired labels.

(xlabs <- paste(month.abb[1+seq(0,30,5) %% 12])

# [1] "Jan" "Jun" "Nov" "Apr" "Sep" "Feb" "Jul"

The above labels only have the months. If you want to add the years, then that's also possible, perhaps underneath if space is an issue. For example, using paste0.

xlabs <- paste0(xlabs, "\n", c(rep(2018,3), rep(2019,2), rep(2020,2)))

axis(side=1, at=seq(0,30,5), labels=xlabs, cex.axis=0.8)

 enter image description here

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...