6

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]

\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
    {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}
    \tkzKiviatGrad[suffix=\%,unity=10](0)
\end{tikzpicture}


\end{document}   
3
  • Welcome to TeX.sx! Commented Apr 6, 2013 at 15:41
  • Have you written intentionally Other, ? because the result is different with Other. Commented Apr 7, 2013 at 7:25
  • There is a similar question, assuming that Spider Graph and Kiviat Diagram are synonyms (like supposed in wikipedia). Therefore here is another possibility with a package: Draw Kiviat Diagramms
    – TimK
    Commented Jun 7, 2015 at 7:03

3 Answers 3

6

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]

\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
    {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}
    \tkzKiviatGrad[suffix=\%,unity=10](0)
\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} 

enter image description here

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]

\tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
    {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}
    \tkzKiviatGrad[suffix=\%,unity=10](0)
\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.

2
  • Since \captionof is integrated into KOMA-Script, too, there is no need to load the capt-of package here.
    – user2574
    Commented Apr 7, 2013 at 6:46
  • @AxelSommerfeldt true! I'll edit right now. Thank you. Commented Apr 7, 2013 at 6:52
3

With PSTricks.

enter image description here

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

\makeatletter
\def\LoadVerbs{\pstVerb{/pt2cm {\strip@pt\psunit\space div} bind def}}
\makeatother

\psset{opacity=0.5,fillstyle=solid}     

\def\Atom#1%
{
    \multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
    \psline{->}(!#1 12 pt2cm add 0)
}

\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%
{
    \psframe[linecolor=#3,fillcolor=#3](#1,#2)(!#1 12 pt2cm add #2 12 pt2cm add)
    \rput[l](!#1 18 pt2cm add #2 6 pt2cm add){#3 description}
}

\addtopsstyle{gridstyle}{griddots=0}
\begin{document}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\LoadVerbs
\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}

How is it made?

enter image description here

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

\makeatletter
\def\LoadVerbs{\pstVerb{/pt2cm {\strip@pt\psunit\space div} bind def}}
\makeatother

\psset{opacity=0.5,fillstyle=solid}     

\def\Atom#1%
{
    \multido{\i=1+1}{#1}{\psline[linecolor=gray](\i,0)(0,\i)}
    \psline{->}(!#1 12 pt2cm add 0)
}

\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%
{
    \psframe[linecolor=#3,fillcolor=#3](#1,#2)(!#1 12 pt2cm add #2 12 pt2cm add)
    \rput[l](!#1 18 pt2cm add #2 6 pt2cm add){#3 description}
}

\addtopsstyle{gridstyle}{griddots=0}
\begin{document}
\begin{frame}
\begin{pspicture}[showgrid=false](-8,-8)(9,8)
\LoadVerbs
\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}
3
  • 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. Commented Apr 7, 2013 at 8:47
  • @AlainMatthes: I have made the interface friendlier. Commented Apr 7, 2013 at 16:14
  • Fine ! it's more friendly for the user. Commented Apr 7, 2013 at 16:56
3

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]
    \tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
        {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)
        \tkzKiviatGrad[suffix=\%,unity=10](0)
\end{scope}
\LegendBox[xshift=-2cm]{current bounding box.south east}%
          {red/red decription,
           blue/blue description,
           green/green }
\end{tikzpicture}
\end{document} 

enter image description here

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]
    \tkzKiviatDiagram[radial=3,lattice=7,gap=1,step=1,label space=2]%
        {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)
        \tkzKiviatGrad[suffix=\%,unity=10](0)
\end{scope}
\LegendBox[shift={(-3cm,3cm)}]{current bounding box.south east}%
          {red/red decription,
           blue/blue description,
           green/green description }
\end{tikzpicture}
\end{document} 

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .