2

I'm trying to reproduce a figure from a paper so it would be in line with the visual style of my paper. The figure I'm trying to reproduce is

enter image description here

Since originally posting I've found a way to define the colors of the ybars and to draw the vertical lines. I was able to do that thanks to this great example example. However, I'm having some difficulties with the horizontal lines. I do not know how I should refer to the x-axis coordinates. I have used the Label names for the x-axis but this doesn't allow me to draw the horizontal line so that it starts from the y-axis and goes all the way to the end. Currently my code is the following:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}

\definecolor{RYB1}{RGB}{128, 177, 211}
\definecolor{RYB2}{RGB}{251, 128, 114}
\definecolor{RYB3}{RGB}{253, 180, 98}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
symbolic x coords={Label 1,Label 2, Label 3, Label 4, Label 5, Label 6, Label 7},
xtick={Label 1, Label 2, Label 3, Label 4, Label 5, Label 6, Label 7},
x tick label style={rotate=45, anchor=east, align=center},
axis lines*=left,
ymajorgrids = true,
y label style={rotate=-90},
ylabel=\Large{\textbf{\%}},
legend style={at={(0.5,-0.10)},
    anchor=north,legend columns=1},
    ymin=0,
    ymax=175,
    bar width=5mm,
    ybar=-0.5cm, 
   enlarge x limits={abs=0.6cm},
    nodes near coords,        
    every node near coord/.append style={color=black},
]
\addplot [RYB1,fill=RYB1]
coordinates{ (Label 1,164) } ;
\addplot [RYB1,fill=RYB1]
coordinates{ (Label 2,156) } ;
\addplot [RYB1,fill=RYB1]
coordinates{ (Label 3,124) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (Label 4,100) } ;
\addplot [RYB3,fill=RYB3]
coordinates{ (Label 5,5) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (Label 6,111) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (Label 7,95) } ;
\draw [RYB3] (axis cs:Label 1,164) -- (axis cs:Label 7,164) ;
\draw [RYB3,->] (axis cs:Label 3,164) node [above] {-24 \%} -- (axis cs:Label 3,139) ;
\draw [RYB3,->] (axis cs:Label 4,164) node [above] {-39 \%} -- (axis cs:Label 4,115) ;
\draw [RYB3,->] (axis cs:Label 5,164) node [above] {-97 \%} -- (axis cs:Label 5,20) ;
\end{axis}
\end{tikzpicture}

\end{document}

This results in to a Figure that looks like this.

enter image description here

There are two issues with the picture. 1) I would like the horizontal line to start from the left end of the first bar and end at same point as the x-axis does. 2) The horizontal line goes over the second bar node (156).

Both of these issues could be resolved if I knew how to refer to the x-coordinates.

1
2

You could change the xticklabels and plot using normal coordinates. I added a postaction to fill the background of 156 and the other labels with white color. I'm not very satisfied for my solution for the orange line, maybe you can improve it further.

Result

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\usetikzlibrary{calc} 

\definecolor{RYB1}{RGB}{128, 177, 211}
\definecolor{RYB2}{RGB}{251, 128, 114}
\definecolor{RYB3}{RGB}{253, 180, 98}

\begin{document}

\begin{tikzpicture}
\pgfplotsset{width=10 cm}
\begin{axis} [
xtick={1,...,7},
xticklabels={{Label 1},{Label 2},{Label 3},{Label 4},{Label 5},{Label 6},{Label 7}},
x tick label style={rotate=45, anchor=east, align=center},
axis lines*=left,
ymajorgrids = true,
y label style={rotate=-90},
ylabel=\Large{\textbf{\%}},
legend style={at={(0.5,-0.10)},
    anchor=north,legend columns=1},
    ymin=0,
    ytick={0,20,...,200},
    ymax=200,
    bar width=5mm,
    ybar=-0.5cm, 
   %enlarge x limits={abs=0.6cm},
    nodes near coords,        
    every node near coord/.append style={postaction={fill=white,draw=none},color=black},%NOTE THE POSTACTION
    xmin=.5,xmax=7.5
]
\addplot [RYB1,fill=RYB1]
coordinates{ (1,164) } ;
\addplot [RYB1,fill=RYB1]
coordinates{ (2,156) } ;
\addplot [RYB1,fill=RYB1]
coordinates{ (3,124) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (4,100) } ;
\addplot [RYB3,fill=RYB3]
coordinates{ (5,5) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (6,111) } ;
\addplot [RYB2,fill=RYB2]
coordinates{ (7,95) } ;
\draw [RYB3] (axis cs:0,164)-- (axis cs:7,164) node [pos=3/7,above] (3) {-24 \%} node [pos=4/7,above] (4) {-39 \%} node [pos=5/7,above](5) {-97 \%};
\draw [RYB3,->] (3)  -- (axis cs:3,139);
\draw [RYB3,->] (4)  -- (axis cs:4,115);
\draw [RYB3,->] (5)  -- (axis cs:5,20);
%Quick and dirty fix for the orange line (I'm not very proud of this, requires calc)
\draw[white] (axis cs:0.5,164) -- ($(axis cs:1,164)-(2.5mm,0)$);
\draw[RYB3] (axis cs:7,164) -- (axis cs:7.5,164);
\end{axis}
\end{tikzpicture}

\end{document}

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.