I have a very simple problem regarding plot settings.
I would like to have ticks (and labels on these ticks) on the y axis in a special way. For instance from 3 to 9, by one unit.
This is the code:
windows()
par(yaxp = c(3, 9, 7))
plot(1:10)
But it does not work. And I really do not understand why?
I also tried playing around with arguments from par() such as tck, tcl, yaxs, yaxt, yaxp and the function axis(). Which lead for example among others to the following codes:
windows()
par(yaxt = "n", yaxp = c(3, 9, 7))
plot(1:10)
or
windows()
par(yaxt = "n")
plot(1:10)
axis(2, at = c(3, 4, 5))
Unfortunately, I failed in every case...
Any ideas??
You've received some good solutions to your immediate problem, but I thought I'd answer the "Why?" portion. R's documentation in this instance is correct, but perhaps not as transparent as it could be.
If you examine the section of ?par describing xaxp, you'll find:
This parameter is reset when a user coordinate system is set up, for
example by starting a new page or by calling plot.window or setting
par("usr"): n is taken from par("lab").
Under yaxp, the same warning is not given, but instead it says simple 'See xaxp'. There is no similar warning for the usr parameter in ?par, but the same is probably the case, since if we look up ?plot.window we see:
A side-effect of the call is to set up the usr, xaxp and yaxp
So what's happening is that a call to plot eventually leads to calling plot.window, a side effect of which is to actually reset the settings for usr, xaxp and yaxp. Unless, of course, you have passed those arguments directly to a higher level function like plot that in turn hands them off down the line to plot.window.
Personally, I think this deserves mention in the Details section of ?par along with the list of parameters that can only be set by using par() rather than passing them to high level plotting functions.
I normally solve this by setting axes = FALSE in the call to plot() and then use axis() to draw the individual axes.
# no call to par() needed
plot(c(1:10), axes = FALSE)
axis(1) # x-Axis
ticks <- seq(3, 9, 1) # sequence for ticks and labels
axis(2, at = ticks, # y-Axis
labels = ticks)
box() # and a box around the plot
This works for me?
plot(1:10, yaxp = c(3, 9, 6))
Related
If you call function hist on r, you will note that the box that usually surrounds plotting region doesn't appear, instead, only rulers indicating plot scale appear on the bottom and on the left. If you use r a lot you may probably have noticed this already. my question is: there is some graphical parameter or workaround to make this happen on any other plot of basic r (like in a scatterplot, a line plot, a qq plot or whatever)?
The only parameter I found was axes, but setting it to FALSE makes it disappear not only the box, but also the rulers.
You are looking for box().
op <- par(mfrow=c(1, 2))
hist(mtcars$mpg, sub="w/o box")
hist(mtcars$mpg, sub="w/ box")
box() ## <-- this
par(op)
the answer is bty graphical parameter:
x= matrix(rnorm(100), ncol= 2)
plot(x, bty= 'n')
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:
call plot() or par(), specifying argument xaxt='n'
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.
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.
Hope this coding will helps you :)
plot(x,y,xaxt = 'n')
axis(side=1,at=c(1,20,30,50),labels=c("1975","1980","1985","1990"))
In case of plotting time series, the command ts.plot requires a different argument than xaxt="n"
require(graphics)
ts.plot(ldeaths, mdeaths, xlab="year", ylab="deaths", lty=c(1:2), gpars=list(xaxt="n"))
axis(1, at = seq(1974, 1980, by = 2))
I am having bother changing the font style and size on my two x-axes, both of which I have moved to the top of the graph.
Here is my code so far:
x1<-Temperature
x2<-Salinity
y<-Depth
par(mar=c(4, 4, 8, 4))
plot(x2,y, type="l",col="darkgrey",ylim=rev(range(0,300)),las=2,xlim=(range(32.5,34.5)),xaxt='n',xlab='',font.axis=2,lwd=3,ylab="Depth [m]",font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=4)
par(new=TRUE)
plot(x1,y, type="l",col="black",ylim=rev(range(0,300)),las=2,xaxt='n',xlab='',lwd=3,ylab='Depth [m]',font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=0)
par(new=TRUE)
This successfully changes my y-axis but leaves my x-axes unchanged.
Help!
Reading through the help for axis, ?axis, looking at the documentation for the ... parameters:
...: other graphical parameters may also be passed as arguments to
this function, particularly, ‘cex.axis’, ‘col.axis’ and
‘font.axis’ for axis annotation, ‘mgp’ and ‘xaxp’ or ‘yaxp’
for positioning, ‘tck’ or ‘tcl’ for tick mark length and
direction, ‘las’ for vertical/horizontal label orientation,
or ‘fg’ instead of ‘col’, and ‘xpd’ for clipping. See ‘par’
on these.
So just pass in your cex.axis etc parameters into the axis call as you did for plot. Here's a reproducible example (Note how I've made up the data, and even though the data is not realistic, at least it makes the example reproducible and still solves your problem):
x1 <- runif(10)
x2 <- runif(10) * 2 + 32.5
y <- runif(10) * 300
par(mar=c(4, 4, 8, 4))
plot(x2,y, type="l",col="darkgrey",ylim=rev(range(0,300)),las=2,xlim=(range(32.5,34.5)),xaxt='n',xlab='',font.axis=2,lwd=3,ylab="Depth [m]",font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
# added in various font/axis labels as in above
axis(side=3, line=4,font.axis=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
par(new=TRUE)
plot(x1,y, type="l",col="black",ylim=rev(range(0,300)),las=2,xaxt='n',xlab='',lwd=3,ylab='Depth [m]',font=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
axis(side=3, line=0,font.axis=2,font.lab=2,cex.lab=1.3,cex.axis=1.2)
(Your subsequent calls to axis where replacing the axis from the plot call, so instead of using the axis parameters from plot it uses the axis parameters from axis).
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:
call plot() or par(), specifying argument xaxt='n'
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.
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.
Hope this coding will helps you :)
plot(x,y,xaxt = 'n')
axis(side=1,at=c(1,20,30,50),labels=c("1975","1980","1985","1990"))
In case of plotting time series, the command ts.plot requires a different argument than xaxt="n"
require(graphics)
ts.plot(ldeaths, mdeaths, xlab="year", ylab="deaths", lty=c(1:2), gpars=list(xaxt="n"))
axis(1, at = seq(1974, 1980, by = 2))
I have a histogram from a list d of values that I make by simply typing
hist(d)
And this is what I get:
How can I make it such that the x-axis extends all the way left to the origin of this plot (the bottom left corner)? Why does it cut off at -0.4?
Macro's answer is by far the simplest route. However, if you really are unhappy with with the default behavior of hist (really, it's the default behavior of axis I suppose) you can always suppress the axes and draw them yourself:
set.seed(123)
d <- rnorm(1000)
hist(d,axes = FALSE)
axis(1,at = seq(-3,3,1),labels = TRUE,pos = 0)
axis(2,pos = -3)
As for the "why?", the defaults for drawing axes have to be set at something, and so there's a lot of code under there that tries pretty hard to ensure that the axis and tick labels are "pretty" according to the sensibilities of, well, whoever wrote it. In general, I think it does a good job, but of course not everyone agrees.
you can tweak the range of x using the xlim tag. For example, try
hist(d,xlim=c(-10,10))
Two suggestions:
#See if this is sufficient:
hist(...)
box()
#If not, try custom axes:
hist(..., xlim = c(-.5, .5), axes = F)
box()
axis(1, seq(-.5, .5, length = 6))
axis(2, seq(0, 30, by = 5))