0

I would like to create a bar chart like the following image taken from a paper, enter image description here

I also saw this question How can I mix an "ybar" and an "ybar stacked" with pgfplots? but it didn't work for me due to my emergency and failing several times to produce a figure with setting like to the above picture.

Thanks in advance for anyone's help and assistance on this essential request.

Also, it is my original codes,

\documentclass{article}
%\usepackage{pgfplots}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}
\usepackage{caption}
\usepackage{polyglossia}
\pagenumbering{gobble}
\makeatletter
\newcommand\resetstackedplots{
\makeatletter
\pgfplots@stacked@isfirstplottrue
\makeatother
\addplot [forget plot,draw=none] coordinates{(-9,0) (-5,0) (-1,0) (3,0) (7,0) (12,0)};
}
\makeatother
\begin{document}
\begin{figure}[h]
\makeatletter
%\pgfplotsset{
%    calculate offset/.code={
%        \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
%        \pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
%        \pgfkeys{/pgf/fpu=false}
%    },
%    every node near coord/.style={
%        /pgfplots/calculate offset,
%        yshift=-\testmacro
%    }
%}
\centering
\begin{tikzpicture}
\begin{axis}[
    width = 12cm,
    height= 10cm,
    ybar stacked,
    xtick=data,
    bar width=0.15cm,
    x = 0.55cm,
    ymin=0,
    enlarge x limits={true, abs value=0.4},
    xticklabels={Football, PolBlogs, PolBooks, Facebook,CalTech,Rice},
%    x t style={below=25mm},
    xmin = -11,
    xmax = 13,
    xmajorgrids = true,
    ymajorgrids = true,
    legend entries={NMI,f1-score},
%    legend style={
%            at={(-0.05,.65)},
%            anchor=south east,
%            column sep=1ex
%        },
         xticklabel style={yshift=-5ex},
         % visualization depends on=y \as \rawy,
           % symbolic x coords={0,1,2,3}, enlarge x limits=0.15,
%            every node near coord/.append style={
%                    font=\tiny,
%                    shift={(axis direction cs:0,-\rawy)}
%                },
]
\addplot +[bar shift=-0.9 cm,color = blue!100] coordinates {(-9,.62) (-5,.12) (-1,.21) (3,.17) (7,.1) (12,0.01)};
\addplot +[bar shift=-0.9 cm,color = red!100] coordinates {(-9,.76) (-5,.77) (-1,.74) (3,.35) (7,.37) (12, .15)};
%\addplot +[bar shift=-.2cm] coordinates {(1,0.75) (2,.5) (3,.5)};
\resetstackedplots
\addplot  +[bar shift=-.6cm,color = blue!100]coordinates {(-9,0.13) (-5,0.01) (-1,0.01) (3,0.04) (7,0.01) (12,.02)};
\addplot  +[bar shift=-.6cm,color = red!100] coordinates {(-9,0.4) (-5,.44) (-1,.31) (3,.11) (7,.13) (12,0)};
%\addplot  +[bar shift=.2cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};
\resetstackedplots
\addplot  +[bar shift=-.3cm,color = blue!100]coordinates {(-9,.01) (-5,0.001) (-1,0.04) (3,.05) (7,.01) (12,.01)};
\addplot  +[bar shift=-.3cm,color = red!100] coordinates {(-9,0.2) (-5,0.001) (-1,.55) (3,.14) (7,.13) (12,0)};
%\addplot  +[bar shift=-.4cm] coordinates {(1,0.2) (2,0.1) (3,0.3)};
\resetstackedplots
\addplot  +[bar shift=0cm,color = blue!100]coordinates {(-9,.45) (-5,.24) (-1,0.43) (3,.11) (7,.21) (12,.14)};
\addplot  +[bar shift=0cm,color = red!100] coordinates {(-9,0.84) (-5,.72) (-1,0.69) (3,.24) (7,.52) (12,.36)};
\resetstackedplots
\addplot  +[bar shift=.3cm,color = blue!100]coordinates {(-9,.56) (-5,.01) (-1,0.01) (3,.01) (7,.05) (12,.25)};
\addplot  +[bar shift=.3cm,color = red!100] coordinates {(-9,0.73) (-5,.3) (-1,0.35) (3,.01) (7,.29) (12,.42)};
\resetstackedplots
\addplot  +[bar shift=.6cm,color = blue!100]coordinates {(-9,.21) (-5,.21) (-1,.49) (3,.01) (7,.01) (12,0.01)};
\addplot  +[bar shift=.6cm,color = red!100]coordinates {(-9,.76) (-5,.66) (-1,.73) (3,.12) (7,.03) (12,.1)};
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
Hadi
  • 103
  • 2
    Welcome to TeX SE. It's not hard, but it'd be useful to see your code, since you said you have tried something, and also the data for your plots. – Alenanno Aug 31 '16 at 14:19
  • How did it not work for you? Did it fail to compile? What errors were you getting? Perhaps include your logfile. Did it compile but look wrong? how did it look? Maybe include a picture of what looked wrong. – Seamus Aug 31 '16 at 14:57
  • Thanks @Alenanno. My difficulty is to add sub caption on the figure like the given sample. – Hadi Aug 31 '16 at 15:14

1 Answers1

0

Well, unfortunately this is not an ideal solution because apparently pgfplots cannot mix ybar and ybar stacked properties, so that the bars that appear at the same x coordinate will inevitably overlap.

I didn't find anything — neither on this site, nor in the manual — to change this behavior. Maybe someone else knows how to do this automatically, but so far I have no idea how to do it.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots,pgfplotstable}

\pgfplotsset{compat=1.13}

\definecolor{dgreen}{RGB}{0,83,54}
\definecolor{lgreen}{RGB}{0,255,28}

\pgfplotscreateplotcyclelist{cyclecol}{
{fill=dgreen},
{fill=lgreen},
}

\pgfplotstableread[col sep=comma]{
index, first, second
 1,     0.62,     .76
 2,     0.12,     .77
 3,     0.21,     .74
 4,     0.17,     .35
 5,     0.1,      .37
 6,     0.01,     .15
% 
 8,     0.13,    0.4  
 9,     0.01,     .44  
10,     0.01,     .31  
11,     0.04,     .11  
12,     0.01,     .13  
13,     0.02,    0 
%
15,      .01,    0.2
16,     0.001,   0.001
17,     0.04,     .55
18,      .05,     .14
19,      .01,     .13
20,      .01,    0 
%      
22,      .45,    0.84
23,      .24,     .72
24,     0.43,    0.69
25,      .11,     .24
26,      .21,     .52
27,      .14,     .36
%      
29,      .56,    0.73
30,      .01,     .3
31,     0.01,    0.35
32,      .01,     .01
33,      .05,     .29
34,      .25,     .42
%      
36,      .21,     .76
37,      .21,     .66
38,      .49,     .73
39,      .01,     .12
40,      .01,     .03
41,     0.01,     .1
}\datatable

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    ybar stacked,
    %width=12cm, height=10cm,
    axis lines*=left, ymajorgrids,
    ymin=0, xmin=0,
    x=3mm, y=5cm,
    %ybar=1mm,
    %bar shift=1mm,
    xtick={3.5,10.5,17.5,24.5,31.5,38.5},
    bar width=1.5mm,
    ytick={0,1},
    enlarge x limits={true, abs value=0.4},
    xticklabels={Football,PolBlogs,PolBooks,Facebook,CalTech,Rice}, 
    legend entries={NMI,f1-score},
    xticklabel style={yshift=-1ex},
    cycle list name=cyclecol
]

\addplot table[x=index, y=first, col sep=comma] \datatable;
\addplot table[x=index, y=second, col sep=comma] \datatable;

\end{axis}
\end{tikzpicture}
\end{document}
Alenanno
  • 37,338
  • Nice work but one more thing, I'd like to add a sub caption like the following image, http://pasteboard.co/fBUiWRR6b.png Thanks in advance @Alenanno – Hadi Sep 01 '16 at 09:31