4

I am learning R, and I want to use tikz to inlcude R plots in my documents. However, to me, half the point is that I want to set absolutely all typefaces, fontsizes, fontweights to the TeX-document font body. I looked up the documentation of tikzDevice and par(), however I wasn't able to find a way to do this. Please see the attached documents to see what I mean:

\documentclass[11pt,norsk,a4paper]{article}
\usepackage[norsk]{babel}
\usepackage[paper=a4paper, top=1in,bottom=1.1in,right=0.9in,left=0.9in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage[makeroom]{cancel}
\usepackage{amsmath,amsthm}
\usepackage{newtxtext}
\usepackage[T1]{fontenc}
\usepackage{pgf, pgfplots}
\usepackage{textcomp}
\date{\normalsize \today}
\begin{document}
I want all fonts in R plots to be exactly like this one.
\begin{figure}[!htbp]
  \begin{center}
    \input{fig1.tex}
  \end{center}
\end{figure}
\end{document}

And the R-script generatic fig1.tex:

library(tikzDevice)
tikz("/home/marius/Dokumenter/stk1110/oblig2/fig1.tex", width = 6, height = 5, pointsize=11)

# some data
menn = c(36.1,36.3,36.4,36.6,36.6,36.7,36.7,37.0,36.5,37.1)  
kvinner = c(36.6,36.7,36.8,36.8,36.7,37.0,37.1,37.3,36.9,37.4)

par(mfrow=c(2,3))

# first row
boxplot(menn)
qqnorm(menn)
qqline(menn)
hist(menn, freq=FALSE)
x = seq(36,37.4,0.01)
lines(x, 1/(2*pi*var(menn))^0.5*exp(-(x-mean(menn))^2/(2*var(menn))))

# second row
boxplot(kvinner)
qqnorm(kvinner)
qqline(kvinner)
hist(kvinner, freq=FALSE)
lines(x, 1/(2*pi*var(kvinner))^0.5*exp(-(x-mean(kvinner))^2/(2*var(kvinner))))

par(ps=1, cex=1, cex.main=111, cex.lab=111, cex.axis=111, cex.sub=111, font.main=1)
dev.off()

I'd like this to work for the majority of different plots, if not all, so that I can have exactly the same font as my document body. Even for figure titles. Are you able to suggest an solution?

  • Maybe a workaround would be to compile with XeLaTeX or LuaLaTeX, using a font that would be recognized by cairo under R? – sztruks Oct 28 '15 at 14:55
2

In one word: knitr.

In more words: Save it as example.Rnw and compite and edit With Rstudio, then use the "Compile PDF" button.

\documentclass[11pt]{article}
\usepackage[paper=a4paper, top=1in,bottom=1.1in,right=0.9in,left=0.9in]{geometry}
\begin{document}
I want all fonts in R plots to be exactly like this one.

<<dev="tikz", echo=F, fig.cap="A tikz plot",  fig.width=6, fig.height=5>>=
# par(ps=1, cex=1, cex.main=111, cex.lab=111, cex.axis=111, cex.sub=111, font.main=1)
# some data
menn = c(36.1,36.3,36.4,36.6,36.6,36.7,36.7,37.0,36.5,37.1)  
kvinner = c(36.6,36.7,36.8,36.8,36.7,37.0,37.1,37.3,36.9,37.4)
par(mfrow=c(2,3))
# first row
boxplot(menn)
qqnorm(menn)
qqline(menn)
hist(menn, freq=FALSE)
x = seq(36,37.4,0.01)
lines(x, 1/(2*pi*var(menn))^0.5*exp(-(x-mean(menn))^2/(2*var(menn))))
# second row
boxplot(kvinner)
qqnorm(kvinner)
qqline(kvinner)
hist(kvinner, freq=FALSE)
lines(x, 1/(2*pi*var(kvinner))^0.5*exp(-(x-mean(kvinner))^2/(2*var(kvinner))))
@
\end{document}

mwe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.