0

I want to change the values of the x-axis from the default ones, to the ones given by me.

Currently I have the following values:

0, 10, 20, 30, 40

But I want to have all the values from 1 to 45, inclusive:

1,2,3,4,5,...,45

How can I do this?

This is what I have so far:

v <- c(5:49)
plot(v,type="o",col="blue",xlab="Instance",ylab="Ratio",pch=0,lty=2,xaxt="n")
axis(side=1,at=seq(1,45,1))
lines(c(10:20),col="green",type="o",pch=5,lty=3)

This is the plot: Plot example

user1956185
  • 389
  • 4
  • 8
  • 16
  • 1
    What plotting function are you using? When asking a question, please include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – MrFlick Jun 25 '14 at 13:02
  • Have you tried `axis` function ? – Ashwin Jun 25 '14 at 13:05
  • In accord with @Ashwin, you will need to suppress the default x-axis values with the `xaxt = "n"` argument in `plot()`. Then utilize the `axis()` function to input your own x-axis values. – ccapizzano Jun 25 '14 at 13:12
  • @MrFlick: I have edited the question to contain the code; @ccapizzano: I have used `xaxt = "n"` but which values to add to the `axis()` function? I tried `axis(side=2,at=seq(1,45,1))` and one time it worked but the values looked strange and another time it didn't work. – user1956185 Jun 25 '14 at 13:17
  • @user1956190, can you please provide a reproducible example as mentioned earlier by @MrFlick. Also `axis(side = 2...)` is creating interval values on the Y-axis. Please view the documentation for `?axis` to better understand each argument. – ccapizzano Jun 25 '14 at 13:57
  • @ccapizzano: I have made some changes to my questions. – user1956185 Jun 25 '14 at 14:19
  • 1
    What you have is the right code for it. To display all the axis labels you can just stretch the plot and it will show all of them (i.e. it is an issue of printing it on the screen rather than creating it). Also, `seq(1, 45, 1)` can be replaced by 1:45 for simplicity. – konvas Jun 25 '14 at 15:07
  • @konvas: and how can I stretch the plot? – user1956185 Jun 25 '14 at 15:21
  • Are you printing this on screen? If yes, then just enlarge the window like any other window. If you are printing to a file, e.g. pdf, just use the height and width arguments. – konvas Jun 25 '14 at 15:50
  • @konvas: I found from where to change the height and width arguments. Thank you. – user1956185 Jun 25 '14 at 15:51

1 Answers1

2

As others have pointed out in comments, you will need to use axis. The difficulty here is to find the right options, to achieve cramming all the axis labels. I suggest:

  • cex.axis= will let you chose the font size.
  • las=2 will make the labels perpendicular to the axis

Here is the code with these additions:

v <- c(5:49)
plot(v,type="o",col="blue",xlab="Instance",ylab="Ratio",pch=0,lty=2,xaxt="n")
axis(side=1,at=seq(1,45,1),cex.axis=0.75,las=2)
lines(c(10:20),col="green",type="o",pch=5,lty=3)

And the result:enter image description here

Jealie
  • 6,157
  • 2
  • 33
  • 36