# Plot a line over a bar chart from a data table

i have the following code:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\bein{document}
\begin{figure}
\centering
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
}
}
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct
\begin{tikzpicture}
\begin{axis}[ybar,
bar width=10pt,
enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
ylabel={Frequency},
xtick=data,
xticklabels = {
Gating1,
Gating2,
Gating3,
},
xlabel=Stimuli Condition,
xticklabel style={yshift=-10ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=1ex,
every node near coord/.append style={
anchor=east,
rotate=90
}
]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\end{axis}
\begin{axis}
[ybar,
enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
xtick=data,
xticklabels = {
},
]
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=1] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=2] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=3] \correct;
\addplot[draw=blue,ultra thick,only marks,mark=-,mark size=25pt] table[x index=0,y index=4] \correct;

\end{axis}
\end{tikzpicture}
\caption{Frequency and Proportion of Statement Responses by Stimuli Type and Gating Condition}\label{Statements}
\end{figure}
\end{document}


and the bar part alone is perfect, with the colours, and spacing etc. I am having problems with adding in the lines. The second data frame, 'correct', should sit over the bars, and be a plotted line with curves. Instead it sits to the right, as an extra set of data on the graph, and gives horizontal lines. I want to end up with something like this, with the lines over the bars, but hopefully with a smoother line.

• Please complete your code fragment to a small document, which we can compile as it is. – Zarko Mar 6 at 15:10
• I actually managed part of it, I put the lines over the bars but they are still horizontal, and not a plot. I have update the code in the original question – Helen Mar 6 at 15:19
• The updated code does not work, e.g. \bein{document} is wrong, but if one fixes this, there are still errors. – user194703 Mar 6 at 15:48
• @Schrödinger'scat, check \addplots, one is without backslash ... now i haven't time for anything more :-( – Zarko Mar 6 at 16:02
• thanks, i wondered where the 4th line was – Helen Mar 6 at 16:31

Is this what you are looking for?

\documentclass{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\begin{document}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
}
}
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct

\begin{tikzpicture}
\begin{axis}[ybar,
bar width=10pt,
enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
ylabel={Frequency},
xtick=data,
xticklabels = {
Gating1,
Gating2,
Gating3,
},
xticklabel style={yshift=-10ex},
xlabel=Stimuli Condition,
xlabel style={yshift=-10ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=1ex,
every node near coord/.append style={
anchor=east,
rotate=90
}
]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\end{axis}
\begin{axis}
[enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
xtick=data,
xticklabels = {
},
]
\addplot[draw=blue,ultra thick, xshift=-18pt] table[x index=0,y index=1] \correct;
\addplot[draw=red,ultra thick,xshift=-6pt] table[x index=0,y index=2] \correct;
\addplot[draw=green,ultra thick,xshift=6pt] table[x index=0,y index=3] \correct;
\addplot[draw=yellow,ultra thick,xshift=18pt] table[x index=0,y index=4] \correct;

\end{axis}
\end{tikzpicture}
\end{document}


Revised solution using \pgfplotstabletranspose. I store the starting points using coordinates (A) through (C). The xshift centers the data points on the bars, and the xscale was a WAG. I've tried computing the scale factor with no success.

\documentclass{standalone}
%\usepackage[utf8]{inputenc}
\usepackage{pgfplotstable}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}

\newlength{\tempdima}

\begin{document}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
}
}
1 829 794 552 401
2 1011 1104 843 620
3 658 755 546 428
}\dataset
1 425 404 319 227
2 511 575 411 341
3 327 386 298 211
}\correct
%\pgfplotstabletypeset{\correct}
\pgfplotstabletranspose[colnames from=0]{\transpose}{\correct}%
%\pgfplotstabletypeset{\transpose}
\begin{tikzpicture}
\begin{axis}[ybar,
bar width=10pt,
enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
ylabel={Frequency},
xtick=data,
xticklabels = {
Gating1,
Gating2,
Gating3,
},
xticklabel style={yshift=-10ex},
xlabel=Stimuli Condition,
xlabel style={yshift=-10ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=1ex,
every node near coord/.append style={
anchor=east,
rotate=90
}
]
\addplot[draw=black,fill=blue!20, nodes near coords=Active] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=red!20, nodes near coords=Interactive] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=green!20, nodes near coords=Passive] table[x index=0,y index=3] \dataset;
\addplot[draw=black,fill=yellow!20, nodes near coords=None] table[x index=0,y index=4] \dataset;
\coordinate (A) at (axis cs: 0,0);
\coordinate (B) at (axis cs: 1,0);
\coordinate (C) at (axis cs: 2,0);
\end{axis}
\begin{axis}
[enlarge x limits={abs=0.9},
width=12cm,
height=6cm,
ymin=0,
ymax=1200,
hide axis
]
\addplot[draw=black,ultra thick,shift=(A),xshift=4pt,xscale=0.2] table[x index=0,y index=1] \transpose;
\addplot[draw=black,ultra thick,shift=(B),xshift=4pt,xscale=0.2] table[x index=0,y index=2] \transpose;
\addplot[draw=black,ultra thick,shift=(C),xshift=4pt,xscale=0.2] table[x index=0,y index=3] \transpose;

\end{axis}
\end{tikzpicture}
\end{document}

• almost but not quite, I wanted just 3 lines, one per group of bars, not connecting the graphs, but thankyou for the code, I will try to amend it later when I work on the project again. I can probably correct it by rearranging the data table. Thanks so much for showing me how not to just have horizontal lines. – Helen Mar 9 at 8:40
• I can't make it work. I need the first part, the bars, to go across the groups, and the line to stay within the groups. Do you know how I can specify the gating condition in the code? I was just thinking to plot like {Gating1 {Active, 425} {Interactive, x} etc}. Is this possible? I didn't get a syntax to work. – Helen Mar 11 at 15:23
• Sorry, but I really don't understand what you are trying to do. – John Kormylo Mar 11 at 15:28
• i've added a quick sketch the original question as it was hard to explain. – Helen Mar 24 at 10:24
• Thankyou so much! I was starting to wonder if it was possible! – Helen Mar 25 at 9:10