Skip to content

Instantly share code, notes, and snippets.

@doraTeX
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doraTeX/10996136 to your computer and use it in GitHub Desktop.
Save doraTeX/10996136 to your computer and use it in GitHub Desktop.
東大化学設問数のTikZによるグラフ化 http://doratex.hatenablog.jp/entry/20140301/1393604168
\documentclass[landscape,dvipdfmx]{jsarticle}
\usepackage{graphicx,xcolor,tikz}
\usepackage[dvipdfm,margin=1cm]{geometry}
\pagestyle{empty}
\def\minYear{1987}
\def\maxYear{2014}
\def\xmin{\minYear-1900-0.5}
\def\xmax{\maxYear-1900+0.5}
%%% 折れ線グラフのデータとデータラベル出力位置
\def\lineChartData{
1987/23/above,
1988/19/below,
1989/20/below,
1990/25/above,
1991/24/above,
1992/23/{above,xshift=4pt},
1993/14/below,
1994/16/{above,xshift=-4pt},
1995/19/above,
1996/15/below,
1997/20/above,
1998/21/below,
1999/23/below,
2000/23/below,
2001/23/below,
2002/23/below,
2003/25/below,
2004/30/above,
2005/27/{above,xshift=4pt},
2006/23/below,
2007/27/above,
2008/27/below,
2009/29/above,
2010/26/below,
2011/32/above,
2012/31/below,
2013/36/{above,xshift=-4pt},
2014/40/below
}
%%% 積み上げ棒グラフのデータ
\def\barChartData{
1987/7/7/9,
1988/7/4/8,
1989/3/6/11,
1990/4/8/13,
1991/7/8/9,
1992/3/9/11,
1993/3/6/5,
1994/4/4/8,
1995/5/6/8,
1996/4/5/6,
1997/5/7/8,
1998/5/7/9,
1999/5/9/9,
2000/5/8/10,
2001/5/9/9,
2002/6/9/8,
2003/7/7/11,
2004/7/11/12,
2005/10/9/8,
2006/6/10/7,
2007/8/12/7,
2008/8/9/10,
2009/10/10/9,
2010/8/9/9,
2011/11/11/10,
2012/9/11/11,
2013/11/12/13,
2014/11/14/15
}
%%% 折れ線グラフの出力
\def\outputLineChartData#1{%
\def\points{}%
\foreach \x/\y/\pos in {#1}{%
\expandafter\node\expandafter[\pos, fill=white, inner sep=2pt, outer sep=5pt] at (\x-1900,\y) {\y};
\xdef\points{\points(\x-1900,\y)}
}
\draw[mark=*, mark size=3pt, line width=2pt, color=red] plot coordinates {\points};
}
\def\lineChart{\bgroup
\def\ymin{8}%
\def\ymax{40}%
\begin{center}
\makebox[0pt][c]{%
\begin{tikzpicture}[x=9mm,y=3mm]
\draw (\xmin,\ymax) -- (\xmin,\ymin) -- (\xmax,\ymin) -- (\xmax,\ymax);
\foreach \x in {1987,...,\maxYear}
\draw[font=\small] (\x-1900,\ymin) node[below] {\x} -- +(0,1);
\foreach \y in {10,15,...,40}{
\ifx\y\ymax\def\style{}\else\def\style{dotted}\fi
\draw[\style] (\xmin,\y) node[left] {\y} -- (\xmax,\y);
}
\expandafter\outputLineChartData\expandafter{\lineChartData}
\end{tikzpicture}}\par
{\Large 東大化学の設問総数の推移}
\end{center}
\egroup}
%%% 積み上げ棒グラフの出力
\def\outputBarChartData#1{%
\foreach \x/\a/\b/\c in {#1}{
\def\xx{\x-1900}
\fill[riron] (\xx-.5*\barwidth, \ymin) rectangle (\xx+.5*\barwidth, \a);
\fill[muki] (\xx-.5*\barwidth, \a) rectangle (\xx+.5*\barwidth, \a+\b);
\fill[yuki] (\xx-.5*\barwidth, \a+\b) rectangle (\xx+.5*\barwidth, \a+\b+\c);
\node[font=\sffamily] at (\xx,\a/2) {\a};
\node[font=\sffamily] at (\xx,\a+\b/2) {\b};
\node[font=\sffamily] at (\xx,\a+\b+\c/2) {\c};
}
}
\def\barChart{\bgroup
\def\ymin{0}%
\def\ymax{40}%
\begin{center}
\makebox[0pt][c]{%
\begin{tikzpicture}[x=9mm,y=3mm]
\foreach \x in {\minYear,...,\maxYear}
\node[font=\small,color=black,below] at (\x-1900,\ymin) {\x};
\foreach \y in {5,10,...,35}{
\draw[dotted] (\xmin,\y) node[left] {\y} -- (\xmax,\y);
}
\foreach \y in {\ymin,\ymax}
\node[left] at (\xmin, \y) {\y};
\expandafter\outputBarChartData\expandafter{\barChartData}
\draw (\xmin,\ymin) rectangle (\xmax,\ymax);
\node[rectangle,draw,right,font=\sffamily] at (\xmin+.5,\ymax-2)
{\legend{riron}{第1問(理論)}\hspace{1zw}\legend{muki}{第2問(無機)}\hspace{1zw}\legend{yuki}{第3問(有機)}};
\end{tikzpicture}}\par
{\Large 東大化学の大問別設問数推移}
\end{center}
\egroup}
%%% 積み上げ棒グラフの凡例
\def\legend#1#2{\textcolor{#1}{\rule[-.12zw]{2zw}{1zw}} #2}
\colorlet{riron}{blue!30!white}
\colorlet{muki}{green!30!white}
\colorlet{yuki}{red!30!white}
\def\barwidth{.5}
\begin{document}
\baselineskip22pt
\lineChart
\barChart
\end{document}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment