# Spider Graph Legend and Caption

I would like to add: legend (Red, blue and Green) and figure caption (with legend) for my Keviat plot. The code of my keviat is listed below. Thank you

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]

{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\end{tikzpicture}

\end{document}

-
Welcome to TeX.sx! –  texenthusiast Apr 6 '13 at 15:41
Have you written intentionally Other,  ? because the result is different with Other. –  Alain Matthes Apr 7 '13 at 7:25

Here's one possibility. If you want your object to be treated as a floating object, you can use the figure environment and then the standard \caption command to provide the caption (as I did in my first example code). If you don't want your diagram to float, you can use a minipage environment and the \captionof command integrated into the KOMA-Script classes, to provide the caption (as I did in my second example code).

To design the legend, you can use a \node and a tabular, at the desired position; of course you can change the settings according to your needs.

First, treating the diagram as a floating figure object:

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand\ColorBox[1]{\textcolor{#1}{\rule{2ex}{2ex}}}

\begin{document}

\begin{figure}
\centering
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]

{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\node[anchor=south west,xshift=-60pt,yshift=40pt] at (current bounding box.south east)
{
\begin{tabular}{@{}lp{3cm}@{}}
\ColorBox{red!50} & red description \\
\ColorBox{green!50} & green description \\
\ColorBox{blue!50} & blue description \\
\end{tabular}
};
\end{tikzpicture}
\caption{A caption for the diagram}
\label{fig:test}
\end{figure}

\end{document}


And without flotation:

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{capt-of}
\usepackage{pgfplots}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand\ColorBox[1]{\textcolor{#1}{\rule{2ex}{2ex}}}

\begin{document}

\noindent\begin{minipage}{\linewidth}
\centering
\begin{tikzpicture}[label distance=.15cm,rotate=30,scale=0.75]

{Cover,
Droppings,
Other,
}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02);label{p4}
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06);label{p5}
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6);label{p6}
\node at (current bounding box.east)
{
\begin{tabular}{@{}ll@{}}
\ColorBox{red} & red description \\
\ColorBox{green} & green description \\
\ColorBox{blue} & blue description \\
\end{tabular}
};
\end{tikzpicture}
\captionof{figure}{A caption for the diagram}
\label{fig:test}
\end{minipage}

\end{document}


In the standard classes book, report, article, the \captionof command is accessible through one of the packages capt-of or caption.

-
Since \captionof is integrated into KOMA-Script, too, there is no need to load the capt-of package here. –  Axel Sommerfeldt Apr 7 '13 at 6:46
@AxelSommerfeldt true! I'll edit right now. Thank you. –  Gonzalo Medina Apr 7 '13 at 6:52

With PSTricks.

\documentclass[pstricks,border=12pt]{standalone}
\usepackage{pst-node}
\usepackage{multido}

\makeatletter
\makeatother

\psset{opacity=0.5,fillstyle=solid}

\def\Atom#1%
{
\multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
}

\def\Molecule#1%
{
\multido{\i=0+90}{4}{\rput{\i}(0,0){\Atom{#1}}}
\multido{\ix=1+1,\i=10+10}{#1}{\rput*{*0}(!\ix\space 6 pt2cm sub -10 pt2cm){\i\%}}
\foreach \a/\t in {0/Right,90/Top,180/Left,270/Bottom}{\pnode(!#1 42 pt2cm add \a\space PtoC){\t}}
}

\def\Label(#1,#2)#3%
{
}

\begin{document}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\rput{30}(0,0)
{
\Molecule{7}
\pspolygon[linecolor=red,fillcolor=red](0.75,0)(0,6.75)(-2,0)
\pspolygon[linecolor=green,fillcolor=green](3,0)(0,4.75)(-3.75,0)
\pspolygon[linecolor=blue,fillcolor=blue](6,0)(0,1.75)(-3,0)
\foreach \n/\t in {Right/Cover,Top/Droppings,Left/Other}{\rput{*0}(\n){\t}}%! removing white space
}
\foreach \y/\c in {-3/red,-4/green,-5/blue}{\Label(5,\y){\c}}
\end{pspicture}
\end{document}


\documentclass{beamer}
\usepackage{pst-node}
\usepackage{multido}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\makeatletter
\makeatother

\psset{opacity=0.5,fillstyle=solid}

\def\Atom#1%
{
\multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
}

\def\Molecule#1%
{
\multido{\i=0+90}{4}{\only<+->{\rput{\i}(0,0){\Atom{#1}}}}
\only<+->{\multido{\ix=1+1,\i=10+10}{#1}{\rput*{*0}(!\ix\space 6 pt2cm sub -10 pt2cm){\i}}}%!
\foreach \a/\t in {0/Right,90/Top,180/Left,270/Bottom}{\pnode(!#1 42 pt2cm add \a\space PtoC){\t}}%!
}

\def\Label(#1,#2)#3%
{
}

\begin{document}
\begin{frame}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\rput{30}(0,0)
{
\Molecule{7}%!
\only<+->{\pspolygon[linecolor=red,fillcolor=red](0.75,0)(0,6.75)(-2,0)}%!
\only<+->{\pspolygon[linecolor=green,fillcolor=green](3,0)(0,4.75)(-3.75,0)}%!
\only<+->{\pspolygon[linecolor=blue,fillcolor=blue](6,0)(0,1.75)(-3,0)}%!
\foreach \n/\t in {Right/Cover,Top/Droppings,Left/Other}{\only<+->{\rput{*0}(\n){\t}}}%! removing white space
}
\foreach \y/\c in {-3/red,-4/green,-5/blue}{\only<+->{\Label(5,\y){\c}}}
\end{pspicture}
\end{frame}
\end{document}

-
There is nothing to avoid code like !5 12 pt2cm add -5 12 pt2cm add it's not very funny for the user. Something to get the separation of presentation and content. –  Alain Matthes Apr 7 '13 at 8:47
@AlainMatthes: I have made the interface friendlier. –  please stop stalking Apr 7 '13 at 16:14
Fine ! it's more friendly for the user. –  Alain Matthes Apr 7 '13 at 16:56

The simplest method with tikz :

First I defined a point (LegendBox_anchor), it's an anchor for the legend's box. Then I used a line to show the color used for a tkzKiviatDiagram Line. The different lines are placed with (LegendBox_anchor). The description is a simple node add at the end of the line. anchor=west is used to align correctly the descriptions

\coordinate[xshift=-2cm] (LegendBox_anchor) at (current bounding box.south east) ;
\draw[line width=3mm,color=red] (LegendBox_anchor) -- ++(.5,0) node[anchor=west] {red description};
\draw[line width=3mm,color=blue] ([yshift=8mm]LegendBox_anchor) -- ++(.5,0) node[anchor=west] {blue description};
\draw[line width=3mm,color=green] ([yshift=16mm]LegendBox_anchor) -- ++(.5,0) node[anchor=west] {green description};


It's possible instead of lines to use rectangles or nodes. The nodes have several advantages to get some refinements but I tried to create something easy to use.

Another possibility is define a box around the different description. I can if you add this possibility later.

Now it's perhaps interesting to add a macro to the package to get legends. Here I added a simple macro ( first try to extend the package )

\newcommand{\LegendBox}[3][]{%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\draw[line width=3mm,color=\col] ([yshift=\hi*8mm]LegendBox_anchor) -- ++(.5,0)
node[anchor=west] {\item}
;}
}


It's based on the first lines. Argument #1 is used to get a better position of the legend box if it's necessary.

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand{\LegendBox}[3][]{%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\draw[line width=3mm,color=\col] ([yshift=\hi*8mm]LegendBox_anchor) -- ++(.5,0)
node[anchor=west] {\item}
;}
}

\begin{document}

\begin{tikzpicture}[label distance=.15cm,scale=0.75]
\begin{scope}[rotate=30]
{Cover,
Droppings,
Other,}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02)
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06)
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6)
\end{scope}
\LegendBox[xshift=-2cm]{current bounding box.south east}%
{red/red decription,
blue/blue description,
green/green }
\end{tikzpicture}
\end{document}


remark : you don't need to add ; at the end of \tkzKiviatLine

Update :

I added a box around the legends, removed some ; I used nodes instead of lines

\documentclass[]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tkz-kiviat,numprint,fullpage}
\usepackage{pgfplots}
\usetikzlibrary{arrows,fit}

\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

\newcommand{\LegendBox}[3][]{%
\xdef\fitbox{}%
\coordinate[#1] (LegendBox_anchor) at (#2) ;
\foreach \col/\item [count=\hi from 0] in {#3} {
\node[color = \col,draw,
fill  = \col!50,
minimum width  = 4 ex,
minimum height = 2 ex,
label={[anchor = left,name=b\hi]right:\item}] at ([yshift=\hi*4 ex]LegendBox_anchor) {};
\xdef\fitbox{\fitbox(b\hi)}
}%
\node [draw,fit=\fitbox(LegendBox_anchor)] {};
}

\begin{document}
\begin{tikzpicture}[label distance=.15cm,scale=0.75]
\begin{scope}[rotate=30]
{Cover,
Droppings,
Other}
\tkzKiviatLine[thick,color=red,fill=red,label=SiteA](0.78,5.59,2.02)
\tkzKiviatLine[thick,color=blue,fill=blue](5.92,1.57,3.06)
\tkzKiviatLine[thick,color=green,fill=green](2.9,4.6,3.6)