Skip to content

Instantly share code, notes, and snippets.

@felixlindemann
Created June 27, 2014 12:38
Show Gist options
  • Save felixlindemann/565903fce7f21da1daea to your computer and use it in GitHub Desktop.
Save felixlindemann/565903fce7f21da1daea to your computer and use it in GitHub Desktop.
\NeedsTeXFormat{LaTeX2e}
\documentclass[a4paper]{scrartcl}
\usepackage[left=1.5cm,right=1.5cm,top=1cm,bottom=1cm,includeheadfoot]{geometry} %4
\usepackage{tikz}
\usepackage{here}
\usepackage{listings}
%
\newcommand{\defineR}{ \lstset{ %
language=R, % choose the language of the code
basicstyle=\footnotesize, % the size of the fonts that are used for the code
numbers=left, % where to put the line-numbers
numberstyle=\footnotesize, % the size of the fonts that are used for the line-numbers
stepnumber=2, % the step between two line-numbers. If it's 1 each line will be numbered
numbersep=5pt, % how far the line-numbers are from the code
backgroundcolor=\color{white}, % choose the background color. You must add \usepackage{color}
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
escapeinside={\%*}{*)} % if you want to add a comment within your code
}}
\begin{document}
\sffamily
\title{Plot in R as TIKZ-\LaTeX-Image}
\author{Felix Lindemann}
\maketitle
\begin{figure}[H]
\centering
\input{tikz}
%\caption{caption for this plot}
\label{fig:ploter}
\end{figure}
\defineR \vspace{-1cm}
\begin{lstlisting}[caption=Random Numbers in R,label=lst.random]
require(tikzDevice) #require this package
set.seed(1) # Output will always be the same
inch<- 0.3937008 # give units
h <- 10 # define height of tikz in cm
w <- 10 # define width of tikz in cm
td <- getwd()
tf<- file.path(td, 'tikz.tex') #set file
tikz(tf,standAlone=FALSE, height=h*inch , width=w*inch )
mu <- 0
sd <- 1
r <- rnorm(1000, mu, sd) #create random numbers
x<-seq(-3,3,by=0.01) # create sequence
hist(r, freq=FALSE, xlab="",
main=paste("Histogram of\n1000 random numbers N(",mu,",",sd,")", sep="")
)
rug(r)
lines(density(r), col=2) # draw Kernel-Density
lines(x, dnorm(x,mu,sd), col=3, lty=2) #draw density
dev.off()
\end{lstlisting}
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment