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 trying to plot a certain number as a function of year for different entities. In this example, it is the number of missions to the Moon for different agencies per year. What I have come up with so far is the following:

Plot until now.

The associated code is:

\begin{tikzpicture}
\begin{axis}[
             width=0.8\textwidth,
             height=0.25\textheight,
             stack plots=y,
             area style,
             xlabel={Year},             
             ylabel={Number of missions},
             enlarge x limits=false,             
             enlarge y limits=upper,                      
             legend pos=outer north east,
             x tick label style={/pgf/number format/1000 sep=},                   ] 
% USA
\addplot coordinates
{(1958,4) (1959,3) (1960,2) (1962,3) (1963,0)
 (1964,2) (1965,2) (1966,4) (1967,7) (1968,2)
 (1969,3) (1970,2) (1971,3) (1972,3) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0) 
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,1)
 (1995,0) (1996,0) (1997,0) (1998,1)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,3) (2010,0)
 (2011,1) } \closedcycle; 
 \addlegendentry{USA}
% Soviets/Russia
\addplot coordinates
{(1958,3) (1959,4) (1960,2) (1962,0) (1963,3)
 (1964,2) (1965,7) (1966,6) (1967,0) (1968,4)
 (1969,5) (1970,5) (1971,2) (1972,1) (1973,2)
 (1974,2) (1975,0) (1976,1)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } \closedcycle; 
 \addlegendentry{Russia}

% Japan 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0) 
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0) 
 (1990,1) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } \closedcycle; 
 \addlegendentry{Japan}
% ESA 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0) 
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,1) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } \closedcycle; 
 \addlegendentry{Europe}
% China 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
 (2008,0) (2009,0) (2010,1)
 (2011,0) } \closedcycle; 
 \addlegendentry{China}
% India 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,2) (2009,0) (2010,0)
 (2011,0) } \closedcycle; 
 \addlegendentry{India}
\end{axis}
\end{tikzpicture}

However, I find this representation highly unsuitable, although it is the best I could come up with. If there is only one entry per year it creates this odd triangle. Moreover, some parts appear wrong, like the plot for India.

What would be an appropriate way of plotting this?

share|improve this question
    
This is quite irrelevant to the question but I think that the filling under the curves is confusing. One wonders if the baseline for each region is the abscissa axis or the curve just under. –  Alfred M. Oct 8 '12 at 14:42
4  
I think that from a statistical point of view it is unprecise to connect the points in this way, because there are only discrete and no continuous values for each year. Not like (1958,3) (1958.5,3.5) (1959,4) for example, which is what the graphic is actually showing. I would tend to use bars in this context. See this image for an example of what I mean (juxtaposition of both). –  brian-ammon Oct 8 '12 at 15:13

2 Answers 2

up vote 2 down vote accepted

For small numbers like these, it might be a good idea to use one symbol per event:

I used Matlab to massage the data into a suitable format using the following script (data is assumed to contain the tabulated data, with the years in the first column and the numbers of missions in the consecutive columns):

numberOfCountries = size(data,2)-1;
Launches=data(:,2:end);

baseLevels = cumsum(Launches,2);
baseLevels = [ zeros(length(data),1) baseLevels];

maxLaunches = max(sum(Launches));
cumLaunches = [zeros(1,numberOfCountries); cumsum(Launches)];

Years=nan(maxLaunches,size(data,2)-1);
Levels=Years;

for country = 1:numberOfCountries
   for j = 1:length(Launches)
       Years(cumLaunches(j,country)+1:cumLaunches(j+1,country),country) = ...
           repmat(data(j,1),Launches(j,country),1);
       Levels(cumLaunches(j,country)+1:cumLaunches(j+1,country),country) = ...
           baseLevels(j,country) + (1:Launches(j,country))';
   end
end

a=Years';
b=Levels';
Output=reshape([a(:) b(:)]',2*size(a,1), [])';

The output can than either be written to a file or just used as a table directly within the .tex document:

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}
\usetikzlibrary{decorations.shapes}
\begin{document}
\definecolor{Color1}{RGB}{55, 126, 184}
\definecolor{Color2}{RGB}{228, 26, 28}
\definecolor{Color3}{RGB}{77, 175, 74}
\definecolor{Color4}{RGB}{152, 78, 163}
\definecolor{Color5}{RGB}{255, 127, 0}
\definecolor{Color6}{RGB}{255, 255, 51}

\pgfplotstableread{
USAyear USAlevel    Russiayear Russialevel  Japanyear Japanlevel    Europeyear Europelevel  Chinayear Chinalevel    Indiayear Indialevel
1958    1   1958    5   1989    1   2002    1   2006    2   2007    1
1958    2   1958    6   2006    1   NaN NaN 2009    1   2007    2
1958    3   1958    7   NaN NaN NaN NaN NaN NaN NaN NaN
1958    4   1959    4   NaN NaN NaN NaN NaN NaN NaN NaN
1959    1   1959    5   NaN NaN NaN NaN NaN NaN NaN NaN
1959    2   1959    6   NaN NaN NaN NaN NaN NaN NaN NaN
1959    3   1959    7   NaN NaN NaN NaN NaN NaN NaN NaN
1960    1   1960    3   NaN NaN NaN NaN NaN NaN NaN NaN
1960    2   1960    4   NaN NaN NaN NaN NaN NaN NaN NaN
1961    1   1962    1   NaN NaN NaN NaN NaN NaN NaN NaN
1961    2   1962    2   NaN NaN NaN NaN NaN NaN NaN NaN
1961    3   1962    3   NaN NaN NaN NaN NaN NaN NaN NaN
1963    1   1963    3   NaN NaN NaN NaN NaN NaN NaN NaN
1963    2   1963    4   NaN NaN NaN NaN NaN NaN NaN NaN
1964    1   1964    3   NaN NaN NaN NaN NaN NaN NaN NaN
1964    2   1964    4   NaN NaN NaN NaN NaN NaN NaN NaN
1965    1   1964    5   NaN NaN NaN NaN NaN NaN NaN NaN
1965    2   1964    6   NaN NaN NaN NaN NaN NaN NaN NaN
1965    3   1964    7   NaN NaN NaN NaN NaN NaN NaN NaN
1965    4   1964    8   NaN NaN NaN NaN NaN NaN NaN NaN
1966    1   1964    9   NaN NaN NaN NaN NaN NaN NaN NaN
1966    2   1965    5   NaN NaN NaN NaN NaN NaN NaN NaN
1966    3   1965    6   NaN NaN NaN NaN NaN NaN NaN NaN
1966    4   1965    7   NaN NaN NaN NaN NaN NaN NaN NaN
1966    5   1965    8   NaN NaN NaN NaN NaN NaN NaN NaN
1966    6   1965    9   NaN NaN NaN NaN NaN NaN NaN NaN
1966    7   1965    10  NaN NaN NaN NaN NaN NaN NaN NaN
1967    1   1967    3   NaN NaN NaN NaN NaN NaN NaN NaN
1967    2   1967    4   NaN NaN NaN NaN NaN NaN NaN NaN
1968    1   1967    5   NaN NaN NaN NaN NaN NaN NaN NaN
1968    2   1967    6   NaN NaN NaN NaN NaN NaN NaN NaN
1968    3   1968    4   NaN NaN NaN NaN NaN NaN NaN NaN
1969    1   1968    5   NaN NaN NaN NaN NaN NaN NaN NaN
1969    2   1968    6   NaN NaN NaN NaN NaN NaN NaN NaN
1970    1   1968    7   NaN NaN NaN NaN NaN NaN NaN NaN
1970    2   1968    8   NaN NaN NaN NaN NaN NaN NaN NaN
1970    3   1969    3   NaN NaN NaN NaN NaN NaN NaN NaN
1971    1   1969    4   NaN NaN NaN NaN NaN NaN NaN NaN
1971    2   1969    5   NaN NaN NaN NaN NaN NaN NaN NaN
1971    3   1969    6   NaN NaN NaN NaN NaN NaN NaN NaN
1993    1   1969    7   NaN NaN NaN NaN NaN NaN NaN NaN
1997    1   1970    4   NaN NaN NaN NaN NaN NaN NaN NaN
2008    1   1970    5   NaN NaN NaN NaN NaN NaN NaN NaN
2008    2   1971    4   NaN NaN NaN NaN NaN NaN NaN NaN
2008    3   1972    1   NaN NaN NaN NaN NaN NaN NaN NaN
2010    1   1972    2   NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1973    1   NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1973    2   NaN NaN NaN NaN NaN NaN NaN NaN
NaN NaN 1975    1   NaN NaN NaN NaN NaN NaN NaN NaN
}\datatable



\begin{tikzpicture}
\begin{axis}[
    compat=1.6,
    y=3mm,
    x=3mm,
    xmin=1957, xmax=2011,
    ymin=0,
    only marks, mark size=4, every axis plot post/.style={draw=black},
    y filter/.code=\pgfmathparse{#1-0.5},
    xlabel={Year},             
    ylabel={Number of missions},           
    enlarge y limits=upper,                      
    legend pos=outer north east,
    x tick label style={/pgf/number format/1000 sep=},
    xtick align=outside,
] 
% USA
\addplot [Color1] table [x=USAyear, y=USAlevel] {\datatable};
\addlegendentry{USA}
\addplot [Color2] table [x=Russiayear, y=Russialevel] {\datatable};
\addlegendentry{Russia}
\addplot [Color3] table [x=Japanyear, y=Japanlevel] {\datatable};
\addlegendentry{Japan}
\addplot [Color4] table [x=Europeyear, y=Europelevel] {\datatable};
\addlegendentry{Europe}
\addplot [Color5] table [x=Chinayear, y=Chinalevel] {\datatable};
\addlegendentry{China}
\addplot [Color6] table [x=Indiayear, y=Indialevel] {\datatable};
\addlegendentry{India}
\end{axis}
\end{tikzpicture}

\end{document}
share|improve this answer
    
This is perfect. Thanks! –  Ingo Oct 12 '12 at 8:27

I came up with this, which is decent although far from perfect I think.

Improved chart.

\begin{tikzpicture}
\begin{axis}[
             width=0.8\textwidth,
             height=0.25\textheight,
             ybar stacked,
             ytick={0,1,...,10},            
             %stack plots=y,
             %area style,
             xlabel={Year},             
             ylabel={Number of missions},
             enlarge x limits=0.05,             
             enlarge y limits=upper,                      
             legend pos=outer north east,
             x tick label style={/pgf/number format/1000 sep=},                  
             bar width=3pt, ] 
% USA
\addplot coordinates
{(1958,4) (1959,3) (1960,2) (1962,3) (1963,0)
 (1964,2) (1965,2) (1966,4) (1967,7) (1968,2)
 (1969,3) (1970,2) (1971,3) (1972,3) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0) 
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,1)
 (1995,0) (1996,0) (1997,0) (1998,1)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,3) (2010,0)
 (2011,1) } ;% \closedcycle; 
 \addlegendentry{USA}
% Soviets/Russia
\addplot coordinates
{(1958,3) (1959,4) (1960,2) (1962,0) (1963,3)
 (1964,2) (1965,7) (1966,6) (1967,0) (1968,4)
 (1969,5) (1970,5) (1971,2) (1972,1) (1973,2)
 (1974,2) (1975,0) (1976,1)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } ;% \closedcycle; 
 \addlegendentry{Russia}

% Japan 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0) 
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0) 
 (1990,1) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } ;% \closedcycle; 
 \addlegendentry{Japan}
% ESA 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0) 
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,1) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,0) (2009,0) (2010,0)
 (2011,0) } ;% \closedcycle; 
 \addlegendentry{Europe}
% China 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,1)
 (2008,0) (2009,0) (2010,1)
 (2011,0) } ;% \closedcycle; 
 \addlegendentry{China}
% India 
 \addplot coordinates
{(1958,0) (1959,0) (1960,0) (1962,0) (1963,0)
 (1964,0) (1965,0) (1966,0) (1967,0) (1968,0)
 (1969,0) (1970,0) (1971,0) (1972,0) (1973,0)
 (1974,0) (1975,0) (1976,0)
 (1977,0) (1978,0) (1979,0) (1980,0) (1981,0)
 (1982,0) (1983,0) (1984,0) (1985,0) (1986,0)  
 (1987,0) (1988,0) (1989,0)
 (1990,0) (1991,0) (1992,0) (1993,0) (1994,0)
 (1995,0) (1996,0) (1997,0) (1998,0)
 (1999,0) (2000,0) (2001,0) (2002,0)
 (2003,0) (2004,0) (2005,0) (2006,0) (2007,0)
 (2008,2) (2009,0) (2010,0)
 (2011,0) } ;% \closedcycle; 
 \addlegendentry{India}
\end{axis}
\end{tikzpicture}
share|improve this answer

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.