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.

  • 3
    It seems to a job for pgfplots to me. Also search the [pgfplots] tag on this site for examples. 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. 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. Apr 6 '13 at 9:45

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


\definecolor{mybarcolor}{RGB}{210 207 155}
    axis background/.style={fill=black!10},%
\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)};
  • 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. :) 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 :)
    – jub0bs
    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;

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);



// Optionally calculate "optimal" number of bins a la Shimazaki and Shinomoto.
int N=bins(a);


enter image description here


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.

  • 4
    You can invoke gnuplot directives from pgfplots.
    – percusse
    Apr 6 '13 at 15:24
  • Welcome to TeX.sx! Apr 6 '13 at 15:42
  • 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.



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. Then you can compile the .tex file with pdflatex as usual.

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


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:

> 2+3
[1] 5

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


\lipsum[1] % dummy text


% R Chunk 

# 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). }


\lipsum[3] % more dummy text

  • 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. 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

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.