3

I'm having some trouble using meta in pgfplots. I want to add an asterisk above some of the bars to show significance. I've plotted my bar plots from a table and I've tried assigning a column of the table to correspond to meta but it doesn't seem to work and after a good search I can't find anyone who has answered this question...

Check out my code at the bottom. I would be super grateful for some help.

Thank you for your time.

\usepackage{graphicx}
\usepackage{pgfplots}
\usepackage{float}
\usepackage{tikz}
\usepackage{caption}
\begin{figure}[h]
\begin{subfigure}[b]{1\textwidth}
    \centering
        \begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[
    legend pos=north west,
    xlabel= Monensin Treatment (hrs),
    ylabel= GAPDH Normalised Expression,
    bar width={1cm},
    ymin=0,
    ymax=8,
    title = {PENK Expression},
    width=12cm,
    height=6cm,
    ytick={0,0.5,1,1.5},
    ymin=0,
    ymax=1.5,
    xtick={1,2,3,4},
    xticklabels={%
        Ctrl,
        1,
        6,
        24
        },
    grid=none,
    ybar,  enlarge x limits={abs=1},
    ]

\addplot[
    fill=blue!25,
    draw=black,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit,
        point meta=explicit symbolic,
] 
table [y error=error, point meta=meta] {
x   y       error    label  meta

1   1.0128   0.07    1 
2   0.371  0.055     2      ***
3   0.30   0.0941    3      
4   0.224  0.0348    4      
};
%\node [above, black, font=\Large] at (axis cs: 0.371) {$\ast$}
\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\caption{}
\end{subfigure}
\\\vspace{0.5cm}
\begin{subfigure}[b]{1\textwidth}
    \centering
        \begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[
    legend pos=north west,
    ylabel= Ct Mean,
    xlabel= Monensin Treatment (hrs),
    bar width={1cm},
    title = {GAPDH Control},
    width=12cm,
    height=6cm,
    ytick={0,10,20,30},
    ymin=0,
    ymax=30,
    xtick={1,2,3,4},
    xticklabels={%
        Ctrl,
        1,
        6,
        24
        },
    grid=none,
    ybar,  enlarge x limits={abs=1},
    ]

\addplot[
    fill=red!25,
    draw=black,
    point meta=y,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error] {
x   y           error    label

1   20.45   4.152    1
2   23.54  0.368     2 
3   22.71   0.492    3
4   23.82  0.221     4
};

\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\caption{}
\end{subfigure}
\label{fig:penkexpression}
\caption{}
\end{figure}
6

Did that code actually work for you without errors?

Anyways, the crucial thing you're missing is nodes near coords. It is this option that actually enables printing of a meta value above the bars.

Further:

  • As you want to print something that isn't a number, and that you provide in the table, you need to add point meta=explicit symbolic, as you have done in the first axis, but you must not place it after /error bars/.cd.

  • In the \addplot table options, you need to provide meta=<column name>, not point meta=<column name>.

  • I had to add a {} in the empty cells in the meta column.

output of code

\documentclass{article}
\usepackage{pgfplots}
\usepackage{subcaption}
\begin{document}
\begin{figure}
\begin{subfigure}[b]{1\textwidth}
    \centering
        \begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[
    legend pos=north west,
    xlabel= Monensin Treatment (hrs),
    ylabel= GAPDH Normalised Expression,
    bar width={1cm},
    ymin=0,
    ymax=8,
    title = {PENK Expression},
    width=12cm,
    height=6cm,
    ytick={0,0.5,1,1.5},
    ymin=0,
    ymax=1.5,
    nodes near coords,
    xtick={1,2,3,4},
    xticklabels={%
        Ctrl,
        1,
        6,
        24
        },
    grid=none,
    ybar,  enlarge x limits={abs=1},
    ]

\addplot[
    fill=blue!25,
    draw=black,
    point meta=explicit symbolic,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit,     
] 
table [y error=error,meta=meta] {
x   y       error    label  meta
1   1.0128   0.07    1      {}
2   0.371  0.055     2      ***
3   0.30   0.0941    3      {}  
4   0.224  0.0348    4      {}
};
%\node [above, black, font=\Large] at (axis cs: 0.371) {$\ast$}
\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\caption{}
\end{subfigure}
\\[0.5cm]
\begin{subfigure}[b]{1\textwidth}
    \centering
        \begin{tikzpicture}[trim axis left, trim axis right]
\begin{axis}[
    legend pos=north west,
    ylabel= Ct Mean,
    xlabel= Monensin Treatment (hrs),
    bar width={1cm},
    title = {GAPDH Control},
    width=12cm,
    height=6cm,
    ytick={0,10,20,30},
    ymin=0,
    ymax=30,
    xtick={1,2,3,4},
    xticklabels={%
        Ctrl,
        1,
        6,
        24
        },
    grid=none,
    ybar,  enlarge x limits={abs=1},
    nodes near coords
    ]

\addplot[
    fill=red!25,
    draw=black,
    point meta=explicit symbolic,
    every node near coord/.style={inner ysep=5pt},
    error bars/.cd,
        y dir=both,
        y explicit
] 
table [y error=error,meta=label] {
x   y           error    label
1   20.45   4.152    1
2   23.54  0.368     2 
3   22.71   0.492    3
4   23.82  0.221     4
};

\draw ({rel axis cs:0,0}|-{axis cs:0,0}) -- ({rel axis cs:1,0}|-{axis cs:0,0});
\end{axis}
\end{tikzpicture}
\caption{}
\end{subfigure}
\label{fig:penkexpression}
\caption{}
\end{figure}
\end{document}
| improve this answer | |
  • Perfect!! I didn't get any errors when I run this code... thank youfor your help – user110968 Sep 14 '17 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.