how to align axis in R so that two axes intersects - r

For example, I have the following simple command:
x<-rnorm(2000, 0, 30)
hist(x)
But the graph shows a gap between the line y=0 and the x-axis. I want it to be shown in the normal format, where the two axes touch on each other at a particular point (x0, y0) which I can specify arbitrarily. What is the option in R to do that?
Thank you.

One way is to plot the x-axis separately and use line to align it with the 0 coordinate.
loc <- hist(x, xaxt="n",bty="l")
axis(1, at=loc$breaks,line=-.75)

I think the easiest way to do this is simply to draw it using box, since plot.histogram skips much of the plotting setup that would allow you to pass the appropriate par settings directly:
x<-rnorm(2000, 0, 30)
hist(x)
box(bty = "l")
See the section in par on bty for the possible options.

You can specify xlim and ylim in hist.
Check
?hist
AND
hist(x, xlim = c(-100, 100), ylim = c(0, 500))

Related

Put tick labels of only x-axis inside plotting area

Is it possible to put tick labels of only x-axis inside plotting area?
I am trying:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tck=.01)
It seems that:
par(mgp=c(0,-1.4, 0))
puts both x and y tick labels inside plotting area.
Why don't you just draw the ticks where you want them using the pos argument to axis():
plot(0:72, xaxt="n")
text(0:71, -1, rep(0:23, 3), cex = 0.5)
axis(1, at=c(0:71), NA, cex.axis=.7, font=1, tck=.01)
I think the best and easy solution is the parameter tcl in axis or par.
Positive values put the marks inside, negative outside, the value is the length.
Your example:
axis(1,at=c(0:71),c(rep(0:23,3)),cex.axis=.7, font=1,tcl=0.3)

Increasing axis ranges

I have a plot that I have added a second line to using lines() function but the added line does not fit within the default axis span so I want to increase the range of both axis so the whole of the new line fits. The code for the graph is the following:
plot(spa8, ci.type="line", col="black", lwd=2, ci.lty=2, ci.col="black",
ylab = "Species Richness", yaxp = c(0,60,6), xaxp = c(0,200,10),
cex.axis = 0.75, frame.plot=FALSE, xvar = "individuals") +
lines(spa7, ci.type="line", col="grey", lwd=2, ci.lty=2, ci.col="grey",
xvar = "individuals")+
box(bty="L")
I have tried using usr() and xaxp()but with no luck, I have also tried to set my own axis using the axis() function but that hasn't worked either. I'm sure I must be doing something wrong here as it seems like something that should be simple to do!
Can someone help me out?
Thanks
Perhaps you could play around with the xlim and ylim arguments within the plot function. For example, try running the code below (line by line) and look at the difference.
## Default.
plot(1:10)
## Set x-axis to be between -10 and 10.
plot(1:10, xlim = c(-10, 10))
## Set x-axis to be between -10 and 10, and y-axis to be between -5 and 5.
plot(1:10, xlim = c(-10, 10), ylim = c(-5, 5))
Hope that helps :)

How to plot the value of abline in R?

I used this code to make this plot:
plot(p, cv2,col=rgb(0,100,0,50,maxColorValue=255),pch=16,
panel.last=abline(h=67,v=1.89, lty=1,lwd=3))
My plot looks like this:
1.) How can I plot the value of the ablines in a simple plot?
2.) How can I scale my plot so that both lines appear in the middle?
to change scale of plot so lines are in the middle change the axes i.e.
x<-1:10
y<-1:10
plot(x,y)
abline(a=1,b=0,v=1)
changed to:
x<-1:10
y<-1:10
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
by "value" I am assuming you mean where the line cuts the x-axis? Something like text? i.e.:
text((0), min(y), "number", pos=2)
if you want the label on the x axis then try:
abline(a=1,b=0,v=1)
axis(1, at=1,labels=1)
to prevent overlap between labels you could remove the zero i.e.:
plot(x,y,xlim=c(-30,30),yaxt="n")
axis(2, at=c(1.77,5,10,15,20,25))
or before you plot extend the margins and add the labels further from the axis
par(mar = c(6.5, 6.5, 6.5, 6.5))
plot(x,y,xlim=c(-30,30))
abline(a=1,b=0,v=1)
axis(2, at=1.77,labels=1.77,mgp = c(10, 2, 0))
Similar in spirit to the answer proposed by #user1317221, here is my suggestion
# generate some fake points
x <- rnorm(100)
y <- rnorm(100)
# positions of the lines
vert = 0.5
horiz = 1.3
To display the lines at the center of the plot, first compute the horizontal and vertical distances between the data points and the lines, then adjust the limits adequately.
# compute the limits, in order for the lines to be centered
# REM we add a small fraction (here 10%) to leave some empty space,
# available to plot the values inside the frame (useful for one the solutions, see below)
xlim = vert + c(-1.1, 1.1) * max(abs(x-vert))
ylim = horiz + c(-1.1, 1.1) * max(abs(y-horiz))
# do the main plotting
plot(x, y, xlim=xlim, ylim=ylim)
abline(h=horiz, v=vert)
Now, you could plot the 'values of the lines', either on the axes (the lineparameter allows you to control for possible overlapping):
mtext(c(vert, horiz), side=c(1,2))
or alternatively within the plotting frame:
text(x=vert, y=ylim[1], labels=vert, adj=c(1.1,1), col='blue')
text(x=xlim[1], y=horiz, labels=horiz, adj=c(0.9,-0.1), col='blue')
HTH

Get plot() bounding box values

I'm generating numerous plots with xlim and ylim values that I'm calculating on a per-plot basis. I want to put my legend outside the plot area (just above the box around the actual plot), but I can't figure out how to get the maximum y-value of the box around my plot area.
Is there a method for even doing this? I can move the legend where I want it by manually changing the legend() x and y values, but this takes a LONG time for the amount of graphs I'm creating.
Thanks!
-JM
Here's a basic example illustrating what I think you're looking for using one of the code examples from ?legend.
#Construct some data and start the plot
x <- 0:64/64
y <- sin(3*pi*x)
plot(x, y, type="l", col="blue")
points(x, y, pch=21, bg="white")
#Grab the plotting region dimensions
rng <- par("usr")
#Call your legend with plot = FALSE to get its dimensions
lg <- legend(rng[1],rng[2], "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = FALSE)
#Once you have the dimensions in lg, use them to adjust
# the legend position
#Note the use of xpd = NA to allow plotting outside plotting region
legend(rng[1],rng[4] + lg$rect$h, "sin(c x)", pch=21,
pt.bg="white", lty=1, col = "blue",plot = TRUE, xpd = NA)
The command par('usr') will return the coordinates of the bounding box, but you can also use the grconvertX and grconvertY functions. A simple example:
plot(1:10)
par(xpd=NA)
legend(par('usr')[1], par('usr')[4], yjust=0, legend='anything', pch=1)
legend( grconvertX(1, from='npc'), grconvertY(1, from='npc'), yjust=0,
xjust=1, legend='something', lty=1)
The oma, omd, and omi arguments of par() control boundaries and margins of plots - they can be queried using par()$omd (etc). and set (if needed) using par(oma=c()) (where the vector can have up to 4 values - see ?par)

Replace X-axis with own values

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.
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:
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

Resources