0

Problem - I am trying to plot a graph but wish to change the x axis and y axis values. With the help of stackoverflow, I understood how to use axis() to customize values but I am unable to get the required output. Whenever I run this below code, only one label on the x axis is being displayed. Here, x contains some numbers. Here is a screenshot enter image description here

Tries - I tried to give a character vector to the label field in the axis function, I tried changing Months to a data frame, nothing seems to help.

Goal - To display all the months of the year on x-axis and its corresponding data (Assuming that first element of vector x belongs to 1st month of the year and so on)

data <- data.frame( x= c(0,42,100,560,800, 900, 1000, 1200, 4800,5000, 5600, 6700, 8000, 12000) , 10, replace= TRUE)

plot(x, xaxt="n", type = "l")
xtick <- seq(1,12, by = 1)
Months <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec")
axis(side=1, at=xtick, labels = Months)

If someone could help me and guide where I am going wrong, it would be great

SSV
  • 149
  • 2
  • 12

1 Answers1

1

What about something like this ?

data <- data.frame( x= c(0,42,100,560,800, 900, 1000, 1200, 4800,5000, 5600, 6700, 8000, 12000))

plot(y =data$x, x = 1:length(data$x), type = "l", xaxt = "n")

Months <- as.vector(c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov","Dec", "Jan", "Feb"))
axis(1, at=1:length(data$x), labels= Months)

The answer can be found in here

Carles
  • 2,731
  • 14
  • 25