PGFPLOTS:在图表轴标签外插入箱线图,与 GNU R 非常相似

PGFPLOTS:在图表轴标签外插入箱线图,与 GNU R 非常相似

我有一些绘制水平条形图的代码。我想做的是将箱线图或置信区间放在图表顶部的水平轴上方。GNU R(参见 R In Action,Robert Kabacoff)允许在水平/垂直轴上/外绘制箱线图,这样可以一目了然地呈现美观且信息量大的信息。

类似地,我试图在 x 轴标签上方添加箱线图或水平误差线。由于箱线图绘制四分位数,因此对于我所展示的内容而言,具有均值最小值/最大值的误差线/置信区间可能更有意义。我如何获得类似的东西?是否存在一些剪辑,因为我的轴向上移似乎忽略了较大的移位值。另外,我无法让垂直红线延伸到 x 轴上方。我的想法是在 x 轴上方绘制一条 +/- n SDEV 的粗红线和最小值/最大值的细误差线,并在图表底部绘制箱线图。如果可能的话,我想用它标记 \mu 和 +/- n\sigma 点。

我注释掉了下面的误差线 addplot。另一个想法是如何让 PGFPLOTS 计算平均值/标准差。如果 boxplot 可以计算四分位数,是否有某种机制可以从内联表中提取数据列 Miles?

我开始这个项目时牢记着 Tufte 原则,但我不断添加更多墨水项目。我希望将一些其他数据合并到这种类型的图表中。我非常感谢任何帮助解决这个问题的人。问候,戴夫

\documentclass{article}
\usepackage[usenames,dvipsnames]{color}
\usepackage[left=0.5in,right=0.5in,top=0.8in,bottom=0.75in]{geometry}
\usepackage{amsmath,relsize}
\usepackage{filecontents} 
\usepackage{pgfplots} 
\usepackage{pgfplotstable}
\pgfplotsset{width=.95\textwidth, compat=newest} 

\pgfplotsset{
    discard if/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
                \def\pgfmathresult{inf}
            \fi
        }
    },
    discard if not/.style 2 args={
        x filter/.code={
            \edef\tempa{\thisrow{#1}}
            \edef\tempb{#2}
            \ifx\tempa\tempb
            \else
                \def\pgfmathresult{inf}
            \fi
        }
    }
}

\pgfkeys{/pgfplots/x axis shift down/.style={
    x axis line style={yshift=-#1},
    xtick style={yshift=-#1},
    xticklabel shift={#1}}}
\pgfkeys{/pgfplots/y axis shift left/.style={
    y axis line style={xshift=-#1},
    ytick style={xshift=-#1},
    yticklabel shift={#1}}}

\begin{document}
\sffamily
\begin{filecontents}{data.csv}
Miles,   Bike,    rdate 
12.5,    Yel-11,  {$\stackrel{\hbox{Yel-11}}   {\hbox{\tiny 03/30/14}}$}
16.5,    Fuji,    {$\stackrel{\hbox{Fuji}}     {\hbox{\tiny 04/23/14}}$}
21.0,    8000SHX, {$\stackrel{\hbox{8000SHX}}  {\hbox{\tiny 05/18/14}}$}
24.2,    Yel-11,  {$\stackrel{\hbox{Yel-11}}   {\hbox{\tiny 05/24/14}}$}
\end{filecontents}
%                                    

\pgfplotstablegetrowsof{data.csv}
\edef\numberofrows{\pgfplotsretval}

\pgfplotsset{every axis/.append style={semithick,tick style={major tick
length=4pt,thick,black}}}
\begin{center}
\fbox{ \begin{tikzpicture}
\begin{axis}[
%/pgf/number format/.cd,1000 sep={},
width=0.90\linewidth,height=4cm,
xbar,/pgf/bar shift=0pt,
% title style={yshift=1cm},
% title={\centering More Customization: ``colorbar top''},
title={\centering label 2014},
title style={yshift=1cm, text width=8cm,font=\rmfamily\itshape\Large},
xmin=0, xmax=50, xticklabel pos=upper, tick align=outside,
xticklabel style={major tick length=20pt, color=black,tickwidth={10pt}},
axis x line*=top, %tickwidth={2pt},
axis y line*=left, y axis line style={opacity=0},
xmajorgrids, major grid style=white, axis on top,
xtick={0,10,15,20,25,30,35,40,50}, %,100,150,200,250},
separate axis lines, x axis shift up=35pt, y axis shift left=10pt,
%xtick=\empty, %xlabel= {},
%enlarge x limits={value=0.1, upper},
enlarge y limits=0.25,
ytick={0,...,\numberofrows},
%ytick={0,...,\pgfmathresult}, %\pgfplotsretval},
y dir=reverse,
y tick label style={align=right, major tick length=5pt},
yticklabels from table={data.csv}{[index]2},
nodes near coords, nodes near coords align=horizontal,
%point meta=explicit symbolic
]

\addplot [draw,fill=blue, discard if not={Bike}{Fuji}] 
        table [y expr=\coordindex, x index=0, col sep=comma]{data.csv};

% \addplot [draw,fill=blue!60!white, discard if not={Bike}{Yfoil}] 
%         table [y expr=\coordindex, x index=0, col sep=comma]{data.csv};

\addplot [draw,fill=yellow,  discard if not={Bike}{Yel-11}] 
        table [ y expr=\coordindex, x index=0,col sep=comma]{data.csv};

% \addplot [draw,fill=red,     discard if not={Bike}{Y-22}] 
%         table [ y expr=\coordindex, x index=0,col sep=comma]{data.csv};

\addplot [draw,fill=gray,    discard if not={Bike}{8000SHX}] 
        table [ y expr=\coordindex, x index=0,col sep=comma]{data.csv};

\draw [red,dotted,thick] (axis cs:18.6,-1) -- (axis cs:18.6,29)
       node[anchor=west,on top] at (axis cs:19.0,0.5)
            {label}; 
\draw  node[anchor=center,on top] at (axis cs:40.0,3) {label};

%\addplot[error bars/.cd, x dir=both, x fixed=5.12,] coordinates{ (18.6,-1)};

\end{axis}
\end{tikzpicture}  }%fbox
\end{center}
\end{document}

相关内容