![Editar](https://rvso.com/image/328472/Editar.png)
Tengo el problema de que en el siguiente gráfico falta el nodo derecho cerca del texto de coordenadas de A. La fuente del problema es un error de redondeo en los datos: deberían sumar 100 pero en A es un poco más. ¿Cuál sería la mejor manera de evitar esto? Simplemente redondear A hacia abajo no es una opción. Tengo toneladas de gráficos de este tipo, con más columnas, y en casi todos hay una fila con una suma demasiado grande.
\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{pgfplotstable}
\pgfplotsset{
my stackbar plot/.style={
xbar stacked,
xmin=0,xmax=100,
symbolic y coords={A,B},
ytick=data,
nodes near coords={xxx},}}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=space]{
text -- -
B 50 50
A 50.01 50
}\data
\begin{axis}[my stackbar plot,]
\addplot table [x expr = \thisrow{--},y=text] {\data};
\addplot table [x expr = \thisrow{-}, y=text] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
Respuesta1
Decidí que la solución más sensata es normalizar los valores. Aquí una versión simplificada del código que utilicé al final:
\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{pgfplotstable}
\pgfplotsset{
my stackbar plot/.style={
xbar stacked,
xmin=0,xmax=100,
symbolic y coords={A,B},
ytick=data,
nodes near coords={xxx},}}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=space]{
text -- -
B 50 50
A 50.01 50
}\data
\pgfplotstablecreatecol[create col/expr={\thisrow{--}+\thisrow{-}}]{sum}\data
\pgfplotstablecreatecol[create col/expr={\thisrow{--}/\thisrow{sum}*100}]{--}\data
\pgfplotstablecreatecol[create col/expr={\thisrow{-}/\thisrow{sum}*100}]{-}\data
\begin{axis}[my stackbar plot,]
\addplot table [x expr = \thisrow{--},y=text] {\data};
\addplot table [x expr = \thisrow{-}, y=text] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
Editar
Pruebas adicionales revelaron que normalizar no es suficiente. El total aún puede ser un poco mayor que 100 y, por lo tanto, la marca podría perderse. Es necesario truncar los valores, verpgfplots: corregir problemas de redondeo.
Respuesta2
También es posible normalizar sólo valores en filas que no sumen 100:
\documentclass[]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.13}
\usepackage{pgfplotstable}
\pgfplotsset{
my stackbar plot/.style={
xbar stacked,
xmin=0,xmax=100,
symbolic y coords={A,B},
ytick=data,
nodes near coords={xxx},}}
\begin{document}
\begin{tikzpicture}
\pgfplotstableread[col sep=space]{
text -- -
B 50 50
A 50.01 50
}\data
\pgfplotstablecreatecol[create col/expr={\thisrow{--}+\thisrow{-}}]{sum}\data
% normalize only if the sum is not 100
\pgfplotsinvokeforeach{--,-}{%
\pgfplotstablemodifyeachcolumnelement{#1}\of\data\as\wert{%
\pgfplotstablegetelem{\pgfplotstablerow}{sum}\of\data%
\pgfmathparse{\pgfplotsretval==100?\wert:\wert*100/\pgfplotsretval}%
\edef\wert{\pgfmathresult}
}%
}
\begin{axis}[my stackbar plot,]
\addplot table [x expr = \thisrow{--},y=text] {\data};
\addplot table [x expr = \thisrow{-}, y=text] {\data};
\end{axis}
\end{tikzpicture}
\end{document}
De la misma manera es posible cambiar sólo los valores en las filas que suman más de 100 usando
% normalize only if the sum is >100
\pgfplotsinvokeforeach{--,-}{%
\pgfplotstablemodifyeachcolumnelement{#1}\of\data\as\wert{%
\pgfplotstablegetelem{\pgfplotstablerow}{sum}\of\data%
\pgfmathparse{\pgfplotsretval<=100?\wert:\wert*100/\pgfplotsretval}%
\edef\wert{\pgfmathresult}
}%
}