1

I would like to add significance bars (as depicted in the image below) for the pgfplot barplot that I coded. I have so far worked with "nodes", just adding asterisks but bars would be preferred.

Thank you for your help :-)

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.7}
\usepackage{tikz}
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage{wrapfig}

\usepackage{geometry} \geometry{ a4paper, total={170mm,257mm}, left=20mm, top=20mm, }

\usepackage{lipsum}

\title{Example} \author{Name } \date{April 2021}

\begin{document}

\maketitle

\section{Introduction}

\begin{figure*}[ht] \centering \resizebox{\textwidth}{!}{% \begin{subfigure}[pt]{0.5\textwidth}

\begin{tikzpicture} \begin{axis}[ ybar, %bar width=.5cm, domain=0:1, every axis plot/.append style={no markers}, %xlabel=Timepoints (d), ylabel= Frequency of Bursts, width=\textwidth, height=7cm, ymax=6, ymin=1, xmin=-0.5, xmax=1.5, xticklabels={DIV15, DIV17},xtick={0,1}, x tick label style={rotate=90}, legend style={at={(0.025,0.875)},anchor=west,legend columns=2}] %NMC %NMC \addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%NmC H (0,3.7) +- (0.17,0.17) (1,4.2) +- (0.28,0.28)
}; \addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%aS L (0,3.3) +- (0.4,0.4) (1,3.4) +- (0.25,0.25) };
\addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%NmC H (0,3.7) +- (0.17,0.17) (1,4.1) +- (0.18,0.18) };
\addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {% aS H (0,2.3) +- (0.1,0.1) (1,3.1) +- (0.35,0.35) };
%NMC %NMC %\node [above, font=\Large] at (axis cs: 0.21,300) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,265) {$\ast$};
\legend{\scriptsize{NmCherry L},\scriptsize{$\alpha$-syn L},\scriptsize{NmCherry H},\scriptsize{$\alpha$syn H}} \end{axis} \end{tikzpicture}
%\caption{Cell viability post-treatment} %\label{fig:sub1} \end{subfigure}

\hspace{8mm}


\begin{subfigure}[pt]{0.5\textwidth} \begin{tikzpicture} \begin{axis}[ ybar, %bar width=.5cm, domain=0:1, every axis plot/.append style={no markers}, %xlabel=Timepoints (d), ylabel= % of Active Neurones, width=\textwidth, height=7cm, ymax=80, ymin=10, xmin=-0.5, xmax=1.5, xticklabels={DIV15, DIV17},xtick={0,1}, x tick label style={rotate=90}, legend style={at={(0.45,0.78)},anchor=west}] %# of BURSTS %# of BURSTS \addplot+[fill,error bars/.cd,%NmC L y dir=both,y explicit] coordinates { (0,58) +- (3.7,3.7) (1,65) +- (1.9,1.9) }; \addplot+[fill,error bars/.cd,%aS L y dir=both,y explicit] coordinates { (0,30) +- (3.4,3.4) (1,51) +- (2.9,2.9) };
\addplot+[fill,error bars/.cd,%NmC H y dir=both,y explicit] coordinates { (0,57) +- (4.2,4.2) (1,60) +- (3.5,3.5) };
\addplot+[fill,error bars/.cd,%aS H y dir=both,y explicit] coordinates { (0,19) +- (2.9,2.9) (1,39) +- (3.8,3.8) };
%\node [above, font=\Large] at (axis cs: 0.21,2.8) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,3.6) {$\ast$};
%# of BURSTS %# of BURSTS %\legend{\scriptsize{NmCherry$^{+}$-Low},\scriptsize{$\alpha$syn-NmC$^{+}$-Low},\scriptsize{NmCherry$^{+}$-High},\scriptsize{$\alpha$syn-NmC$^{+}$-High}} \end{axis} \end{tikzpicture}
%\caption{Network burst activity post-treatment} %\label{fig:sub1} \end{subfigure}} %\decoRule \ \caption[]{\ %\decoRule \ \footnotesize{\textit{.}}} \label{fig:3.52} \end{figure*}

\end{document}

Juan Castaño
  • 28,426

1 Answers1

1

Complete code at the end, after a small wall of text.


Well, drawing a line from a to be would be the same as in normal TikZ:

\draw (a) -- (b);  % if a and be are named coordinates
\draw (axis cs:0,0) -- (axis cs:1,1); % for explicit coordinates

If you have compat=1.11 or higher (you currently have 1.7), then axis cs: becomes the default, so \draw (0,0) -- (1,1); would do the same as the above.

To add a node above the middle of such a line, do

\draw (0,0) -- node[above] {foo} (1,1);

For the "hooks" at the end, you could do something like https://tex.stackexchange.com/a/203830/, or draw them manually with e.g. \draw (0,-3pt) |- (1,0) -- (1,-3pt);, and there may be other solutions around the site as well. In the code below I defined a new line style using the arrows.meta library and

\tikzset{
  % define new arrow tip, which is just a straight line
  bar/.tip={Rectangle[length=1pt, width=3pt]},
  % use the new arrow tip, but only on one side of the line
  % left/right is when standing in the middle of the line looking towards
  % the end of the line, hence right on one and left on the other
  signbarL/.style={
    {bar[right]}-{bar[left]}
  }
}

Which isn't fantastic, as the line has to be drawn from right to left, but works for this case.

Having come this far, you mostly just need to figure out which coordinates to use. For the y-coordinate, I guess just look at the diagram, between 4 and 4.5 seems fine for the first one, for example.

For the x-coordinate you could just do some trial and error to find appropriate values, knowing that the center of the two groups are at 0 and 1. Or make use of the fact that the width of the bars is saved in \pgfplotbarwidth, and the space between the bars is 2pt (unless you change it).

Using \pgfplotbarwidth directly in an explicit coordinate didn't work, but it does work for relative coordinates, hence the use of a construction like

\draw [signbarL] (axis cs:0,4.1) % set point at middle of first bar group, at y=4.1
                  ++(2*\pgfplotbarwidth+3pt, 0) % move point 2 bar widths + 1.5 bar space to the right, without drawing a line
                  --  % draw a line to the next coordinate
                  +(-2*\pgfplotbarwidth-2pt, 0); % end point is 2 bar widths and one bar space left of the previous point

A bit complicated perhaps, if logical. As mentioned, you could do some trial and error to arrive at e.g. \draw [signbarL] (axis cs:0.235,4.45) --node[above]{$\ast$} (axis cs:-0.235, 4.45);, if you prefer that.


A couple of other things: pgfplotstable loads pgfplots which loads tikz which loads graphicx, so strictly speaking you only need to load the first of these. (Doesn't really hurt to load the rest I think, if a package is loaded already, the second time nothing is done, if I remember correctly.)

For subfigure the valid options for the (first) optional argument are one of b, B, c (default), t and T, so pt is not valid. subfigure is not a floating environment like figure, the optional argument has to do with how the environment is anchored on the baseline, not with selecting allowed positions on the page. Of course, if you're not adding subcaptions, you don't need the subfigure environments at all.

Finally while I see that it's easy to dump everything in a \resizebox to make it fit in the text area, personally I'd set the width of each axis to a reasonable value, so nothing gets additional scaling.

Sorry about the rant :), complete code below.

enter image description here

\documentclass{article}
\usepackage{pgfplotstable} % loads pgfplots which loads tikz which loads graphicx
\pgfplotsset{compat=1.7}
\usetikzlibrary{arrows.meta}
\usepackage{subcaption}
\usepackage{wrapfig}

\usepackage{geometry} \geometry{ a4paper, total={170mm,257mm}, left=20mm, top=20mm, }

\tikzset{ bar/.tip={Rectangle[length=1pt, width=3pt]}, signbarL/.style={ {bar[right]}-{bar[left]} } }

\begin{document}

\begin{figure*}[ht] % \centering % doesn't do much, since the content is \textwidth anyways \resizebox{\textwidth}{!}{% \begin{subfigure}{0.5\textwidth}

\begin{tikzpicture} \begin{axis}[ ybar, %bar width=.5cm, domain=0:1, every axis plot/.append style={no markers}, %xlabel=Timepoints (d), ylabel= Frequency of Bursts, width=\textwidth, height=7cm, ymax=6, ymin=1, xmin=-0.5, xmax=1.5, xticklabels={DIV15, DIV17},xtick={0,1}, x tick label style={rotate=90}, legend style={ at={(0.025,0.875)}, anchor=west, legend columns=2, nodes={font=\scriptsize} }] %NMC %NMC \addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%NmC H (0,3.7) +- (0.17,0.17) (1,4.2) +- (0.28,0.28)
}; \addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%aS L (0,3.3) +- (0.4,0.4) (1,3.4) +- (0.25,0.25) };
\addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {%NmC H (0,3.7) +- (0.17,0.17) (1,4.1) +- (0.18,0.18) };
\addplot+[fill,error bars/.cd, y dir=both,y explicit] coordinates {% aS H (0,2.3) +- (0.1,0.1) (1,3.1) +- (0.35,0.35) };
%NMC %NMC %\node [above, font=\Large] at (axis cs: 0.21,300) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,265) {$\ast$};

\draw [signbarL] (axis cs:0,4.1) ++(2\pgfplotbarwidth+3pt, 0) -- +(-2\pgfplotbarwidth-2pt, 0); \draw [signbarL] (axis cs:0,4.25) ++(2\pgfplotbarwidth+3pt, 0) -- +(-3\pgfplotbarwidth-4pt, 0); \draw [signbarL] (axis cs:0,4.4) ++(2\pgfplotbarwidth+3pt, 0) --node[above]{$\ast$} +(-4\pgfplotbarwidth-6pt, 0);

\legend{NmCherry L,$\alpha$-syn L,NmCherry H,$\alpha$syn H} \end{axis} \end{tikzpicture}
% \caption{Cell viability post-treatment} % \label{fig:sub1} \end{subfigure}

\hspace{8mm}


\begin{subfigure}{0.5\textwidth} \begin{tikzpicture} \begin{axis}[ ybar, %bar width=.5cm, domain=0:1, every axis plot/.append style={no markers}, %xlabel=Timepoints (d), ylabel= % of Active Neurones, width=\textwidth, height=7cm, ymax=80, ymin=10, xmin=-0.5, xmax=1.5, xticklabels={DIV15, DIV17},xtick={0,1}, x tick label style={rotate=90}, legend style={at={(0.45,0.78)},anchor=west}] %# of BURSTS %# of BURSTS \addplot+[fill,error bars/.cd,%NmC L y dir=both,y explicit] coordinates { (0,58) +- (3.7,3.7) (1,65) +- (1.9,1.9) }; \addplot+[fill,error bars/.cd,%aS L y dir=both,y explicit] coordinates { (0,30) +- (3.4,3.4) (1,51) +- (2.9,2.9) };
\addplot+[fill,error bars/.cd,%NmC H y dir=both,y explicit] coordinates { (0,57) +- (4.2,4.2) (1,60) +- (3.5,3.5) };
\addplot+[fill,error bars/.cd,%aS H y dir=both,y explicit] coordinates { (0,19) +- (2.9,2.9) (1,39) +- (3.8,3.8) };
%\node [above, font=\Large] at (axis cs: 0.21,2.8) {$\ast$};
%\node [above, font=\Large] at (axis cs: 1.21,3.6) {$\ast$};
%# of BURSTS %# of BURSTS %\legend{\scriptsize{NmCherry$^{+}$-Low},\scriptsize{$\alpha$syn-NmC$^{+}$-Low},\scriptsize{NmCherry$^{+}$-High},\scriptsize{$\alpha$syn-NmC$^{+}$-High}} \end{axis} \end{tikzpicture}
%\caption{Network burst activity post-treatment} %\label{fig:sub1} \end{subfigure}} %\decoRule \ \caption[]{\ %\decoRule \ \footnotesize{\textit{.}}} \label{fig:3.52} \end{figure*} \end{document}

Torbjørn T.
  • 206,688