1

I want to make specific tick labels on yaxis to shift far enough so they do not overlap bars in a stacked bar chart, but also I would all labels to keep the same 'apparent' separation distance (this last condition I could accommodate other options).

I would also appreciate extra help to make xaxis detach from the image border (so it looks more symmetrical).

Below I attached an image to illustrate the issues I need to fix enter image description here

I believe it could be done by implementing 'visualization depends on' but not sure how to introduce this in the code.

    \documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.8,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}, 
 }
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
Species     A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0



}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},             
            ytick=data,     % Use as many tick labels as y coordinates
            yticklabels from table={\testdata}{Species}  % Get the labels from the Label column of the \datatable
]
\addplot [fill=black!20!black!40] table [x=A, meta=Species,y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, meta=Species,y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, meta=Species,y expr=\coordindex] {\testdata};
\end{axis}
\end{tikzpicture}

\end{document} 

Thank you in advance for the help, this community is amazing!

  • 1
    You are using a column Species in yticklabels from table={\testdata}{Species} and the point meta, but your table does not seem to have such a column. Could you please add it? – Schrödinger's cat Jan 17 at 3:03
  • Sorry about that, it was a mistake from trying to turn the code into a generic example. Thanks for letting me know, and for the help. I just left the original name for that column as 'Species'. – Emmo03 Jan 17 at 3:34
  • 1
    Well, I wrote an answer based on your original code. It adds these texts as nodes near coord rather than yticks because then a simple trick allows one to place them automatically where you want to have them. – Schrödinger's cat Jan 17 at 3:38
  • Thank you so much! I did not think of nodes near coord because I was planning to use them for displaying the actual values within the bars. Would that be conflicting with your solution? – Emmo03 Jan 17 at 3:43
  • 1
    I do not think that this will conflict because I added them to the last plot only. That is, if you introduce others, they will not overrule what is done in the last plot, nor does the last plot affect the previous ones. I also added a second, more mundane solution. – Schrödinger's cat Jan 17 at 3:48
1

Here is something that attaches these texts in the way you want them to attach. Unfortunately, at this point your code does not seem to run through because the table does not have the columns you use. So I made something up. The key point is that

\addplot [only marks,mark size=0pt,nodes near
    coords=text~\the\numexpr\coordindex+1,nodes near coords style={anchor=east}] 
    table[x expr={-0.01},y expr=\coordindex]  {\testdata};

automatically places the nodes where you seem to like them to be.

\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\pgfplotsset{compat=1.16,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}}
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
LABEL       A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0
}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$},             
            ytick=\empty,     % Use as many tick labels as y coordinates
            %yticklabels from table={\testdata}{Species}  % Get the labels from the Label column of the \datatable
            visualization depends on=\thisrow{LABEL}\as\myX,
]
\addplot [fill=black!20!black!40] table [x=A, y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,nodes near
    coords=text~\the\numexpr\coordindex+1,nodes near coords style={anchor=east}] 
    table[x expr={-0.01},y expr=\coordindex]  {\testdata};
\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

Or for your updated example: reading the content out of the Species row and using another trick.

\documentclass[border={0pt,0pt,0pt,0pt}]{standalone}%[a4paper]{article}
\usepackage{helvet}
\usepackage[eulergreek]{sansmath}
\usepackage{tikz,pgfplotstable}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.8,
 every axis/.style={axis on top},
 tick label style = {font=\sansmath\sffamily\small\itshape,xshift={-19}},
 every axis label = {font=\sansmath\sffamily},
 legend style = {font=\sansmath\sffamily},
 label style = {font=\sansmath\sffamily}, 
 }
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
Species     A       B       C
text1       3.67    2.16    2.27
text2       2.76    0       0
text3       2.52    0       1.94
text4       0       0.90    0
text5       0       0.85    1.69
text6       0       -3.81   0
text7       0       -1.09   0



}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=-8,         % Start x axis at 0
            xmax=8,         
            xticklabel style=transparent,    
            xtick style=transparent,
            ytick style=transparent,            
            axis y line*=middle,
            every inner y axis line/.append style = {dotted, ultra thick},
            height=5cm,
            width=11.9cm,
            enlarge y limits={abs=0.925},
            enlarge x limits={abs=0.925},            
            xlabel={RS = $\log (LC_{50\textit{Lf}}/LC_{50\textit{i}})$}, 
            ytick=\empty            
]
\addplot [fill=black!20!black!40] table [x=A, y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=black!60!black!20] table [x=B, y expr=\coordindex] {\testdata};
\addplot [fill=black!5] table [x=C, y expr=\coordindex] {\testdata};
\addplot [only marks,mark size=0pt,point meta=explicit symbolic,
    nodes near coords,nodes near coords style={anchor=east}] 
    table[x expr={-1*ifthenelse(\thisrow{A}+\thisrow{B}+\thisrow{C}>0,
        \thisrow{A}+\thisrow{B}+\thisrow{C},0)},y expr=\coordindex,meta=Species]  {\testdata};
\end{axis}
\end{tikzpicture}

enter image description here

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.