# [R] URGENT help-Problem with panel barplot spacing

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Tue May 28 19:55:54 CEST 2019

```I think this is what you want.  You didn't send a reproducible example
(no values for djf.gcms or for cores1).

For what I think you are doing, lattice would be much simpler.  It
handles the repetition within each panel for you.

## generate some data
djf.gcms <- matrix(sample(50, size=9*12, replace=TRUE), 9, 12)

library(lattice)
library(latticeExtra)

djfs <- cbind(stack(data.frame(djf.gcms)), letter=factor(letters[1:9]))

tmp <-
barchart(letter ~ values | ind, group=letter, col=1:9, data=djfs,
horizontal=TRUE,
stack=TRUE, type="i",
scales=list(x=list(alternating=FALSE, axs="i", limits=c(0,59))),
origin=0, layout=c(4, 3), between=list(x=1, y=2)) +
layer(panel.text(x=x+5, y, label=x)) +
layer(panel.abline(v=seq(0,50,10), col="gray"), under=TRUE)
tmp

Rich

The best place to start learning lattice is the trellis book
http://geog.uoregon.edu/GeogR/pdfs/trellis.user.pdf

The definitive reference is Deepayan Sarkar's book,
Lattice: Multivariate Data Visualization with R

My book (HH2) is
Heiberger, Richard M. and Holland, Burt (2015).
Statistical Analysis and Data Display: An Intermediate Course with
Examples in R.
Springer, second edition. ISBN 978-1-4939- 2121-8.
https://www.springer.com/us/book/9781493921218

See HH2 Chapter 4 Graphs for a general discussion
and many examples throughout the book and in the accompanying CRAN package HH.

install.packages("HH")

On Tue, May 28, 2019 at 6:42 AM Kwesi A. Quagraine
<starskykwesi using gmail.com> wrote:
>
> Hello All,
>
> I am struggling to control the spaces between barplots I have panelled. I
> would be grateful for any help in reducing the spaces between the plots.
> For instance, how I can reduce distance between 1 and 2, 2 and 3 etc.
>
> I would appreciate any help on this.
>
> Thanks
>
> Here’s a snippet of my code and attached is the current image I generate
> from the command;
>
> ##for djf
> postscript("fig_paper2_fre_obs_models_djf_1980_2013_4x3_new.eps",width=10,height=8,paper="special",horizontal=T,onefile=T)
>
> par(mfrow=c(3,4))
> par(mar=c(6,11,1,2))
>
> a =1
> for (j in a) {
>   djf.bar<- barplot(djf.gcms[,j],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> j,type="n",font.main = 1, cex.main = 1.5,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   djf.bar<-barplot(djf.gcms[,j],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> j,font.main = 1, cex.main = 1.5,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   ## Add text at top of bars
>   text(y = djf.bar, x = djf.gcms[,j], label = round(djf.gcms[,j],
> digits=0), pos = 4, cex = 1.2)
> }
>
>
> for (i in 2:4) {
>   djf.bar<-barplot(djf.gcms[,i],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> i, yaxs="i",type="n",font.main = 1, cex.main = 1.1,las=1, axisnames =
> FALSE, width = 0.8,cex.names=1.0,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   djf.bar<-barplot(djf.gcms[,i],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> i, yaxs="i",font.main = 1, cex.main = 1.1,las=1, axisnames = FALSE, width =
> 0.8,cex.names=1.0,horiz = TRUE)
>   text(y = djf.bar, x = djf.gcms[,i], label = round(djf.gcms[,i],
> digits=0), pos = 4, cex = 1.0)
>
> }
>
> b=5
> for (k in b) {
>   djf.bar<-barplot(djf.gcms[,k],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> k,type="n",font.main = 1, cex.main = 1.1,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   djf.bar<-barplot(djf.gcms[,k],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> k,font.main = 1, cex.main = 1.1,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   text(y = djf.bar, x = djf.gcms[,k], label = round(djf.gcms[,k],
> digits=0), pos = 4, cex = 1.2)
>
> }
>
> for (n in 6:8) {
>   barplot(djf.gcms[,n],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> n,type="n",font.main = 1, cex.main = 1.1,las=1, axisnames = FALSE, width =
> 0.8,cex.names=1.0,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   barplot(djf.gcms[,n],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> n,font.main = 1, cex.main = 1.1,las=1, axisnames = FALSE, width =
> 0.8,cex.names=1.0,horiz = TRUE)
>   text(y = djf.bar, x = djf.gcms[,n], label = round(djf.gcms[,n],
> digits=0), pos = 4, cex = 1.0)
>
> }
>
> c = 9
> for (m in c) {
>   djf.bar<-barplot(djf.gcms[,m],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> m,type="n",font.main = 1, cex.main = 1.1,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   djf.bar<-barplot(djf.gcms[,m],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> m,font.main = 1, cex.main = 1.1,las=1, axisnames = T, width =
> 0.8,cex.names=1.5,horiz = TRUE)
>   title("Frequency (%)", line = -17.0)
>   text(y = djf.bar, x = djf.gcms[,m], label = round(djf.gcms[,m],
> digits=0), pos = 4, cex = 1.2)
>
> }
>
> for (i in 10:12) {
>   djf.bar<-barplot(djf.gcms[,i],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> i,type="n",font.main = 1, cex.main = 1.1,las=1, axisnames = F, width =
> 0.8,cex.names=1.0,horiz = TRUE)
>   abline(v=c(seq(0,50,10)),col='grey',lwd=0.2)
>   par(new=TRUE)
>   djf.bar<-barplot(djf.gcms[,i],ylim=c(0,9),xlim=c(0,50),col=cores1,main =
> i,font.main = 1, cex.main = 1.1,las=1, axisnames = F, width =
> 0.8,cex.names=1.0,horiz = TRUE) #xlab="Frequency (%)"
>   title("Frequency (%)", line = -17.0)
>   text(y = djf.bar, x = djf.gcms[,i], label = round(djf.gcms[,i],
> digits=0), pos = 4, cex = 1.0)
>
> }
> dev.off()
>
>
>
> ------------
> Try not to become a man of success but rather a man of value- Albert
> Einstein
>
> Kwesi A. Quagraine
> Department of Physics
> School of Physical Sciences
> College of Agriculture and Natural Sciences
> University of Cape Coast
> Cape Coast, Ghana
>
> Alt. Email: kwesi using csag.uct.ac.za
> Web: http://www.recycleupghana.org/
> Office: +27 21 650 3164
> Skype: quagraine_cwasi
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help