Adjust size/width of barplot plots when using mfrow - r

I want to produce two figures using base R, both showing barplots. The first figure should contain two bar plots and the second figure should contain four bar plots.
I used par(mfrow = c(...)) to arrange multiple bar plots in one figure.
I don't have trouble to produce the figures themselves, but when I save the figures bar widths and tick labels are different in size.
To my understanding, when I produce the second figure with four bar plots and chose double the width of the first figure when exporting, bars and labels should be displayed with the same size in the file. However, the labels are much smaller and the bars have a different width in the second figure. Can anyone tell me why?
Here a simple example:
png(filename="plot1.png", width=200, height=300, bg="white")
par(mfrow = c(1, 2), mar = c(1, 2, 1, 1), oma = c(0, 0, 0, 0))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
dev.off()
png(filename="plot2.png", width=400, height=300, bg="white")
par(mfrow = c(1, 4), mar = c(1, 2, 1, 1), oma = c(0, 0, 0, 0))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
barplot(height = c(2,3), width = 1, xlim = c(0,2))
dev.off()
Plot 1:
Plot 2:

Probably par and pdf(width, height) should be equal.
png(filename="plot1.png", width=400, height=300, bg="white")
par(mfrow=c(1, 4), mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0))
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))
dev.off()
png(filename="plot2.png", width=400, height=300, bg="white")
par(mfrow=c(1, 4), mar=c(1, 2, 1, 1), oma=c(0, 0, 0, 0))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))
dev.off()
Another solution is to use layout.
def.par <- par(no.readonly=TRUE) # save par default, for resetting...
# 1 x 2 plot
layout(matrix(c(1:2, 0, 0), nrow=1, ncol=4, byrow=TRUE))
layout.show(n=2) # to inspect layout # MARK
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))
# 1 x 4 plot
layout(matrix(c(1:4), nrow=1, ncol=4, byrow=TRUE))
layout.show(n=4) # to inspect layout
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))
# 2 x 4 plot
layout(matrix(c(1:2, 0, 0, 3:6), nrow=2, ncol=4, byrow=TRUE))
layout.show(n=6) # to inspect layout
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2)))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2)))
par(def.par) # reset to default
However, both solutions bring a half empty plot 1, the reason can be seen in code above at # MARK.
We could use the magick package to "chop" the first plot to the desired content. First we create the *.pngs with the second method.
clr <- "#ED7C22" # color
png(filename="plot1.png", width=400, height=300, bg="white")
layout(matrix(c(1:2, 0, 0), nrow=1, ncol=4, byrow=TRUE))
replicate(2, barplot(height=c(2,3), width=1, xlim=c(0,2), col=clr, border=0))
dev.off()
png(filename="plot2.png", width=400, height=300, bg="white")
layout(matrix(c(1:4), nrow=1, ncol=4, byrow=TRUE))
replicate(4, barplot(height=c(2,3), width=1, xlim=c(0,2), col=clr, border=0))
dev.off()
Now, using image_chop we trim plot1.png to it's left half.
library(magick)
(i <- image_read("plot1.png"))
i.chopped <- image_chop(i, "200x+200") # says: trim by 200px at pos. 200
Finally, we export the chopped image.
image_write(i.chopped, path="plot1.ch.png", format="png")
Plot 1 ("chopped")
Plot 2

Related

Eliminating dead space from a plot consisting of two pie charts

I have this plot which I want to save as PDF.
pdf(file="pie_charts.pdf", width=8, height=5, onefile=F)
layout(matrix(c(1,2,3,3), ncol=2, byrow=TRUE), heights=c(4, 1))
par(mar=c(0,0,0,0), xpd=TRUE)
pie(c(1,9),col=c("black","white"))
pie(c(1,3),col=c("black","white"))
plot.new()
legend(x="center", ncol=2,legend=c("Black","Whtie"),fill=c("black","white"), bty = "n",cex=1.3)
dev.off()
And this is what I am getting
It looks quite good but I want to eliminate as much empty space as possible between the individual pie charts as well as between them and the legend. Any suggestions?
With layout(), I think you may be a bit limited with changing margins to squeeze the pie charts together.
This isn't an elegant solution but it works. I went in to the pie() function and modified the xlim arguments. This was my only change.
In other words, pie has this inside its function:
xlim <- ylim <- c(-1, 1)
Change the xlim to shift the pie charts left or right.
I made mypieleft() and mypieright().
mypieleft<-function(blah blah){
[untouched code from pie]
# xlim <- ylim <- c(-1, 1)
xlim <- c(-1.20, 0.80)
ylim <- c(-1, 1)
[untouched code from pie]
}
and
mypieright<-function(blah blah){
[untouched code from pie]
# xlim <- ylim <- c(-1, 1)
xlim <- c(-0.75, 1.25)
ylim <- c(-1, 1)
[untouched code from pie]
}
Then change your code slightly:
layout(matrix(c(1,2,3,3), ncol=2, byrow=TRUE), heights=c(4, 1))
par(oma=c(0,0,0,0), xpd=TRUE)
mypieleft(c(1,9),col=c("black","white"))
mypieright(c(1,3),col=c("black","white"))
plot.new()
legend(x="center", ncol=2,legend=c("Black","Whtie"),fill=c("black","white"), bty = "n",cex=1.3)
I get this image.
Just increase the radius of the pies:
layout(matrix(c(1, 2, 3, 3), ncol=2, byrow=TRUE), heights=c(4, 1))
par(mar=c(0, 1, 0, 0)) # increase left margin to accommodate text
pie(c(1, 9), col=c("black","white"), radius=1)
par(mar=c(0, 0, 0, 1)) # increase right margin to accommodate text
pie(c(1, 3), col=c("black", "white"), radius=1)
plot.new()
legend(x="center", ncol=2, legend=c("Black", "White"),
fill=c("black", "white"), bty="n", cex=1.3)
See the radius arg at ?pie.

Center align bottom legend viewport or grob relative to plot area with grid package

I'm trying to align a legend at the bottom of a chart, centered respectively to that chart. But I'm having trouble aligning it.
The pictures below show the current rendering, where you can clearly see the legend is misaligned (red line for guidance).
library(grid)
draw <- function() {
masterLayout <- grid.layout(
nrow = 4,
ncol = 1,
heights = unit(c(0.1, 0.7, 0.1, 0.1), rep("null", 4)))
vp1 <- viewport(layout.pos.row=1, layout.pos.col = 1, name="title")
vp2 <- viewport(layout.pos.row=2, layout.pos.col = 1, name="plot")
vp3 <- viewport(layout.pos.row=3, layout.pos.col = 1, name="legend")
vp4 <- viewport(layout.pos.row=4, layout.pos.col = 1, name="caption")
pushViewport(
vpTree(viewport(layout = masterLayout, name = "master"),
vpList(vp1, vp2, vp3, vp4)))
## Draw main plot
seekViewport("plot")
pushViewport(viewport(width=unit(.8, "npc")))
grid.rect(gp=gpar("fill"="red")) # dummy chart
popViewport(2)
## Draw legend
seekViewport("legend")
colors <- list(first="red", second="green", third="blue")
data.names <- names(colors)
legend.cols <- length(data.names)
pushViewport(viewport(
width = unit(0.8, "npc"),
layout = grid.layout(ncol=legend.cols * 2,
nrow=1,
widths=unit(2.5, "cm"),
heights=unit(0.25, "npc"))))
idx <- 0
for(name in data.names) {
idx <- idx + 1
pushViewport(viewport(layout.pos.row=1, layout.pos.col=idx))
grid.circle(x=0, r=0.35, gp=gpar(fill=colors[[name]], col=NA))
popViewport()
idx <- idx + 1
pushViewport(viewport(layout.pos.row=1, layout.pos.col=idx))
grid.text(x=unit(-0.8, "npc"), "text", just="left")
popViewport()
}
popViewport(2)
}
draw()
I don't understand why you're doing so much with individual viewports. It makes it very complex. I would have thought it was much easier to have one viewport for the legend and then control the x coordinate of the text and circles relative to that. Something like this; I'm not sure it's exactly what you want but it feels it should be easy to control if you need to tweak it:
library(grid)
draw <- function() {
masterLayout <- grid.layout(
nrow = 4,
ncol = 1,
heights = unit(c(0.1, 0.7, 0.1, 0.1), rep("null", 4)))
vp1 <- viewport(layout.pos.row=1, layout.pos.col = 1, name="title")
vp2 <- viewport(layout.pos.row=2, layout.pos.col = 1, name="plot")
vp3 <- viewport(layout.pos.row=3, layout.pos.col = 1, name="legend")
vp4 <- viewport(layout.pos.row=4, layout.pos.col = 1, name="caption")
pushViewport(
vpTree(viewport(layout = masterLayout, name = "master"),
vpList(vp1, vp2, vp3, vp4)))
## Draw main plot
seekViewport("plot")
pushViewport(viewport(width=unit(.8, "npc")))
grid.rect(gp=gpar("fill"="red")) # dummy chart
popViewport(2)
## Draw legend
seekViewport("legend")
colors <- list(first="red", second="green", third="blue")
lab_centers <- seq(from = 0.2, to = 0.8, length = length(colors))
disp <- 0.03 # how far to left of centre circle is, and to right text is, in each label
for(i in 1:length(colors)){
grid.circle(x = lab_centers[i] - disp, r = 0.1, gp=gpar(fill = colors[[i]], col=NA))
grid.text("text", x = lab_centers[i] + disp)
}
popViewport(2)
}
draw()
grid.lines(c(0.5, 0.5), c(0, 1))
If your legend labels aren't all the same length, you probably need to left align them and tweak the way I've used a disp parameter but shouldn't be too hard.

Barplot -adding percentages

So i have this numeric variables which reflect percentages
data1.pct<-19
data2.pct<-5
data3.pct<-76
class1.pct<-35
class2.pct<-18
class3.pct<-47
Now i am using this code to generate barplot
CairoPDF(paste('data1/', data, '_plot1.pdf', sep=''), family='sans', pointsize=9, width=6, height=3.25)
par(mar=(c(4, 4, 1, 13) + 0.1), mgp=c(3, 2, 0), xpd=TRUE)
barplot(cbind(
c(data1.pct, data2.pct, data3.pct),
c(class1.pct, class2.pct, class3.pct)), col=c("firebrick3", "dodgerblue3", "mistyrose1"), ylim=c(0,100), space=c(0,1)
)
legend("topright", inset=c(-0.55, 0), legend=c("not attend", "refused", "attend"), col=c("mistyrose1", "dodgerblue3", "firebrick3"), lty=1, lwd=2, bty='n')
dev.off()
and the result is
I would like to add corresponding percentages inside barplot, that is numbers/percentages in my variables. So My output should be:
I would like to use barplot funcion to do this and NOT ggplot2
I have tried adding percentages with
text(mydata, 0, round(data1.pct), 1),cex=1,pos=3) but this is not right.
To get the y-values for the text, you can use cumsum along with tail and head to get the midpoints of each bar section.
par(mar=(c(4, 4, 1, 13) + 0.1), mgp=c(3, 2, 0), xpd=TRUE)
## Make the matrix for barplot
mat <- cbind(c(data1.pct, data2.pct, data3.pct), c(class1.pct, class2.pct, class3.pct))
## Get the y-values for text
ys <- apply(mat, 2, function(x) c(x[1]/2, head(cumsum(x),-1) + tail(x,-1)/2))
## Make barplot, store x data
xs <- barplot(mat, col=c("firebrick3", "dodgerblue3", "mistyrose1"), ylim=c(0,100), space=c(0,1))
## Add text
text(rep(xs, each=nrow(ys)), c(ys), labels=c(mat))
legend("topright", inset=c(-0.55, 0), legend=c("not attend", "refused", "attend"), col=c("mistyrose1", "dodgerblue3", "firebrick3"), lty=1, lwd=2, bty='n')

Title going out of frame in plot canvas in R

Hi everybody I have 3 plots with density bars on either axis which I have done this way (here is a simpler form presented with only 3 ordinary plots but the other parts are necessary as required for a more complicated function which I have left off here just for the ease of viewing)
scatterBar.Norm <- function(x,y) {
zones <- matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(5/7,2/7), heights=c(2/7,5/7))
title("My Title", outer=TRUE);
par(mar=c(3,3,1,1),mgp=c(2,1,0))
plot(1:10, xlab="Magnification", ylab="residue", col=2)
par(mar=c(0,3,1,1))
plot(1:10, xlab="Magnification", ylab="residue",col=3)
par(mar=c(3,0,1,1))
plot(1:10, xlab="Magnification", ylab="residue", col=4)}
scatterBar.Norm(2,3)
The problem :
Firstly the The plot title the "My Title" part is going out of the canvas , how to fix it ?
Thanks for the much needed help in advance.
You've instructed R to plot the title in the outer margin, but (at least in your example) you haven't set up that margin. The following should work:
scatterBar.Norm <- function(x, y) {
zones <- matrix(c(2, 0, 1, 3), ncol=2, byrow=TRUE)
layout(zones, widths=c(5, 2), heights=c(2, 5))
par(mar=c(3, 3, 1, 1), mgp=c(2, 1, 0), oma=c(0, 0, 3, 0))
plot(1:10, xlab="Magnification", ylab="residue", col=2)
par(mar=c(0, 3, 1, 1))
plot(1:10, xlab="Magnification", ylab="residue", col=3)
par(mar=c(3, 0, 1, 1))
plot(1:10, xlab="Magnification", ylab="residue", col=4)
title("My Title", outer=TRUE)
}
plot.new()
scatterBar.Norm(2, 3)

Gradient legend in base

Earlier I asked about creating a gradient of n values in base graphics (LINK). Now I'd like to create a gradient legend that goes with it. My ideal would be something like ggplot2's gradient legends:
Here's some code similar to what I'm working with:
colfunc <- colorRampPalette(c("red", "blue"))
plot(1:20, 1:20, pch = 19, cex=2, col = colfunc(20))
Here is an example of how to build a legend from first principles using rasterImage from grDevices and layout to split the screen
layout(matrix(1:2,ncol=2), width = c(2,1),height = c(1,1))
plot(1:20, 1:20, pch = 19, cex=2, col = colfunc(20))
legend_image <- as.raster(matrix(colfunc(20), ncol=1))
plot(c(0,2),c(0,1),type = 'n', axes = F,xlab = '', ylab = '', main = 'legend title')
text(x=1.5, y = seq(0,1,l=5), labels = seq(0,1,l=5))
rasterImage(legend_image, 0, 0, 1,1)
Late to the party, but here is a base version presenting a legend using discrete cutoffs. Thought it might be useful for future searchers.
layout(matrix(1:2,nrow=1),widths=c(0.8,0.2))
colfunc <- colorRampPalette(c("white","black"))
par(mar=c(5.1,4.1,4.1,2.1))
plot(1:10,ann=FALSE,type="n")
grid()
points(1:10,col=colfunc(10),pch=19,cex=1.5)
xl <- 1
yb <- 1
xr <- 1.5
yt <- 2
par(mar=c(5.1,0.5,4.1,0.5))
plot(NA,type="n",ann=FALSE,xlim=c(1,2),ylim=c(1,2),xaxt="n",yaxt="n",bty="n")
rect(
xl,
head(seq(yb,yt,(yt-yb)/10),-1),
xr,
tail(seq(yb,yt,(yt-yb)/10),-1),
col=colfunc(10)
)
mtext(1:10,side=2,at=tail(seq(yb,yt,(yt-yb)/10),-1)-0.05,las=2,cex=0.7)
And an example image:
The following creates a gradient color bar with three pinpoints without any plot beforehand and no alien package is needed. Hope it is useful:
plot.new()
lgd_ = rep(NA, 11)
lgd_[c(1,6,11)] = c(1,6,11)
legend(x = 0.5, y = 0.5,
legend = lgd_,
fill = colorRampPalette(colors = c('black','red3','grey96'))(11),
border = NA,
y.intersp = 0.5,
cex = 2, text.font = 2)
As a refinement of #mnel's great answer, inspired from another great answer of #Josh O'Brien, here comes a way to display the gradient legend inside the plot.
colfunc <- colorRampPalette(c("red", "blue"))
legend_image <- as.raster(matrix(colfunc(20), ncol=1))
## layer 1, base plot
plot(1:20, 1:20, pch=19, cex=2, col=colfunc(20), main='
Awesome gradient legend inside')
## layer 2, legend inside
op <- par( ## set and store par
fig=c(grconvertX(c(0, 10), from="user", to="ndc"), ## set figure region
grconvertY(c(4, 20.5), from="user", to="ndc")),
mar=c(1, 1, 1, 9.5), ## set margins
new=TRUE) ## set new for overplot w/ next plot
plot(c(0, 2), c(0, 1), type='n', axes=F, xlab='', ylab='') ## ini plot2
rasterImage(legend_image, 0, 0, 1, 1) ## the gradient
lbsq <- seq.int(0, 1, l=5) ## seq. for labels
axis(4, at=lbsq, pos=1, labels=F, col=0, col.ticks=1, tck=-.1) ## axis ticks
mtext(sq, 4, -.5, at=lbsq, las=2, cex=.6) ## tick labels
mtext('diff', 3, -.125, cex=.6, adj=.1, font=2) ## title
par(op) ## reset par

Resources