Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the actual x axis values to plot as x axis ticks in R

Tags:

plot

r

I am creating a plot in R and I dont like the x axis values being plotted by R.

For example:

x <- seq(10,200,10) y <- runif(x)  plot(x,y) 

This plots a graph with the following values on the X axis:

50, 100, 150, 200

However, I want to plot the 20 values 10,20, 30 ... 200 stored in variable x, as the X axis values. I have scoured through countless blogs and the terse manual - after hours of searching, the closest I've come to finding anything useful is the following (summarized) instructions:

  1. call plot() or par(), specifying argument xaxt='n'
  2. call axis() e.g. axis(side = 1, at = seq(0, 10, by = 0.1), labels = FALSE, tcl = -0.2)

I tried it and the resulting plot had no x axis values at all. Is it possible that someone out there knows how to do this? I can't believe that no one has ever tried to do this before.

like image 577
Homunculus Reticulli Avatar asked Aug 02 '12 10:08

Homunculus Reticulli


People also ask

How do you specify the x axis in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

Which function would you use to set the ticks for X axis of the plot?

xticks( ticks ) sets the x-axis tick values, which are the locations along the x-axis where the tick marks appear. Specify ticks as a vector of increasing values; for example, [0 2 4 6] . This command affects the current axes.

How do I customize axis in R?

You can create custom axes using the axis( ) function. axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...) the coordinate at which the axis line is to be drawn. If you are going to create a custom axis, you should suppress the axis automatically generated by your high level plotting function.


2 Answers

You'll find the answer to your question in the help page for ?axis.

Here is one of the help page examples, modified with your data:

Option 1: use xaxp to define the axis labels

plot(x,y, xaxt="n") axis(1, xaxp=c(10, 200, 19), las=2) 

Option 2: Use at and seq() to define the labels:

plot(x,y, xaxt="n") axis(1, at = seq(10, 200, by = 10), las=2) 

Both these options yield the same graphic:

enter image description here


PS. Since you have a large number of labels, you'll have to use additional arguments to get the text to fit in the plot. I use las to rotate the labels.

like image 152
Andrie Avatar answered Sep 30 '22 15:09

Andrie


Take a closer look at the ?axis documentation. If you look at the description of the labels argument, you'll see that it is:

"a logical value specifying whether (numerical) annotations are  to be made at the tickmarks," 

So, just change it to true, and you'll get your tick labels.

x <- seq(10,200,10) y <- runif(x) plot(x,y,xaxt='n') axis(side = 1, at = x,labels = T) # Since TRUE is the default for labels, you can just use axis(side=1,at=x) 

Be careful that if you don't stretch your window width, then R might not be able to write all your labels in. Play with the window width and you'll see what I mean.


It's too bad that you had such trouble finding documentation! What were your search terms? Try typing r axis into Google, and the first link you will get is that Quick R page that I mentioned earlier. Scroll down to "Axes", and you'll get a very nice little guide on how to do it. You should probably check there first for any plotting questions, it will be faster than waiting for a SO reply.

like image 39
nograpes Avatar answered Sep 30 '22 15:09

nograpes