
我想創建一個類似於 長條圖範例,但用箱線圖代替長條圖。
到目前為止我所擁有的 MWE 是:
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{statistics}
\begin{document}
\pgfplotstableread{
%\begin{filecontents}{test.dat}
error
1
2
3
5
5
5
5
5
5
15
%\end{filecontents}
}\mytable
%Define the color series
\definecolor{RYB1}{RGB}{230,97,1}
\definecolor{RYB2}{RGB}{200,150,250}
\pgfplotscreateplotcyclelist{colorbrewer-RYB}{
{RYB1!50!black,fill=RYB1},
{RYB2!50!black,fill=RYB2}} %Do not add a comma after the last element of the list!
%Define the width of the boxes
\def\boxwidth{0.75}%
\begin{tikzpicture}
\begin{axis}[
scale only axis,
axis on top,
ymin=0, ymax=20,
ylabel={Error [m]},
boxplot/draw direction=y,
cycle list name=colorbrewer-RYB,
xtick={1.5, 4.5},
xticklabels={Group A, Group B},
tick align=inside,
xtick style={draw=none},
legend style={at={(0.5,-0.15)}, anchor=north,legend columns=-1},
legend image code/.code={
\draw[#1, draw=none] (0cm,-0.1cm) rectangle (0.6cm,0.1cm);
},
]
%Group A
\addplot+[
boxplot={
draw position=1,
average, % <---- Where is the average?
box extend=\boxwidth
},
]
table[ y index=0, row sep=newline ]{\mytable}; % <---- Where are the outliers?
\addlegendentry{Yes\;};
\addplot+[
boxplot={
draw position=2,
every average, % <---- This was a desperate try with a surprising result
box extend=\boxwidth
},
]
table[ y index=0, row sep=newline ]{\mytable};
\addlegendentry{No\;};
%Group B
\addplot+[
boxplot prepared={draw position=4,
lower whisker=2.5,
lower quartile=4,
median=5,
average=6,
upper quartile=8,
upper whisker=9,
box extend=\boxwidth,
}
]
coordinates {(0,12) (0,10)};
\addplot+[
boxplot prepared={draw position=5,
lower whisker=2.5,
lower quartile=4,
median=8.5,
average=10,
upper quartile=12,
upper whisker=15,
box extend=\boxwidth,
},
]
table[row sep=\\,y index=0] { 0\\ 14\\ 15\\ };
\end{axis}
\end{tikzpicture}
\end{document}
這導致
新增/功能請求
如果能夠像使用長條圖一樣建立這種類型的分組箱線圖,那就太好了。在這些中,人們可以指定一組符號座標並使用這些座標簡單地添加條形。具有相同座標的條形會形成一個群組,其中的間距會自動得到很好的處理。
在所提出的 MWE 中,所有許多維度量都是手動定義的。如果可能的話,最好能夠建立圖形,以便在定義所需的高度和寬度時自動縮放,例如使用類似的東西,
\setlength\figureheight{1.5in}% \setlength\figurewidth{1.5in}%
這樣就不需要手動更改長度。為此,我想能夠使用符號座標添加框會很有幫助。
所提出的 MWE 的問題
前兩個圖(A 組)的數據取自同一個表,而另外兩個圖(B 組)的方框已準備好。上圖有以下問題(我不知道如何解決,顯然我做錯了什麼...)
平均值的問題
- 在第一個框中,我使用了“平均”選項,旨在繪製平均值,但它不起作用。
- 在第二次中,我(偶然)嘗試使用“每個平均值”來代替,結果令人驚訝:它繪製了異常值。
如何正確繪製平均值?如果可能的話,可以選擇形狀(所有框的形狀相同)。
異常值問題
我還想繪製所有框中的異常值,如果可能的話還能夠選擇形狀(所有框都相同)。
在前兩張圖的情況下,作為從表導入的原始數據,我猜離群值的計算是自動的。也許我需要添加正確的選項...我如何添加它們?
對於準備好的繪圖,我嘗試新增座標和表格,但沒有成功。我缺什麼?