116

I have a question regarding the command plot().

Is there a way to fully eliminate the x-axis and replace it with own values? I know that I can get rid of the axis by doing

plot(x,y, xaxt = 'n')

and then add an axis with

axis(side = 1 etc.)

However, when I add the axis, obviously it still refers to the data plotted as 'x'. I would only like to plot the 'y'-values and add the x-axis my own in the sense of just "drawing" the x-axis with own values specified. Is there any way to do that?

The background of this question is that my two data frames differ in their length and therefore I cannot plot them.

Wael
  • 1,640
  • 1
  • 9
  • 20
Dani
  • 2,325
  • 5
  • 24
  • 20
  • Do you want to plot vectors with different lengths or just want to set the x label by yourself? Could you provide an example or give more information about the datset? – Manoel Galdino Mar 03 '11 at 15:48
  • You might also want to see how to combine two data frames together. You might be able to make more plots from your data and probably probably make the data more informative. – Sam Mar 03 '11 at 19:49
  • 2
    possible duplicate of [R: How do I set what plot() labels the x-axis with?](http://stackoverflow.com/questions/1395577/r-how-do-i-set-what-plot-labels-the-x-axis-with) – Blue Magister Jan 11 '14 at 04:57
  • possible duplicate of [R, change the spacing of tick marks on the axis of a plot?](http://stackoverflow.com/questions/3785089/r-change-the-spacing-of-tick-marks-on-the-axis-of-a-plot) – Waldir Leoncio Aug 29 '14 at 19:40
  • possible duplicate of [How to specify the actual x axis values to plot as x axis ticks in R](http://stackoverflow.com/questions/11775692/how-to-specify-the-actual-x-axis-values-to-plot-as-x-axis-ticks-in-r) – n0p Nov 13 '14 at 16:06

2 Answers2

204

Not sure if it's what you mean, but you can do this:

plot(1:10, xaxt = "n", xlab='Some Letters')
axis(1, at=1:10, labels=letters[1:10])

which then gives you the graph:

enter image description here

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks Tim! Not exactly what I was looking for but it helped my do what I wanted =) – Dani Mar 03 '11 at 15:20
  • 5
    x and y are the same length in this case. how do you draw n labels when the length of the other axis is m? – Colbert Sesanker Feb 24 '13 at 00:31
  • 3
    just for reference: the `xaxt="n"` option in the `plot` command suppresses the labeling of the x-axis. If this option is not included, `axis` will be a NOOP. – Steen Sep 20 '14 at 19:36
  • This works, but how do I rotate it so it is vertical? las=2 doesn't work. – runningbirds Jan 25 '16 at 19:29
  • I tried the solution. `xaxt = "n"` option worked for ts-class time series. But, in the case of multiple time series (mts-class) it did not work. – Erdogan CEVHER Oct 01 '17 at 21:13
  • if anyone is looking for the same problem in ggplot2 look at this [link](https://stackoverflow.com/questions/20529252/changing-x-axis-tick-labels-in-r-using-ggplot2). – snicz Aug 09 '20 at 07:26
21

You could set labels = FALSE inside axis(...) and then print the labels in a separate command using text(...). This option would allow you to rotate the text in case you need it.

lablist<-as.vector(c(1:10))
axis(1, at=seq(1, 10, by=1), labels = FALSE)
text(seq(1, 10, by=1), par("usr")[3] - 0.2, labels = lablist, srt = 45, pos = 1, xpd = TRUE)

Detailed explanation here

Image with rotated labels

user9869932
  • 6,571
  • 3
  • 55
  • 49