2

I want to transform a set of barcharts (such as in the subset in the MWE below) to a stacked bar chart which is more legible, as in this example:

Stacked bar chart

\documentclass{article}
\usepackage{tikz}

\begin{document}

            Calendars and reminders\\                   
            \begin{centering}
                \begin{bchart}[max=70,scale=1,width=14cm,unit=\%]
                        \bcbar[text={Daily},color=cyan!20!green!40]{51}
                        \bcbar[text={Weekly},color=cyan!60!green!20]{24}    
                        \bcbar[text={Less often}, color=cyan!10]{25}
                     \end{bchart}
                 \end{centering}                        

            Send/receive texts\\                    
            \begin{centering}
                \begin{bchart}[max=70,scale=1,width=14cm,unit=\%]
                        \bcbar[text={Daily},color=cyan!20!green!40]{67}
                        \bcbar[text={Weekly},color=cyan!60!green!20]{23}    
                        \bcbar[text={Less oft}, color=cyan!10]{7}
                     \end{bchart}
                 \end{centering}

            Make or receive phone calls\\                   
            \begin{centering}
                \begin{bchart}[max=70,scale=1,width=14cm,unit=\%]
                        \bcbar[text={Daily},color=cyan!20!green!40]{46}
                        \bcbar[text={Weekly},color=cyan!60!green!20]{38}    
                        \bcbar[text={Less often}, color=cyan!10]{16}
                     \end{bchart}
                 \end{centering}
\end{document}

I tried to use the solution to a previously asked question here (How to draw bar chart using tikz?) without success.

Can you help? Thanks!

My first attempts (not a MWE, but for you to have an idea of what I'm doing wrong):

\begin{tikzpicture}
\pgfplotstableread{ % Read the data into a table macro

Label   Daily Weekly Less~often
Calendars~and~reminders      0.51      0.24      0.25
    Send/receive~texts           0.67      0.23      0.07
    Make/receive~phone~calls     0.46      0.38      0.16
}\datatable

\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=0,         % Start x axis at 0
            ytick=data,     % Use as many tick labels as y coordinates
            yticklabels from table={\datatable}{Label}  % Get the labels from the Label column of the \datatable
    ]
    \addplot [fill=cyan!20!green!40] table [x=First, y expr=\coordindex] {\datatable};   % "First" column against the data index
    \addplot [fill=cyan!60!green!20]table [x=Second, y expr=\coordindex] {\datatable};
    \addplot [fill=cyan!10] table [x=Third, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}
Adriana
  • 23

1 Answers1

1

Based on OP's MWE, this is an attempt to make it work, so that the OP can have a starting point. The main errors come from ~ (now replaced by \space) in Label column and the use of x=First, Second and Third (now they are changed to Daily, Weekly, and LessOften) on read-in.

enter image description here

Code

\documentclass[border=10pt]{standalone}%[a4paper]{article}

\usepackage{tikz,pgfplots,pgfplotstable}
\pgfplotsset{compat=1.8}
\begin{document}

\pgfplotstableread{ % Read the data into a table macro
Label                                Daily Weekly LessOften
Calendars\space And\space Reminders   0.51  0.24  0.25
Send/receive\space Texts              0.67  0.23  0.07
Make/receive\space Phone\space Calls  0.46  0.38  0.16
}\testdata

\begin{tikzpicture}
\begin{axis}[
            xbar stacked,   % Stacked horizontal bars
            xmin=0,         % Start x axis at 0
            ytick=data,     % Use as many tick labels as y coordinates
            yticklabels from table={\testdata}{Label}  % Get the labels from the Label column of the \datatable
]
\addplot [fill=cyan!20!green!40] table [x=Daily, meta=Label,y expr=\coordindex] {\testdata};   % "First" column against the data index
\addplot [fill=cyan!60!green!20] table [x=Weekly, meta=Label,y expr=\coordindex] {\testdata};
\addplot [fill=cyan!10] table [x=LessOften, meta=Label,y expr=\coordindex] {\testdata};
\end{axis}
\end{tikzpicture}

\end{document}
Jesse
  • 29,686