我在向箱线图添加合适的图例时遇到了很大问题tikz
。图例要么根本没有显示,要么打乱了整个boxplot
布局。如果网格内部或外部有一个不与箱线图重叠的基本图例,我会很满意legend
(例如研究 A [红色],研究 B [蓝色])。
\begin{tikzpicture}[
thick, /pgfplots/boxplot/box extend=0.3,]
\begin{axis}
[
name=border,
grid=both,
ytick={1,...,4},
yticklabel style={align=right},
yticklabels={
statement a,
statement b,
statement c,
statement d},
cycle list={{red},{blue}},
xticklabels={placeholder, Strongly\\Disagree, Disagree, Neutral, Agree, Strongly\\Agree},
x tick label style ={rotate=90,anchor=east},
x tick label style ={font=\footnotesize},
xticklabel style = {align=center},
x dir=reverse,
y dir=reverse
]
\addplot+[ %PS VO1
boxplot prepared={
median=0,
average=0.27,
upper quartile=1,
lower quartile=-1,
upper whisker=2,
lower whisker=-2,
draw position=0.8
},
]
coordinates{};
\addplot+[ %IS VO1
boxplot prepared={
median=0.5,
average=-0.08,
upper quartile=1,
lower quartile=-2,
upper whisker=2,
lower whisker=-2,
draw position=1.2
},
] coordinates{};
\addplot+[ %PS VO2
boxplot prepared={
median=1,
average=0.64,
upper quartile=2,
lower quartile=0,
upper whisker=2,
lower whisker=-2,
draw position=1.8
},
]
coordinates{};
\addplot+[ %IS VO2
boxplot prepared={
median=1,
average=1.12,
upper quartile=2,
lower quartile=0.75,
upper whisker=2,
lower whisker=-2,
draw position=2.2
},
]
coordinates{};
\addplot+[ %PS VO3
boxplot prepared={
median=1,
average=0.55,
upper quartile=1,
lower quartile=0,
upper whisker=2,
lower whisker=-1,
draw position=2.8
},
]
coordinates{};
\addplot+[ %IS VO3
boxplot prepared={
median=1,
average=0.96,
upper quartile=2,
lower quartile=0.75,
upper whisker=2,
lower whisker=-2,
draw position=3.2
},
]
coordinates{};
%%
\addplot+[ %PS VO4
boxplot prepared={
lower whisker=-1,
upper whisker=2,
average=0.73,
median=1,
lower quartile=0,
upper quartile=2,
draw position=3.8
},
]
coordinates{};
\addplot+[ %IS VO4
boxplot prepared={
lower whisker=-2,
upper whisker=2,
average=0.77,
median=1,
lower quartile=-1,
upper quartile=2,
draw position=4.2
},
]
coordinates{};
\end{axis}
\end{tikzpicture}
由于每个语句有 2 个箱线图,draw position
我添加了整个 tikz 代码。期待您的建议
答案1
您可以根据自己的喜好定义自己的图例legend image code
。例如,
\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{statistics}
\pgfplotsset{compat=1.17}
\pgfplotsset{
/pgfplots/custom legend/.style={
legend image code/.code={
\draw [only marks,mark=diamond*]
plot coordinates {
(0.3cm,0cm)
};
}, },
}
\begin{document}
\begin{tikzpicture}[
thick, /pgfplots/boxplot/box extend=0.3,]
\begin{axis}
[
name=border,
grid=both,
ytick={1,...,4},
yticklabel style={align=right},
yticklabels={
statement a,
statement b,
statement c,
statement d},
cycle list={{red},{blue}},
xticklabels={placeholder, Strongly\\Disagree, Disagree, Neutral, Agree, Strongly\\Agree},
x tick label style ={rotate=90,anchor=east},
x tick label style ={font=\footnotesize},
xticklabel style = {align=center},
x dir=reverse,
y dir=reverse,
custom legend,
legend style={at={(1,1)},anchor=south east,legend columns=2,
column sep=0.5em}
]
\addplot+[ %PS VO1
boxplot prepared={
median=0,
average=0.27,
upper quartile=1,
lower quartile=-1,
upper whisker=2,
lower whisker=-2,
draw position=0.8
},
]
coordinates{};
\addlegendentry{pft}
\addplot+[ %IS VO1
boxplot prepared={
median=0.5,
average=-0.08,
upper quartile=1,
lower quartile=-2,
upper whisker=2,
lower whisker=-2,
draw position=1.2
},
] coordinates{};
\addlegendentry{blub}
\addplot+[ %PS VO2
boxplot prepared={
median=1,
average=0.64,
upper quartile=2,
lower quartile=0,
upper whisker=2,
lower whisker=-2,
draw position=1.8
},
]
coordinates{};
\addplot+[ %IS VO2
boxplot prepared={
median=1,
average=1.12,
upper quartile=2,
lower quartile=0.75,
upper whisker=2,
lower whisker=-2,
draw position=2.2
},
]
coordinates{};
\addplot+[ %PS VO3
boxplot prepared={
median=1,
average=0.55,
upper quartile=1,
lower quartile=0,
upper whisker=2,
lower whisker=-1,
draw position=2.8
},
]
coordinates{};
\addplot+[ %IS VO3
boxplot prepared={
median=1,
average=0.96,
upper quartile=2,
lower quartile=0.75,
upper whisker=2,
lower whisker=-2,
draw position=3.2
},
]
coordinates{};
%%
\addplot+[ %PS VO4
boxplot prepared={
lower whisker=-1,
upper whisker=2,
average=0.73,
median=1,
lower quartile=0,
upper quartile=2,
draw position=3.8
},
]
coordinates{};
\addplot+[ %IS VO4
boxplot prepared={
lower whisker=-2,
upper whisker=2,
average=0.77,
median=1,
lower quartile=-1,
upper quartile=2,
draw position=4.2
},
]
coordinates{};
\end{axis}
\end{tikzpicture}
\end{document}