1

Hi I'm trying to create two charts side by side on a page. And I would like to have the width of the second chart to be smaller than the first one but NOT the height. I want the width to be smaller because of the fewer x axis ticks in the second chart.

For some reason the graph decides to move the bars all the way to the left and right instead of spreading the bars linearly through the chart, which I also would like a a fix for.

Here's my main.tex file:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdflscape}
\usepackage[table]{xcolor}
\usepackage[landscape, a4paper, bottom=0.0in, margin=0in]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\usepackage{array}
\usepackage{caption}
\usepackage{subcaption}


%Customization
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\pgfplotsset{compat=newest} % Allows to place the legend below plot
\usepgfplotslibrary{units} % Allows to enter the units nicely


\pagenumbering{gobble}
\definecolor{customblue}{RGB}{21,155,151}
\definecolor{custompink}{RGB}{230,55,148}


\title{test}
\author{sinasohi}
\date{January 2017}

\begin{document}
%\maketitle

\include{Page1}
\include{Page3}

\end{document}

Here's my code for Page3.tex, which I include in main.tex:

\pgfplotstableread[row sep=\\,col sep=&]{
    interval    & a     & b     \\
    K74         & 32.9  & 27.5  \\
    K76         & 23.5  & 22.5  \\
    K75         & 25.7  & 25.0  \\
    K77         & 22.3  & 30    \\
    K89-90      & 30.6  & 30    \\
    K92         & 19.3  & 15    \\
    }\mydata

\pgfplotstableread[row sep=\\,col sep=&]{
    interval        & a     & b     \\
    K78             & 18.8  & 20  \\
    K85, K86, K87   & 66.3  & 60  \\
    }\yourdata

\begin{figure}
\centering
\begin{minipage}{.4\textwidth}
\caption*{\textbf{Kardiovaskulære diagnoser}}
\begin{tikzpicture}
    \begin{axis}[
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            bar width=.6cm,
            symbolic x coords={K74,K76,K75,K77,K89-90,K92},
            xtick=data,
            ymin=0, %ymax=40,
            width=1\textwidth,
            nodes near coords,
            every node near coord/.append style={color=black}
        ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\mydata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\mydata};
        \legend{Klinik 12345, Landsrapport}
    \end{axis}
\end{tikzpicture}
\end{minipage}
\begin{minipage}{.4\textwidth}
\caption*{\textbf{Yderligere diagnoser}}
\begin{tikzpicture}
    \begin{axis}[
            %legend style={legend columns=1,at={(1,1)},anchor=north west},
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            bar width=.6cm,
            symbolic x coords={K78, {K85, K86, K87}},
            xtick=data,
            ymin=0, %ymax=40,
            width=1\textwidth,
            nodes near coords,
            every node near coord/.append style={color=black}
        ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\yourdata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\yourdata};
        \legend{Klinik 12345, Landsrapport}
    \end{axis}
\end{tikzpicture}
\end{minipage}
\end{figure}

This is how it looks:

enter image description here

  • It would be very helpful, if you provide a complete MWE (Minimal Working Example) from \documentclass till \end{document}. Otherwise we have to rely on the magical crystal ball. Also, please include the screenshot as image on this site instead of linking it somewhere else. – Jan Jan 20 '17 at 9:06
  • Thanks for the suggestion, I added the remaining code! :-) – Vanquiza Jan 20 '17 at 9:22
2

You can set the height in addition to the width. For the placement of the bars on the second axis, use something like enlarge x limits=0.7. Don't know why it does a bad job initially.

I would set this using a groupplot though, remove the minipages and use the title of the axis instead of \caption*.

Also, try avoiding loading packages more than once. You have xcolor twice. It is also loaded by pgfplots though, so unless you're using the colortbl features at some other point in the document, it doesn't hurt removing \usepackage[table]{xcolor} as well. I also changed from compat=newest to compat=1.14. Christian Feuersänger recommends using an explicit value, so that your figure doesn't change if you update pgfplots.

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pdflscape}
\usepackage[table]{xcolor}
\usepackage[landscape, a4paper, bottom=0.0in, margin=0in]{geometry}
\usepackage{pgfplots}

\pgfplotsset{compat=1.14} % Allows to place the legend below plot
\usepgfplotslibrary{
  units, % Allows to enter the units nicely
  groupplots
}

\definecolor{customblue}{RGB}{21,155,151}
\definecolor{custompink}{RGB}{230,55,148}

\begin{document}
\pgfplotstableread[row sep=\\,col sep=&]{
    interval    & a     & b     \\
    K74         & 32.9  & 27.5  \\
    K76         & 23.5  & 22.5  \\
    K75         & 25.7  & 25.0  \\
    K77         & 22.3  & 30    \\
    K89-90      & 30.6  & 30    \\
    K92         & 19.3  & 15    \\
    }\mydata

\pgfplotstableread[row sep=\\,col sep=&]{
    interval        & a     & b     \\
    K78             & 18.8  & 20  \\
    K85, K86, K87   & 66.3  & 60  \\
    }\yourdata

\begin{figure}
\centering
\begin{tikzpicture}
    \begin{groupplot}[
            group style={group size=2 by 1},
            legend style={at={(0.5,1)}, text width=6em, text height=1.5ex, anchor=north,legend columns=-1},
            ybar,
            /tikz/bar width=.6cm,            
            xtick=data,
            ymin=0, %ymax=40,
            ,height=10cm,
            nodes near coords,
            every node near coord/.append style={color=black}            
        ]

\nextgroupplot[
   title=\textbf{Kardiovaskulære diagnoser},
   width=.55\textwidth,
   symbolic x coords={K74,K76,K75,K77,K89-90,K92}
]
        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\mydata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\mydata};
        \legend{Klinik 12345, Landsrapport}

\nextgroupplot[
    symbolic x coords={K78, {K85, K86, K87}},
    width=.3\textwidth,
    enlarge x limits=0.7,
    title=\textbf{Yderligere diagnoser}
    ]

        \addplot[red!20!customblue,fill=customblue!80!white] table[x=interval,y=a]{\yourdata};
        \addplot[red!20!custompink,fill=custompink!80!white] table[x=interval,y=b]{\yourdata};
        \legend{Klinik 12345, Landsrapport}
\end{groupplot}
\end{tikzpicture}
\end{figure}
\end{document}
  • Thank you very much! This is exactly what I was looking to do! – Vanquiza Jan 20 '17 at 10:29

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.