Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I am looking for a way to automatically create images like the one below:

Bart chart graph

There is no MWE because all I have, for now, is this image (created, I believe, by SPSS, but I have nothing to do with that part.) I could simply add this image to my document, of course, but since these issues come up with some frequency I'd like to try a "native" LaTeX way, as it were. I am pretty sure some graphics package is more than up to the task, but I don't even know where to start looking.

I don't expect you to do the work for me, so all I'm asking for, really, is a pointer in the right direction, an FAQ, a HOWTO, perhaps a sample.

share|improve this question
3  
It seems to a job for pgfplots to me. Also search the [pgfplots] tag on this site for examples. –  Peter Jansson Apr 6 '13 at 9:05
3  
A thing you need to think about: In which form(at) is the data available for TeX? Otherwise the tag pgfplots gives you plenty of examples. Also check pgfplotstable. –  Qrrbrbirlbel Apr 6 '13 at 9:07
    
@Qrrbrbirlbel This is a small sample, so data would be input by hand in the source code (for now). –  Ingmar Apr 6 '13 at 9:13
2  
you could also check out R in combination with knitr. The setup is a bit difficult but it's very powerful. –  ralfix Apr 6 '13 at 9:45

5 Answers 5

up vote 9 down vote accepted

This is not a full answer, only a pointer that assumes no automated handling of the data is required.

EDIT: as recommended by Jake, using \addplot table instead of \addplot plot coordinates saves a lot of hassle.

enter image description here

\documentclass{book}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\definecolor{mybarcolor}{RGB}{210 207 155}
\pgfmathsetmacro{\xmin}{-.5}
\begin{axis}[ymin=0,%
    xmin=\xmin,%
    xtick={0,2,4,6},%
    xticklabels={.00,2.00,4.00,6.00},%
    axis background/.style={fill=black!10},%
    ylabel=\textbf{H\"{a}ufigkeit},%
    xlabel=$D_\mathrm{rel}$,%
]
\addplot[ybar interval,fill=mybarcolor] table {%
0   7
.5  38
1   23
1.5 8
2   8
2.5 8
3   2
3.5 1
4   2
4.5 0
5   0
5.5 1
6   1
};
\addplot[domain=\xmin:6,samples=100] {18*exp(-.5*(x-1.5)^2)};
\end{axis}
\end{tikzpicture}
\end{document}
share|improve this answer
7  
+1, very nice. I would recommend using \addplot table with an inline table instead of coordinates, which saves you the effort of typing all the parentheses. –  Jake Apr 6 '13 at 9:44
    
I think we can safely assume that $D_\mathrm{rel}$ is meant here. :) –  Qrrbrbirlbel Apr 6 '13 at 10:25
    
@Qrrbrbirlbel Yes. I was just trying to stick to the original as much as possible, to the point of being silly :) –  Jubobs Apr 6 '13 at 10:50

Here is a slightly modified example from the Asymptote manual. It not only creates an image from statistical data automatically, but also demonstrates how to generate the data in the first place. Process the following hist.asy file with asy -f pdf hist.asy.

import graph;
import stats;
size(400,200,IgnoreAspect);

real lineWidth=2pt;
pen histFillPen=rgb(217/255, 206/255, 143/255)+opacity(0.7);
pen histLinePen=black;
pen linePen=red+lineWidth; 
pen bgPen=rgb(241/255, 240/255, 238/255); 

int n=10000;
real[] a=new real[n];
for(int i=0; i < n; ++i) a[i]=Gaussrand();

path g=graph(Gaussian,min(a),max(a));

pair gMin=min(g);
pair gMax=max(g)+(0,0.05);

filldraw(box(gMin,gMax),bgPen,black);

draw(g,linePen);

// Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto.
int N=bins(a);
histogram(a,min(a),max(a),N,normalize=true,low=0,fillpen=histFillPen,drawpen=histLinePen,bars=true);

xaxis("$x$",BottomTop,RightTicks);
yaxis("$dP/dx$",LeftRight,RightTicks(trailingzero));

enter image description here

share|improve this answer

If you are willing to use another tool to create the plot as PDF and include it in your TeX, I will definitely suggest gnuplot. It is quite powerful and you can virtually graph anything you wish with it. For automated creation of the plot, you will need to include the gnuplot command in your LaTeX Makefile or sequence. You may also parameterize the datafile if you would like to generate multiple similar plots from multiple datafiles.

share|improve this answer
4  
You can invoke gnuplot directives from pgfplots. –  percusse Apr 6 '13 at 15:24
    
Welcome to TeX.sx! –  texenthusiast Apr 6 '13 at 15:42
    
Thanks for both of you :) –  Bichoy Apr 6 '13 at 15:45
1  
I agree with the gnuplot approach, it's pretty easy and you can get awesome results –  Nico Apr 6 '13 at 19:01

I'm currently creating statistical plots from data. Here is how I do it.

I read the data from a database (PostgreSQL in my case) into R, using RPostgreSQL (R has other database adapters). Or course you can use other databases or read from a CSV file, but databases are nice if you want to preprocess the data before importing into R.

Then I create graphs using the R package ggplot2, which is currently the graphing method du jour in R circles, and must be among the most powerful graphing utilities in existence. Check the ggplot2 tag on Stack Overflow. As Benjamin Mako Hill wrote:

I make my graphs in ggplot2 which is so trendy that I feel that mentioning this is a sort of R-hipster confession.

Finally, I use RTikZDevice, which provides a TikZ driver for R to generate a TikZ LaTeX file for inclusion in a LaTeX document. The results look pretty good.

It is worth noting that the Python graphics library Matplotlib, as of 1.2, also has a TikZ/PGF driver. See Typesetting With XeLaTeX/LuaLaTeX. While I don't think Matplotlib has anything to equal ggplot2 in terms of sophistication, it can certainly plot histograms, and may be a viable alternative to R in some situations.

The obvious reason for preferring this approach to simply inserting a PDF generated from R or Matplotlib, is that by using PGF/TikZ, we get better integrated graphics, which use the same fonts as the enclosing document. It is possible the resulting graphics are also of better quality, but I have not made a careful comparison.

share|improve this answer

MWE

I think that creating a LateX document with images (and text results) generated automatically from statistical data is work for R with Sweave or knitr.

In brief, you can make a normal LateX document that has the extension .Rnw instead of .tex (noweb file) with inserted "chunks" of R code. Then Sweave in R exports this as a true .tex file, where the R chunks are changed by LateX code with the results (images, tables or text) generated by these R chunks, that you can compile with pdflatex as usual.

For example, a minimal test.Rnw file with a minimal R chunk could be:

\documentclass{article}
\begin{document}
<<>>=
2+3
@
\end{document}

The above file with a .tex extension and compiled with pdflatex will produced simply:

<<>>= 2+3 @

But converted with R CMD Sweave test.Rnw you will obtain this test.tex:

\documentclass{article}
\usepackage{Sweave}
\begin{document}
\begin{Schunk}
\begin{Sinput}
> 2+3
\end{Sinput}
\begin{Soutput}
[1] 5
\end{Soutput}
\end{Schunk}

That compiled (with pdflatex test.tex) will produce:

> 2+3

[1] 5

Well, not very impressive, just show a simple sum as seen in a R console. But R is a fantastic statistical program, and the result of a R chunk could be a \includegraphics{} with a nice graph, or a complete LaTeX table float (\begin{table} ... ), with or without showing the input. Even results that are simple numbers can be plugged into the text with \Sexpr (example: \Sexpr{2+3}}. Thus, for instance to generate periodical statistical LaTeX reports, only modifying the original data managed by R (a simple .csv file, for example) with two commands or simple script you can obtain automatically updated graphs, but also updated tables and updated results in plain text. Without editing LaTeX code every time. And without open R. That is enough impressive now?.

The above screen capture was generated with this more elaborated hist.Rnw file:

% File: hist.Rnw
%
% Usage:  
%
% R CMD Sweave hist.Rnw 
% pdflatex hist.tex

\documentclass{article}
\usepackage[utf8]{inputenc} 
\usepackage{lipsum} 
\begin{document}

\lipsum[1] % dummy text

\begin{figure}[h]   
\centering

% R Chunk 
<<Histogran,echo=F,fig=T>>=

# Some random data
foo <- rnorm(1000, mean=1, sd=1)

# The histogram 
hist(foo, prob=TRUE, border="tan4", ylab="Häufigkeit",
xlab="D_rel",col=rainbow(25,start=.4,end=.35), main="")

# To have also the ugly background area (optional): 
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =  "lightgrey") 

hist(foo, prob=TRUE, border="tan4", ylab="Häufigkeit", xlab="D_rel",col=rainbow(40,start=.1,end=.4), main="",
add=T) 

# Density plot
curve(dnorm(x, mean=mean(foo), sd=sd(foo)), lwd=2, col="darkred", add=T)
@ % end of R chunk

\caption{This is a histogram of foo population with mean=\Sexpr{round(mean(foo),2)} and sd=\Sexpr{round(sd(foo),2)}. On the other hand,  significance of Shapiro-Wilk test of normality is p=\Sexpr{signif(shapiro.test(foo)$p.value,2)}, so this is a normal population as sure as 2+2=\Sexpr{2+2}, as everybody knows (except some processors with floating point bugs). }

\end{figure}

\lipsum[3] % more dummy text

\end{document}
share|improve this answer
    
Thanks for chiming in. It's an interesting approach, but it will take some time to read up on it all. Luckily my requirements aren't that high for the time being ... –  Ingmar Apr 6 '13 at 18:42
1  
@Ingmar, R compared with SPSS is like LateX compared with Word. At first it need some hard learning curve, but it is awesome and saving time. R+LaTeX is a childplay and it cannot be compared with anything. –  Fran Apr 6 '13 at 18:53
    
Hi Fran. Made some minor grammar corrections, but I'm stumped by "where the R chunks are changed by LateX code with the results (images, tables or text) generated with this code, that you can compile with pdflatex as usual." Not sure exactly what you meant to say here. –  Faheem Mitha Apr 7 '13 at 8:08
    
@FaheemMitha. I think that this mean that it said. Please see the edited answer. I hope that now is more understandable. –  Fran Apr 7 '13 at 12:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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