3

I am trying to change the axis y line location from 0 to 100 from these questions (1, 2). Say that the error bars represent confidence intervals on percentage scale, so 100% is no difference. Here is what I get, if I just change the values for data.txt:

enter image description here

As you see, the dashed line is at 0 and is drawn on the left side of the plot. Here is the code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
\usepackage{siunitx}

%% The data files
\begin{filecontents}{data.txt}
name  z p mean  lci uci
{\textbf{Variable A}} {}  {}  {}  {}  {}
Afear -0.96 0.33  98.2  89.3  107.5
Anofear 0.09  0.93  100.1 90.2  110.8
B+2 0.29  0.78  88.3  62.8  115.9
B+1 0.84  0.4 92.1  86.3  105.3
B1:1  2.19  0.03  50.2  40.6  60.8
B-1 1.02  0.31  89.2  75.2  101.2
B-2 -0.1  0.92  120.3 95.2  137.8
C+2 -1.11 0.27  110.5 89.2  130.5
C+1 1.15  0.25  105.8 78.6  115.6
\end{filecontents}

% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{data.txt}\data
\pgfplotstableset{create on use/error/.style={
    create col/expr={\thisrow{uci}-\thisrow{mean}
    }
  }
}

% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplot}[1]{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=6.5cm,
  enlarge y limits={abs=0.5},
  axis y line*=middle,
  y axis line style=dashed,
  ytick=\empty,
  axis x line*=bottom]
% 
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{#1};
\end{axis}
\end{tikzpicture}%
}

\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[multicolumn names,
columns={name,mean,error,z,p},
  % Booktabs rules
  every head row/.style={before row=\toprule, after row=\midrule},
  every last row/.style={after row=[1ex]},
  % Set header name
  columns/name/.style={string type,column name={}},
    % Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
  columns/error/.style={
    column name={$\bar{x} \pm$ 95\% CI},
    assign cell content/.code={% use \multirow for Z column:
    \ifnum\pgfplotstablerow=1
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{6.5cm}{\errplot{\data}}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
  columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
  columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
  ]{\data}
\end{table}
\end{document}

If I follow the solution in Jake's answer to this question, I get a following table:

enter image description here

So it seems that the empty row somehow confuses pgfplots. Yet, I would like to keep the empty row. I feel that I am very close to a solution, but cannot figure out how to do this. Could someone kindly help me?

Here is the code for the second table:

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
\usepackage{siunitx}

%% The data files
\begin{filecontents}{data.txt}
name  z p mean  lci uci
{\textbf{Variable A}} {}  {}  {}  {}  {}
Afear -0.96 0.33  98.2  89.3  107.5
Anofear 0.09  0.93  100.1 90.2  110.8
B+2 0.29  0.78  88.3  62.8  115.9
B+1 0.84  0.4 92.1  86.3  105.3
B1:1  2.19  0.03  50.2  40.6  60.8
B-1 1.02  0.31  89.2  75.2  101.2
B-2 -0.1  0.92  120.3 95.2  137.8
C+2 -1.11 0.27  110.5 89.2  130.5
C+1 1.15  0.25  105.8 78.6  115.6
\end{filecontents}

% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{data.txt}\data
\pgfplotstableset{create on use/error/.style={
    create col/expr={\thisrow{uci}-\thisrow{mean}
    }
  }
}

\pgfplotsset{
    axis line origin/.style args={#1,#2}{
        x filter/.code=\pgfmathparse{\pgfmathresult-#1},
        y filter/.code=\pgfmathparse{\pgfmathresult-#2},
        xticklabel=\pgfmathparse{\tick+#1}\pgfmathprintnumber{\pgfmathresult},
        yticklabel=\pgfmathparse{\tick+#2}\pgfmathprintnumber{\pgfmathresult}
    }
}

% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplot}[1]{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=6.5cm,
  enlarge y limits={abs=0.5},
  axis y line*=middle,
  axis line origin={100,0},
  y axis line style=dashed,
  ytick=\empty,
  axis x line*=bottom]
% 
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{#1};
\end{axis}
\end{tikzpicture}%
}

\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[multicolumn names,
columns={name,mean,error,z,p},
  % Booktabs rules
  every head row/.style={before row=\toprule, after row=\midrule},
  every last row/.style={after row=[1ex]},
  % Set header name
  columns/name/.style={string type,column name={}},
    % Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
  columns/error/.style={
    column name={$\bar{x} \pm$ 95\% CI},
    assign cell content/.code={% use \multirow for Z column:
    \ifnum\pgfplotstablerow=1
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{6.5cm}{\errplot{\data}}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
  columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
  columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
  ]{\data}
\end{table}
\end{document} 
Mikko
  • 735

2 Answers2

2

It is just a line after all, draw it yourself:

\draw[dashed](axis cs:100,0)--(axis cs:100,100);

provided that the "official" axis has been removed:

axis y line=none,

The complete code:

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
\usepackage{siunitx}

%% The data files
\begin{filecontents}{data.txt}
name  z p mean  lci uci
{\textbf{Variable A}} {}  {}  {}  {}  {}
Afear -0.96 0.33  98.2  89.3  107.5
Anofear 0.09  0.93  100.1 90.2  110.8
B+2 0.29  0.78  88.3  62.8  115.9
B+1 0.84  0.4 92.1  86.3  105.3
B1:1  2.19  0.03  50.2  40.6  60.8
B-1 1.02  0.31  89.2  75.2  101.2
B-2 -0.1  0.92  120.3 95.2  137.8
C+2 -1.11 0.27  110.5 89.2  130.5
C+1 1.15  0.25  105.8 78.6  115.6
\end{filecontents}

% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{data.txt}\data
\pgfplotstableset{create on use/error/.style={
    create col/expr={\thisrow{uci}-\thisrow{mean}
    }
  }
}

% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplot}[1]{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=6.5cm,
  enlarge y limits={abs=0.5},
  axis y line=none,
  ytick=\empty,
  axis x line*=bottom,
  ]

% 
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{#1};

\draw[dashed](axis cs:100,0)--(axis cs:100,100);

\end{axis}
\end{tikzpicture}%
}

\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[multicolumn names,
columns={name,mean,error,z,p},
  % Booktabs rules
  every head row/.style={before row=\toprule, after row=\midrule},
  every last row/.style={after row=[1ex]},
  % Set header name
  columns/name/.style={string type,column name={}},
    % Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
  columns/error/.style={
    column name={$\bar{x} \pm$ 95\% CI},
    assign cell content/.code={% use \multirow for Z column:
    \ifnum\pgfplotstablerow=1
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{6.5cm}{\errplot{\data}}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
  columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
  columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
  ]{\data}
\end{table}
\end{document}

The result:

enter image description here

  • 1
    Both solutions work perfectly, but this one is more straightforward so I will accept it. For others with a similar problem: see also Jake's solution. – Mikko Feb 05 '14 at 09:10
2

You can adjust the axis line origin style to only apply to non-empty entries:

\pgfplotsset{
    axis line origin/.style args={#1,#2}{
        x filter/.code={
                \ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#1}\fi
            },
        y filter/.code={
                \ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#2}\fi
            },
        xticklabel=\pgfmathparse{\tick+#1}\pgfmathprintnumber{\pgfmathresult},
        yticklabel=\pgfmathparse{\tick+#2}\pgfmathprintnumber{\pgfmathresult}
    }
}

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}
\usepackage{xspace}
\usepackage{tabularx}
\usepackage{siunitx}

%% The data files
\begin{filecontents}{data.txt}
name  z p mean  lci uci
{\textbf{Variable A}} {}  {}  {}  {}  {}
Afear -0.96 0.33  98.2  89.3  107.5
Anofear 0.09  0.93  100.1 90.2  110.8
B+2 0.29  0.78  88.3  62.8  115.9
B+1 0.84  0.4 92.1  86.3  105.3
B1:1  2.19  0.03  50.2  40.6  60.8
B-1 1.02  0.31  89.2  75.2  101.2
B-2 -0.1  0.92  120.3 95.2  137.8
C+2 -1.11 0.27  110.5 89.2  130.5
C+1 1.15  0.25  105.8 78.6  115.6
\end{filecontents}

% Read data files, create new column ``upper CI boundary - mean''
\pgfplotstableread{data.txt}\data
\pgfplotstableset{create on use/error/.style={
    create col/expr={\thisrow{uci}-\thisrow{mean}
    }
  }
}

\pgfplotsset{
    axis line origin/.style args={#1,#2}{
        x filter/.code={
                \ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#1}\fi
            },
        y filter/.code={
                \ifx\pgfmathresult\empty\else\pgfmathparse{\pgfmathresult-#2}\fi
            },
        xticklabel=\pgfmathparse{\tick+#1}\pgfmathprintnumber{\pgfmathresult},
        yticklabel=\pgfmathparse{\tick+#2}\pgfmathprintnumber{\pgfmathresult}
    }
}


% Define the command for the plot (I had to do this separately for each dataset)
\newcommand{\errplot}[1]{%
\begin{tikzpicture}[trim axis left,trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=6.5cm,
  enlarge y limits={abs=0.5},
  axis line origin={100,0},
  axis y line*=middle,
  y axis line style=dashed,
  ytick=\empty,
  axis x line*=bottom]
% 
% ``mean'' must be present in the datafile, ``error'' is the newly generated column
\addplot+[only marks][error bars/.cd,x dir=both, x explicit]
table [x=mean,y expr=\coordindex,x error=error]{#1};
\end{axis}
\end{tikzpicture}%
}

\begin{document}
% Get number of rows in datafile
\pgfplotstablegetrowsof{\data}
\let\numberofrows=\pgfplotsretval

\begin{table}
\caption{Table caption.}
% Print the table
\pgfplotstabletypeset[multicolumn names,
columns={name,mean,error,z,p},
  % Booktabs rules
  every head row/.style={before row=\toprule, after row=\midrule},
  every last row/.style={after row=[1ex]},
  % Set header name
  columns/name/.style={string type,column name={}},
    % Use the ``error'' column to call the \errplot command in a multirow cell in the first row, keep empty for all other rows
  columns/error/.style={
    column name={$\bar{x} \pm$ 95\% CI},
    assign cell content/.code={% use \multirow for Z column:
    \ifnum\pgfplotstablerow=1
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{6.5cm}{\errplot{\data}}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/mean/.style={column name=$\bar{x}$, string type, column type={S[table-format=-2.2]}},
  columns/z/.style={column name=$z$, string type, column type={S[table-format=-1.2]}},
  columns/p/.style={column name=$p$, string type, column type={S[table-format=1.2]}},
  ]{\data}
\end{table}
\end{document}
Jake
  • 232,450