Spacing and plot dimensions in R using layout - r

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.

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))

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.

R- Break axis matplot function

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:

How to avoid wired ylab error when plotting in R

I need a two y-axes figure. hrbrmstr suggested to use simple plots. But when adapting the graph to my setting I observed I cannot add the ylab on the right hand side, getting a wired error:
Error in axis(4, ylim = c(0, 1), col = "black", col.axis = "black", las = 1, :
'labels' is supplied and not 'at'
Is this avoidable?
look at the code the bottom line fpr SOURCE OF ERROR
featPerf <- data.frame( expS=c("1", "2", "3", "4"),
exp1=c(1000, 0, 0, 0),
exp2=c(1000, 5000, 0, 0),
exp3=c(1000, 5000, 10000, 0),
exp4=c(1000, 5000, 10000,20000),
accuracy=c(0.4, 0.5, 0.65, 0.9) )
# make room for both axes ; adjust as necessary
par(mar=c(5, 5, 5, 7) + 0.2)
# plot the bars first with no annotations and specify limits for y
#barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", ylim=c(0, max(colSums(featPerf[2:5]))))
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
# make the bounding box (or not...it might not make sense for your plot)
#box()
# now make the left axis
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
# start a new plot
par(new=TRUE)
# plot the line; adjust lwd as necessary
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5)
# annotate the second axis -- SOURCE OF ERROR -> VVVVVVVVVVVVVVVVVV
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy")
Like this?
par(mar=c(4,4,1,4) + 0.2)
barplot(as.matrix(featPerf[,2:5]), axes=FALSE, xlab="", ylab="", beside=TRUE)
axis(2, ylim=c(0, max(colSums(featPerf[2:5]))), col="black", las=1)
par(new=TRUE)
plot(x=1:4, y=featPerf[,6], xlab="Experiments", ylab="Abs. # of Features", axes=FALSE, type="l", ylim=c(0,1), lwd=5, col="blue")
axis(4, ylim=c(0,1), col="blue", col.axis="blue", las=1)
mtext("Accuracy",4,line=2, col="blue")
For the record, it is never a good idea to stack plots on top of each other this way (with two axes). I've made the line and the axis the same color in an attempt to draw attention to what you are doing, but this is still a very bad idea.
First of all it is not advisable to use two Y-axes in a same plot.
If you add at argument to the axis call, you get the name "Accuracy" on the right hand side of the plot.
axis(4, ylim=c(0,1), col="black", col.axis="black", las=1, labels="Accuracy",
at = .5)

R, plot , font size changes in multiple plot figures

I am creating graphs for a publication and would like them to have the same font size.
When I create a figure with multiple plots, the font size decreases even though I haven't changed the tiff() resolution or pointsize parameter.
I increased the figure size according to ultimately fit the number of plots, and made sure the margins are equivalent for single and multiple plot figures.
Following is an example code (The font size is consistent between 1x1 and 2x1 figure, but decreases for 3x2 figure):
tiff("1x1.tif", width=3,height=2.5,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(1,1),mar=c(4,4,.5,.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
dev.off()
tiff("2x1.tif", height=2.5*2,width=3,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(2,1),mar=c(2,4,2.5,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(4,4,0.5,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
dev.off()
tiff("3x2.tif", height=2.5*3,width=3*2,units="in",res=600,pointsize=8,
compression="lzw",restoreConsole=T)
par(mfrow=c(3,2),mar=c(.5,4,4,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(.5,2,4,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="",yaxt="n",ylab="")
par(mar=c(2.5,4,2,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="")
par(mar=c(2.5,2,2,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),xaxt="n",xlab="",yaxt="n",ylab="")
par(mar=c(4.5,4,0,0.5)+0.1)
plot(x=rnorm(10),y=rnorm(10))
par(mar=c(4.5,2,0,2.5)+0.1)
plot(x=rnorm(10),y=rnorm(10),yaxt="n",ylab="")
dev.off()
Why is this happening?
P.S.: I'm not using ggplot2 or lattice because I'm using my own error bar function on the "actual" figures (I can't remember why right now but I tried working with the ggplot2 error bars and didn't get what I wanted).
The parameter controlling the overall relative size of objects in the plot (including text) is called cex. When you use many panels it is decreased by default, but it can be overridden by manually setting it to 1.
par(mfrow=c(3,2), mar=c(.5,4,4,0.5)+0.1, cex=1)
Off-topic-tip
It looks like you should use oma (outer margin) rather than calling par(mar=...) between the calls to plot. I find it very useful, but hardly anyone seems to know of it. Also ann=FALSE turns off all anotations, las=1 turns axis tick labels horizontal.
par(mfrow=c(3,2), oma=c(4.5, 4, 4, 2.5), mar=rep(.1, 4), cex=1, las=1)
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n", yaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, xaxt="n", yaxt="n")
plot(x=rnorm(10), y=rnorm(10), ann=FALSE)
plot(x=rnorm(10), y=rnorm(10), ann=FALSE, yaxt="n")
title("My plot", outer=TRUE)
mtext("X-axis label", 1, 3, outer=TRUE)
mtext("Y-axis label", 2, 3, outer=TRUE, las=0)

Resources