1

I created the following histogram but all labels were messed up.

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      bar width=20pt,
      ymin=0,
      %ymax=100,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={First Variable, Second Variable, Third Variable, Fourth Variable, Fifth Variable},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]

      \addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
        (First Variable,42)
        (Second Variable,36)
        (Third Variable,23)
        (Fourth Variable,22)
        (Fifth Variable,61)
      };
  \end{axis}
  \end{tikzpicture}
\end{document}

Then I shortened the labels to fit them well but it required me to add the legend. But the legend is just giving one label, not others. I need help regarding either fitting the labels or explaining the labels in the legend.

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{tikzpicture}[font=\small]
    \begin{axis}[
      ybar,
      bar width=20pt,
      ymin=0,
      %ymax=100,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={A, B, C, D, E},
      xticklabel style={anchor=base,yshift=-\baselineskip},
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]

      \addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
        (A,42)
        (B,36)
        (C,23)
        (D,22)
        (E,61)
      };
\legend{A=First Variable, B=Second Variable, C=Third Variable, D=Fourth Variable,E=Fifth Variable}
  \end{axis}
  \end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688

1 Answers1

3

You may not need to shorten the labels. If you add text width=1.3cm to the xticklabel style the labels will break across multiple lines. If you also make the diagram a bit wider by setting width, there is more room. The exact lengths used will depend on your specific case, so adjust the width and text width settings as you see fit.

enter image description here

\documentclass{standalone}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\pgfplotsset{
  compat=newest,
  xlabel near ticks,
  ylabel near ticks
}

\begin{document}
  \begin{tikzpicture}[font=\small]
    \begin{axis}[
      width=10cm, % increase width of diagram a bit
      height=6cm,
      ybar,
      bar width=20pt,
      ymin=0,
      %ymax=100,
      xtick=data,
      axis x line=bottom,
      axis y line=left,
      enlarge x limits=0.2,
      symbolic x coords={First Variable, Second Variable, Third Variable, Fourth Variable, Fifth Variable},
      xticklabel style={
         anchor=base,
         yshift=-\baselineskip,
         text width=1.3cm, % means longer labels will be broken across multiple lines
         align=center, % center align
         % font=\scriptsize % if necessary, you could reduce the font size further
      },
      nodes near coords={\pgfmathprintnumber\pgfplotspointmeta\%}
    ]

      \addplot[fill=orange,fill opacity=0.5, text opacity=1] coordinates {
        (First Variable,42)
        (Second Variable,36)
        (Third Variable,23)
        (Fourth Variable,22)
        (Fifth Variable,61)
      };
  \end{axis}
  \end{tikzpicture}
\end{document}
Torbjørn T.
  • 206,688