使用 tikzpicture 自定义 PGFplots 表的轴线位置

使用 tikzpicture 自定义 PGFplots 表的轴线位置

我正在尝试将axis y line这些问题中的位置从 0 更改为 100(12)。假设误差线代表百分比尺度的置信区间,因此 100% 没有区别。如果我只更改 的值,就会得到以下结果data.txt

在此处输入图片描述

如您所见,虚线位于 0 处,绘制在图的左侧。代码如下:

\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}

如果我按照 Jake 的回答中的解决方案这个问题,我得到下表:

在此处输入图片描述

因此,空行似乎在某种程度上混淆了 pgfplots。但是,我想保留空行。我觉得我离解决方案很近了,但不知道该怎么做。有人能帮帮我吗?

这是第二张表的代码:

\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} 

答案1

毕竟这只是一条线,自己画吧:

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

假设“官方”轴线已被移除:

axis y line=none,

完整代码:

\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}

结果:

在此处输入图片描述

答案2

您可以调整axis line origin样式以仅适用于非空条目:

\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}

相关内容