mjandrews
mjandrews

Reputation: 2464

Minimizing whitespace around plots when using tikzDevice in R

I use tikzDevice in R to write tikz code to place plots in LaTeX documents. I have found that the whitespace around the plots, i.e. the margins, is usually excessive.

Here is a minimal example of exporting a R plot 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 gives you a small pdf where, as you can see, the is a lot of whitespace in the margins. Ideally, I would like there to be no whitespace at all below the x-axis point labels, or to the left of the y-axis point labels, etc.

By the way, when inserting into a LaTeX document, I will always use standAlone=F in the tikz command above and then use \input{binomial} in the document. There is no difference in whitespace as can be easily verified by \framebox{\input{binomial}}. Also, par(xaxs='i') made no difference.

Does any know of anyway where I can remove all the whitespace from the margins of these plots?

Upvotes: 3

Views: 1415

Answers (2)

Martin Raic
Martin Raic

Reputation: 31

Try op <- par(cex=0.5) before the barplot, but after the tikz command. You can also modify the size of the title, the axis annotation and the labels of the axis by adding cex.main= ..., cex.axis= ... or cex.lab= ..., respectively. These numbers get multiplied by cex.

Upvotes: 2

m0nhawk
m0nhawk

Reputation: 24238

Add this before the barplot:

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

Where 1 is a margin. It's counted from the axes, so 0 would strip the ticks. Try several, 1.8 is OK for me for you plot.

Upvotes: 1

Related Questions