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

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de. En français: TeXnique.fr.

% 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

  • #2 RolPasto, May 24, 2012 at 6:30 p.m.

    I think it would make sense to change the following line:

    \draw[->,xshift=-6cm] (6,0) -- coordinate (y axis mid)(6,27);

    for this one:

    \draw[->] (0,0) -- coordinate (y axis mid)(0,14);

    and remove the option of the tikzpicture "y=0.5cm"

    Bye

  • #3 MJ, June 9, 2013 at 11:48 p.m.

    How can i add a trend line in such a scatterplot?

Adding comments is currently not enabled.