Example: Scatterplot

Published 2007-08-02 | Author: Ista Zahn

A scatterplot of the relationship between education and income. The plotted data is from the car R package by John Fox. Note that the data is released under the GPL 2 licence.

Author:Ista Zahn

Download as: [PDF] [TEX]

Scatterplot
% Author:  Ista Zahn
\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}[only marks, y=.5cm]
    \draw plot[mark=*,xshift=-6cm] file {data/ScatterPlotExampleData.data};
    \draw[->,xshift=-6cm] (6,0) -- coordinate (x axis mid) (17,0);
    \draw[->,xshift=-6cm] (6,0) -- coordinate (y axis mid)(6,27);
    \foreach \x in {6,8,10,12,14,16}
        \draw [xshift=-6cm](\x cm,1pt) -- (\x cm,-3pt)
            node[anchor=north] {$\x$};
    \foreach \y/\ytext in {0/0,2.5/5000,5/10000,7.5/15000,10/20000,12.5/25000}
        \draw (1pt,\y cm) -- (-3pt,\y cm) node[anchor=east] {$\ytext$};
    \node[below=1cm] at (x axis mid) {Education};
    \node[left=2cm,rotate=90] at (y axis mid) {Income};
\end{tikzpicture}

\end{document}

Comments

  • #1 Claudio, May 7, 2010 at 4:50 p.m.

    The label on the y axis is not centered on the axis. The code in question is

    \node[left=2cm,rotate=90] at (y axis mid) {Income};
    

    [left=2cm] moves the reference point 2cm left from (y axis mid) and sets [anchor=east] which then by 90° rotation is positionned north.

    To fix it (even if not very intuitive):

    \node[rotate=90, above=2cm] at (y axis mid) {Income};
    

    The node is first rotated, then in the rotated environment moved 2cm "up" (left) and anchor is set to "south" (east).

    Best Claudio

Post a comment

Markdown syntax enabled. No HTML allowed.