0
\documentclass[a4paper]{book} 
\usepackage{filecontents}
\usepackage{subcaption}
\usepackage{graphicx}

\begin{filecontents*}{happyRel.csv}
    happy,sampleA,sampleB
    1,36,18
    2,24,43
    3,26,20
    4,2,9
    5,0,10
    6,13,0
    7,2,2
\end{filecontents*}

\begin{filecontents*}{sadRel.csv}
    sad,sampleA,sampleB
    < 15,3,7
    16 - 50,15,16
    51 - 100,8,50
    101 - 500,12,24
    > 500,11,3
    ∅,50,0  
\end{filecontents*}

\begin{filecontents*}{happines.tex}
  \documentclass{standalone}
  \usepackage{pgfplots}
  \pgfplotsset{compat=newest}
  \usepackage{pgfplotstable}

  \begin{document}
  \pgfplotstableread[col sep=comma]{happyRel.csv}\datatable
    \begin{tikzpicture}
      \begin{axis}[
          title=Happiness,
          ybar,
          ymin=0,
          ymax=55,
          xticklabels={ji, 1, 2, 3, 4, 5, 6, 7},
          xticklabel style={anchor=center, anchor=north},
          yticklabels = {ji, \(0\%\), \(10\%\), \(20\%\), \(30\%\), \(40\%\), \(50\%\)},
          point meta=explicit symbolic,
          %xtick pos=left, % align x-axis ticks to left side
          legend style={at={(1,0.95)},
              anchor=north,
              legend columns=1,
              fill=white,
              /tikz/every odd column/.append style={column sep=0.5cm},
              /tikz/every even column/.append style={column sep=0.1cm},
              nodes={anchor=west}, % align text to the left
          },
      ]
      \addplot table[x expr=\coordindex, y=sampleA, meta=sampleA]{\datatable};
      \addplot table[x expr=\coordindex, y=sampleB, meta=sampleB]{\datatable};
      \legend{Sample A (\(N=26\)), Sample B (\(N=23\))}
      \end{axis}
    \end{tikzpicture}
  \end{document}  
\end{filecontents*}

\begin{filecontents*}{sadness.tex}
  \documentclass{standalone}
  \usepackage{pgfplots}
  \pgfplotsset{compat=newest}
  \usepackage{pgfplotstable}

  \begin{document}
  \pgfplotstableread[col sep=comma]{sadRel.csv}\datatable
  \begin{tikzpicture}
      \begin{axis}[
          title=Sadness (people),
          ybar,
          ymin=0,
          ymax=55,
          xticklabels={ji, <15, 16-50, 51-100, 101-500, >500, \(-\)},
          xticklabel style={rotate=45,anchor=center,anchor=north east},
          yticklabels = {ji, \(0\%\), \(10\%\), \(20\%\), \(30\%\), \(40\%\), \(50\%\)},
          ytick distance=10, % set distance between x-axis ticks to 1
          point meta=explicit symbolic,
          xtick pos=left, % align x-axis ticks to left side
      ]
      \addplot table[x expr=\coordindex, y=sampleA, meta=sampleA]{\datatable};
      \addplot table[x expr=\coordindex, y=sampleB, meta=sampleB]{\datatable};
      \end{axis} 
    \end{tikzpicture}
  \end{document}
\end{filecontents*}

\begin{document}
  \chapter{First chapter}
  \chapter{Second chapter}
  \begin{figure}
    \centering
    \begin{subfigure}[t]{0.49\linewidth}
      \centering
      \includegraphics[width=1.2\linewidth]{happines.pdf}
      \caption{Likert scale responses of people's happiness}
      \label{fig:happy}
    \end{subfigure}
    \begin{subfigure}[t]{0.49\linewidth}
      \centering
      \includegraphics[width=\linewidth]{sadness.pdf}
      \caption{Demographic responses of people's sadness}
      \label{fig:sad}
    \end{subfigure}
    \caption{A general caption to explain the common things of the graph}
    \label{fig:happysad}
  \end{figure}
  I think if I make the picture in its own \texttt{happysad.tex} file, I won't be able to get the most accurate referencing system (e.\@~g.\@ figure \ref{fig:happy}).
\end{document}

Apologies for the horribleness with all the filecontentsing, it's just in to approximate the working structure of my work (when it's not being MWEd).

How would I go about making the legend not see-through for the b-graph?

enter image description here

BlueIris
  • 339
  • Why do you have: includegraphics[width=1.2\linewidth]{happines.pdf} ? I.e. excessive width? – MS-SPO May 10 '23 at 10:30
  • 1
    If not, the left one is weirdly small. – BlueIris May 10 '23 at 10:52
  • Why don't you build the two figures in the same tikzpicture with two axis environments? You can add the legend to the second one and locate it wherever needed. Maybe you will have to give up the subfigure things, but... – Rmano May 11 '23 at 12:21
  • Can be useful: https://tex.stackexchange.com/questions/237213/how-to-add-one-single-legend-entry-for-several-plots and https://tex.stackexchange.com/questions/198997/pgfplots-two-y-axis-with-three-plots-and-one-legend – Rmano May 11 '23 at 12:29

0 Answers0