R- Break axis matplot function - r

I need to break an axis from 0.5 to 1.5. My code is:
matplot( wxyz$days_until_last_pay, wxyz[,c(2,3,4,5)], type=c("b"), pch=1, col=1:4,
main="x![enter image description here][1]", cex.main=0.8)
legend("bottomright", inset=c(0,-0.57), fill=NULL,
legend = c("mean","median","max", "min"), col=1:4, pch=1, cex=0.8)
library("plotrix")
axis.break(axis=2,1,,2,style="zigzag", brw=0.03)
But I only get a line in it. This is not breaking the axis.
How can I solve this?
Thanks!

axis.break puts a break into an existing plot, so if the axis is not "broken" it will not work.
One suggestion is to make two plots on top of each other and set their ylim be so that there is a gap between 0.5 and 1.5, e.g.
## Some data, set.seed(1)
dat <- matrix(c(rnorm(50, 2, 0.1),
rnorm(50, 0.2, 0.05),
rnorm(50, 0.3, 0.05)),
byrow=FALSE, ncol=3)
## Split the device into two subplots
par(mfrow = c(2,1))
## Set the bottom margin of the top plot to 0.1
par(mar=c(0.1,4.1,4.1,2))
## Top plot (first column of the matrix)
plot(dat[,1], add=T, type="l", xaxt="n", ylab="", ylim=c(1.5, 2.5))
## Set the top margin of the bottom plot to 0.1
par(mar=c(5.1,4.1,0.1,2))
## Bottom plot
matplot(dat[,2:3], type="l", col=2:3, ylab="", ylim=c(0, 0.5))
This gives you something like:

Related

How to keep the space between tick and label minimum?

i've got a tiny problem in here, which i would like to have some hints on.
How can i change the space between ticks and labels? (indicated with 1 & 2)
my current structure looks as follows:
par(mfrow=c(5,2),oma=c(0,0,2,0),las=1,mar=c(3,5,2,1),cex.lab=0.9, cex.axis=0.7)
plot(sapply(ERRORS.train.fast[[1]],mean),main="Pipe 63569",type="l", ylab="", xlab="",xaxt="n")
axis(1, at=1:29,labels=seq(2,30,1))
title(ylab= "RMSE (-)",line=3)
title(xlab= "K-Value",line=2)
highly appreciate your help!
cheers,
Olli
You can use the padj argument for "adjustment for each tick label perpendicular to the reading direction." (from ?axis)
par(mfrow = c(1, 2))
plot(1:5, axes = F)
axis(1)
plot(1:5, axes = F)
axis(1, padj = -.75)
Unfortunately, the directions are different for the different axes (because it is relative what is "up" for the text), so to move the labels closer to the ticks, you will want lower padj values for the horizontal axis, but higher padj values for the vertical axis.
If you rotate the labels (as shown in your example plot on the vertical axis), you will use hadj instead of padj. Overall, I would expect you want something like:
plot(1:5, axes = F)
axis(1, padj = -.75)
axis(2, hadj = 0, las = 1)
You can use the mpg par.
par(mfrow=c(1,2))
plot(iris[,3:4], pch=20, col=rainbow(3)[iris$Species],
ylab="", xlab="",xaxt="n")
axis(1, at=1:7)
plot(iris[,3:4], pch=20, col=rainbow(3)[iris$Species],
ylab="", xlab="", xaxt="n")
axis(1, at=1:7, mgp=c(0,0.5,0))

Matching an arrow's ends with the x-axis R plot

I'm wondering how to to get the left-most end and the right-most end of my arrows() in my plot to EXACTLY match the left-most end and the right-most end of my X-AXIS lying beneath it (if possible, such that one can read the values (within rounding error) for two ends of the arrow off the X-Axis right below it)?
Here is my R code with no success:
ci <- c(0.09253967, 0.48434172)
plot(1, 1, ty="n" ,ann=F, yaxt="n", bty="n", xlim=c(ci[1], ci[2]), ylim=c(0, 1), xaxs="i")
arrows(ci[1], .01, ci[2], .01, code=3, lwd=2, angle = 90, length = .08 )
One method: control where axis places the ticks:
ci <- c(0.09253967, 0.48434172)
plot(1, 1, ty="n" ,ann=F, yaxt="n", bty="n", xlim=c(ci[1], ci[2]), ylim=c(0, .3), xaxt="n")
axis(side = 1, at = ci)
arrows(ci[1], .01, ci[2], .01, code=3, lwd=2, angle = 90, length = .08 )
... but the ticks are at not-pretty locations (though you can easily control this).
Another method: find what axTicks thinks would be the correct axis ticks for the current plot:
newx <- range(axTicks(side = 1))
newx
# [1] 0.1 0.4
# ... your plotting code ...
arrows(newx[1], 0.02, newx[2], 0.02, code=3, lwd=2, angle=90, length=.08)

Spacing and plot dimensions in R using layout

I'm using the layout function to arrange two plots using the following code:
#Set up layout
m <- matrix(c(1,1,2, 1,1,2), nrow = 3, ncol = 2)
layout(m)
#First plot
plot(data$WG,data$RPOP, pch=21, cex=0.8, col=data$circle, bg=data$fill, xaxt='n', yaxt='n', bty="L", xlab="", ylab="Y label 1",xlim=c(0.02,1), ylim=c(0.02,1))+axis(side=1, at=seq(0,1, .1), pos=-0.02, tck=-.01, labels=FALSE)+axis(side=2, at=seq(0,1, .1), pos=-0.02, tck=-.01, las=1)
#Second plot
datum<-data.frame(prop=c(0.027879, 0.031515,0.001212,0,0,0,0,0,0,0,0),freq=c(0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0))
plot(datum$freq,datum$prop, cex=0, xaxt='n', yaxt='n', bty="L", xlab="X label", ylab="Y label 2", xlim=c(0.02,1), ylim=c(0.001,0.05))+axis(side=1, at=seq(0, 1, 0.1), pos=-0.001, tck=-.01)+axis(side=2, at=seq(0, 0.05, 0.01), pos=-0.02, tck=-.01, las=1)+lines(datum$freq,datum$prop, lwd=2, col="red")
And this is the result I get the following graph:
I would like to get rid of the space between the two graphs and at the same time I would like the width and height of the top plot to be the same. Is there a way to achieve this with the layout function?
Change the margins to get the plots closer together. The default margins are 5.1, 4.1, 4.1, 2.1 in the order bottom, left, top, right. So we'll make the bottom margin smaller for the top plot and the top margin smaller for the bottom plot. To reset the margins to the default after you're done, do par(mar=c(5,4,4,2) + 0.1):
#Set up layout
m <- matrix(c(1,1,2, 1,1,2), nrow = 3, ncol = 2)
layout(m)
# Smaller bottom margin for top plot
par(mar=c(1,4,4,2))
#First plot
plot(data$WG,data$RPOP, pch=21, cex=0.8, col=data$circle, bg=data$fill, xaxt='n', yaxt='n', bty="L", xlab="", ylab="Y label 1",xlim=c(0.02,1), ylim=c(0.02,1))+axis(side=1, at=seq(0,1, .1), pos=-0.02, tck=-.01, labels=FALSE)+axis(side=2, at=seq(0,1, .1), pos=-0.02, tck=-.01, las=1)
#Second plot
datum<-data.frame(prop=c(0.027879, 0.031515,0.001212,0,0,0,0,0,0,0,0),freq=c(0, 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0))
# Smaller top margin for bottom plot
par(mar=c(5,4,1,2))
plot(datum$freq,datum$prop, cex=0, xaxt='n', yaxt='n', bty="L", xlab="X label", ylab="Y label 2", xlim=c(0.02,1), ylim=c(0.001,0.05))+axis(side=1, at=seq(0, 1, 0.1), pos=-0.001, tck=-.01)+axis(side=2, at=seq(0, 0.05, 0.01), pos=-0.02, tck=-.01, las=1)+lines(datum$freq,datum$prop, lwd=2, col="red")
UPDATE: One way to get a square plot would be to set par(pty="s") (the default is "m"), which gives a 1/1 aspect ratio. However, after you reset par(pty="m") for the second plot, the two plots might not have the same width and you would have to play around with the plot size to get them lined up.
Another option would be to set the output file size to get the desired aspect ratio when you save it to png or pdf. For example:
png("test.png", 600, 800)
... All of your plotting code, including call to layout...
dev.off()
pdf("test.pdf", 6, 8)
... All of your plotting code, including call to layout...
dev.off()
There's probably a programmatic way to ensure the desired aspect ratio and line up the plots correctly without any manual adjustments, but I'm not sure how to do it.

R - how two have two y-axes with zeroes aligned in the middle of the plot

I am plotting two graphs on the same plot. Each one has a different ylim, and I would like to have the zeroes aligned in the middle of the plot.
This is my code:
# data
time <- seq(0.1, 10, by = 0.1)
det_rot <- runif(100, min=-100, max=100)
vel_mag <- runif(100, min=0, max=5)
# first plot
smoothingSpline = smooth.spline(time, det_rot, spar=0.20)
plot(time, det_rot,lwd=2,
ann=FALSE, las=2, pch="", ylim=c(-100,250)) # , pch=""
lines(smoothingSpline, lwd=2, col="red")
par(new=TRUE)
# second plot
smoothingSpline2 = smooth.spline(time, vel_mag, spar=0.20)
plot(time, vel_mag,
xaxt="n",yaxt="n",xlab="",ylab="",pch="", ylim=c(0,6))
lines(smoothingSpline2, lwd=2, col="blue",)
axis(4)
See the plot:
Simple fix: change ylims to c(-250, 250) and c(-6,6) respectively.

Adding label to secondary axis in R

I have this code:
# Plotting everything
plot( p1, col= "lightgreen", xlim=c(-2.5,4.5), ylim=c(0, 700), main="Daily Total Precipitation for AR and Oct-May", xlab="ln(x)" , ylab="Frequency", xaxt = "n") # first histogram
plot( p2, col="red", xlim=c(-2.5,4.5), ylim=c(0, 700), xaxt = "n" , add=T)
# Adding in text labels on top of the bars
text(x, y, paste(round(percents,2),"%"), cex=0.50, pos=3, offset=0.3, col="black")
axis(side=1, at=breaks) # new x-axis
# parameter that needs to be set to add a new graph on top of the other ones
par(new=T)
plot(x, percents, xlim=c(-2.5,4.5), type="l", col="yellow", lwd=3.0, axes=F, ylab=NA, xlab=NA)
axis(side=4, at=seq(0,100,by=10), col="yellow", col.axis="yellow") # additional y-axis
mtext("Percent", side=4, col="yellow")
# legend settings
legend("topleft", c("AR", "Oct-May"), lwd=10, col=c("red", "lightgreen"))
Which produces this graph:
And I can't seem to figure out how to get the secondary y-axis label to show up in the correct position. Any help or suggestions is greatly appreciated.
Edit: Using RStudio.
One option is to specify the line argument to mtext(). In the example below I add a couple more lines to the right (side = 4) margin of the plot using par(), and then I draw three labels using mtext() at the default (line = 0), line 3 (line = 3), and line -3 (line = -3):
op <- par(mar = c(5,4,4,4) + 0.1)
plot(1:10)
mtext("line0", side = 4)
mtext("line3", side = 4, line = 3)
mtext("line-3", side = 4, line = -3)
par(op)
Note that line numbers increase away from the plot region and that negative line values move into the plot region, or to the left of the right boundary of the plot region.
It takes a little playing with the number of margin lines (as set in par(mar = x)) and which line you want to draw on using mtext(), but a little trial and error should get you what you want.
Note also that you don't need to specify integer values for the line argument. You can specify fractions of lines too: line = 2.5.

Resources