图形图例由两个子图共享,我如何确保它覆盖第二个图形而不是穿过第二个图形?

图形图例由两个子图共享,我如何确保它覆盖第二个图形而不是穿过第二个图形?
\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}

对于所有这些糟糕的操作filecontents,我深表歉意,这只是为了近似我的工作结构(当没有被MWE设计时)。

我该如何让图例不透明呢b

在此处输入图片描述

相关内容