如何在表格内绘制堆积条形图?

如何在表格内绘制堆积条形图?

我在 Latex 中有以下代码。它仅绘制一个数据系列 (data2)。我想在表格内为 data1 和 data2 绘制不同颜色的条形图。

有任何想法吗。?

非常感谢。

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}

% The data
\begin{filecontents}{data.txt}
accession,data1,data2
C1,150708.4564,481.0388003
C2,49652.53112,275.2190163
C3,37379.1284,58.85806921
C4,34165.85698,26.06571636
C5,30856.19158,35.96802428
C6,15204.22768,26.71594573
\end{filecontents}


\pgfplotstableread[col sep=comma]{data.txt}\data

\newcommand{\errplot}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=5cm,
  enlarge y limits={abs=0.5},
  axis y line*=middle,
  ytick=\empty,
  axis x line*=bottom,
  xbar,
  bar width=1.5ex,
  xmin=0,
  visualization depends on=x \as \rawx,
  nodes near coords,
  every node near coord/.style={
  anchor=east,
  shift={(axis direction cs:-\rawx,0)}
  }
 ]
% 

\addplot [draw=black, fill=red]
 table [x=data2,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}

\begin{document}

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

% Print the table
\pgfplotstabletypeset[columns={accession,data1,data2},
  % Booktabs rules
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=[3ex]\bottomrule},
  % Set header name
  columns/accession/.style={string type,column type=l,column name=accession},
  columns/data1/.style={string type,column type=l,column name=data1}, 
  columns/data2/.style={
    column name={Data2},
    assign cell content/.code={% use \multirow for Z column:
   \ifnum\pgfplotstablerow=0
   \pgfkeyssetvalue{/pgfplots/table/@cell content}
   {\multirow{\numberofrows}{6.5cm}{\errplot}}%
   \else
   \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
   \fi
  }
 },
]{\data}
\end{document}

答案1

\newcommand为每个data说法定义\newcommand{\errplota}{}\newcommand{\errplotb}{}

\documentclass{article}
\usepackage{booktabs}
\usepackage{multirow}
\usepackage{pgfplotstable}
\usepackage{filecontents}

% The data
\begin{filecontents}{data.txt}
accession,data1,data2
C1,150708.4564,481.0388003
C2,49652.53112,275.2190163
C3,37379.1284,58.85806921
C4,34165.85698,26.06571636
C5,30856.19158,35.96802428
C6,15204.22768,26.71594573
\end{filecontents}


\pgfplotstableread[col sep=comma]{data.txt}\data

\newcommand{\errplota}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=5cm,
  enlarge y limits={abs=0.5},
  axis y line*=middle,
  ytick=\empty,
  axis x line*=bottom,
  xbar,
  bar width=1.5ex,
  xmin=0,
  visualization depends on=x \as \rawx,
  nodes near coords,
  every node near coord/.style={
  anchor=east,
  shift={(axis direction cs:-\rawx,0)}
  }
 ]
% 
\addplot [draw=black, fill=red]
 table [x=data1,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}
\newcommand{\errplotb}{%
\begin{tikzpicture}[trim axis right]
\begin{axis}[y=-\baselineskip,
  scale only axis,
  width=5cm,
  enlarge y limits={abs=0.5},
  axis y line*=middle,
  ytick=\empty,
  axis x line*=bottom,
  xbar,
  bar width=1.5ex,
  xmin=0,
  visualization depends on=x \as \rawx,
  nodes near coords,
  every node near coord/.style={
  anchor=east,
  shift={(axis direction cs:-\rawx,0)}
  }
 ]
% 
\addplot [draw=black, fill=yellow]
 table [x=data2,y expr=\coordindex]{\data};
\end{axis}
\end{tikzpicture}%
}

\begin{document}

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

% Print the table
\pgfplotstabletypeset[columns={accession,data1,data2},
  % Booktabs rules
  every head row/.style={before row=\toprule,after row=\midrule},
  every last row/.style={after row=[3ex]\bottomrule},
  % Set header name
  columns/accession/.style={string type,column type=l,column name=accession},
  columns/data1/.style={
    column name={Data1},
    assign cell content/.code={% use \multirow for Z column:
   \ifnum\pgfplotstablerow=0
   \pgfkeyssetvalue{/pgfplots/table/@cell content}
   {\multirow{\numberofrows}{6.5cm}{\errplota}}%
   \else
   \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
   \fi
  },}, 
  columns/data2/.style={
    column name={Data2},
    assign cell content/.code={% use \multirow for Z column:
   \ifnum\pgfplotstablerow=0
   \pgfkeyssetvalue{/pgfplots/table/@cell content}
   {\multirow{\numberofrows}{6.5cm}{\errplotb}}%
   \else
   \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
   \fi
  },
}
]{\data}
\end{document}

在此处输入图片描述

相关内容