# Is there an elegant way to create categorial scatter plots with mean and error bars?

I have data for 4 substances (categories) from 5 indepent experiments. GraphPad Prism offer a diagramm type (see pic), which shows a scatter plot with some jitter, mean and error bars.

I used Jake's solution from this topic as a starting point and came up with the code shown as a minimal working example:

\documentclass{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8,
jitter/.style={
x filter/.code={\pgfmathparse{\pgfmathresult+rnd*#1}}
},
jitter/.default=0.1
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[symbolic x coords={A,B,C,D}, xtick={A,B,C,D},scatter/classes={
A={mark=square*,blue},
B={mark=triangle*,red},
C={mark=o,draw=black},
D={mark=*,draw=black}},];
scatter src=explicit symbolic,jitter=0.5,only marks,] coordinates {
(A,10) [A]
(A,12) [A]
(A,13) [A]
(A,11) [A]
(A,15) [A]
(B,90) [B]
(B,69) [B]
(B,75) [B]
(B,80) [B]
(B,85) [B]
(C,10) [C]
(C,5) [C]
(C,6) [C]
(C,7) [C]
(C,8) [C]
(D,40) [D]
(D,50) [D]
(D,45) [D]
(D,66) [D]
(D,39) [D]
};
red,
only marks,
mark=-,%
error bars/.cd,%
y dir=both, y explicit,%
] table[col sep=tab,y error=sd,] {
sample  mean    sd
A   12.200  1.923538
B   79.800  8.228001
C   7.200   1.923538
D   48.000  10.97725
};%
\end{axis}
\end{tikzpicture}
\end{document}


which gives:

The problems are:
1. The offset to the right due to the jitter. The points should be centered around the position of symbolic x mark.

1. The line of the mark for the mean is to short. It should be a little longer, then the scatter of points for each category.

2. The error bars are to short an should be a little longer at the ends. I am sure, I can figured it out using 'error bar style' myself. But I'd be most grateful, if someone already knows how to do this and shares his/her knowledge.

--> I solved 1.+3. this issue by defining:

\pgfdeclareplotmark{(-)}
{\draw[line width=0.4mm, black] (-30,0) -- (30,0);}

\pgfdeclareplotmark{(--)}
{\draw[line width=0.4mm, black] (-20,0) -- (20,0);}


and using the first as mark symbol and the latter as error mark.

Any advice would be great and appreciated.

• rnd generates numbers between 0 and 1, while rand generates numbers between -1 and 1 Aug 6, 2017 at 9:09

Thanks to Torbjorn T.'s comment I was able to solve this myself. I am grateful and here is the final code with it's ouput as picture:

\documentclass{scrartcl}
\usepackage{pgfplots}
\pgfplotsset{width=7cm,compat=1.8,
jitter/.style={
x filter/.code={\pgfmathparse{\pgfmathresult+rand*#1}}
},
jitter/.default=0.1
}
\pgfdeclareplotmark{(-)}
{\draw[line width=0.4mm, black] (-25,0) -- (25,0);}

\pgfdeclareplotmark{(--)}
{\draw[line width=0.4mm, black] (-15,0) -- (15,0);}

\begin{document}
\begin{tikzpicture}
\begin{axis}[symbolic x coords={A,B,C,D}, xtick={A,B,C,D},scatter/classes={
A={mark=diamond*, draw=black, fill=blue},
B={mark=diamond*, draw=black, fill=orange},
C={mark=diamond*,  draw=black, fill=black},
D={mark=diamond*,  draw=black, fill=green}},];
scatter src=explicit symbolic,jitter=0.2,only marks,] coordinates {
(A,10) [A]
(A,12) [A]
(A,13) [A]
(A,11) [A]
(A,15) [A]
(B,90) [B]
(B,69) [B]
(B,75) [B]
(B,80) [B]
(B,85) [B]
(C,10) [C]
(C,5) [C]
(C,6) [C]
(C,7) [C]
(C,8) [C]
(D,40) [D]
(D,50) [D]
(D,45) [D]
(D,66) [D]
(D,39) [D]
};
black,
only marks,
mark=(-),%
error bars/.cd,%
y dir=both, y explicit,error mark=(--), error bar style={line width=.4mm},%
] table[col sep=tab,y error=sd,] {
sample  mean    sd
A   12.200  1.923538
B   79.800  8.228001
C   7.200   1.923538
D   48.000  10.97725
};%
\end{axis}
\end{tikzpicture}
\end{document}