# Minimizing gaps around graphs when using tikzDevice in R

I am using `tikzDevice`

in R to write code `tikz`

for placing graphs in LaTeX documents. I found that there is a gap around the plots i.e. The margin is usually excessive.

Below is a minimal example of exporting an R chart as tikz code:

``````x <- seq(30, 70)
probs <- dbinom(x, size=100, prob=0.5)
library(tikzDevice)
tikz('binomial.tex', standAlone = T, width=3, height=2.5)
barplot(names.arg=x, probs, cex.names=0.6, cex.axis=0.6)
dev.off()
```

```

Running `pdflatex binomial.tex`

it gives a small pdf file, where, as you can see, there are a lot of spaces in the margins. Ideally, I would like there to be no spaces at all under the x-axis point labels, or to the left of the y-axis point labels, etc.

By the way, when pasting into a LaTeX document, I will always use `standAlone=F`

in the command `tikz`

above and then use `\input{binomial}`

in the document. There is no difference in space, which is easy to check with `\framebox{\input{binomial}}`

. It also `par(xaxs='i')`

doesn't matter.

Does anyone still know where I can remove all the spaces from the fields of these parcels?

+3

source to share

Add this before `barplot`

:

``````op <- par(mar = rep(1, 4))
```

```

Where `1`

is the margin. It is counted from the axes, so it `0`

will split ticks. Try a few that `1.8`

works for me for you.

+1

source

Try `op <- par(cex=0.5)`

before `barplot`

but after the command `tikz`

. You can also resize the title, axis annotation and axis labels by adding `cex.main=`

..., `cex.axis=`

... or `cex.lab=`

..., respectively. These numbers are multiplied by `cex`

.

+2

source

All Articles