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:
plot()
or par()
, specifying argument xaxt='n'
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.
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.
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.
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.
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With