4

I have the following box plot:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
    boxplot/draw direction=y,
    xtick={1,2},
    xticklabels={Arm,Focal}
    ]
    \addplot+[
    boxplot prepared={
      lower whisker=0.1036,
      lower quartile=0.1141,
      median=0.1301,
      upper quartile=0.2035,
      upper whisker=0.4313
    },
    ] coordinates {ArmAmpRemaining.csv};
     \addplot+[
    boxplot prepared={
      median=0.2402,
      upper quartile=0.4032,
      lower quartile=0.1616,
      upper whisker=4.1380,
      lower whisker=0.1001
    },
    ] coordinates {FocalAmpRemaining.csv};

  \end{axis}
\end{tikzpicture}

\end{document}

which gives:

enter image description here

I am trying to add the following additional points for "Arm" (ArmAmpRemaining.csv):

0,0.013180293
0,0.018654822
0,0.059293293
0,0.01712113
0,0.020415919
0,0.002116679
0,0.024196466
0,0.030270146
0,0.003312453
0,0.005664347
0,0.000423168
0,0.016275721
0,0.076458262
0,0.011121185
0,0.002557231
0,0.021957368
0,0.005987516
0,0.012759191
0,0.00235364
0,0.015715795
0,0.018448824
0,0.047329083
0,0.009960141
0,0.000900088
0,0.057903216
0,0.039836288
0,0.086832933
0,0.004415255
0,0.008460102
0,0.069336409
0,0.032064647
0,0.069953061
0,0.003605351
0,0.092048857
0,0.015318523
0,0.070592642
0,0.035856016
0,0.014697134
0,0.030244227
0,0.047442789
0,0.008148126
0,0.006431479
0,0.048919375
0,0.036452628
0,0.04012338
0,0.012237314
0,0.041330832
0,0.006483737
0,0.086826313
0,0.019028645
0,0.014778075
0,0.019564107
0,0.090206592
0,0.041855532
0,0.02772211
0,0.001268211
0,0.016971965
0,0.018543926
0,0.009281348
0,0.003170742
0,0.002008093
0,0.066902523
0,0.006594555
0,0.020020344
0,0.043824041
0,0.022989584
0,0.025013343
0,0.000741184
0,0.003801368
0,0.014534248
0,0.020886245
0,0.074713821
0,0.014659161
0,0.067094868
0,0.06916922
0,0.049128461
0,0.00224711
0,0.041028905
0,0.023492552
0,0.088936758
0,0.013947374
0,0.042410699
0,0.058028193
0,0.052355981
0,0.062576054
0,0.014741442
0,0.002988748
0,0.020011129

Similarly for Focal (FocalAmpRemaining.csv):

0,0.085550433
0,0.021287249
0,0.054641992
0,0.089767814
0,0.054300597
0,0.085360625
0,0.013180293
0,0.037799388
0,0.020739184
0,0.080429543
0,0.025299929
0,0.087961644
0,0.038837295
0,0.055432935
0,0.018654822
0,0.085538635
0,0.046548923
0,0.019007399
0,0.075910383
0,0.009710866
0,0.09294269
0,0.046695181
0,0.098408808
0,0.02654031
0,0.065928833
0,0.059293293
0,0.01712113
0,0.020415919
0,0.08186054
0,0.007369306
0,0.049673824
0,0.006224083
0,0.075212939
0,0.033084059
0,0.092996841
0,0.090102422
0,0.073958473
0,0.093766407
0,0.002116679
0,0.071691835
0,0.024196466
0,0.032544779
0,0.030270146
0,0.027683596
0,0.023143642
0,0.042322269
0,0.047166157
0,0.053851801
0,0.003312453
0,0.005664347
0,0.000423168
0,0.06610637
0,0.016275721
0,0.076458262
0,0.011121185
0,0.065921039
0,0.009776751
0,0.002557231
0,0.033379799
0,0.021957368
0,0.005987516
0,0.088669241
0,0.021519103
0,0.084979332
0,0.013585773
0,0.012759191
0,0.068397095
0,0.007899124
0,0.00235364
0,0.057863429
0,0.047545926
0,0.015715795
0,0.075272089
0,0.086902272
0,0.018448824
0,0.050769638
0,0.096159283
0,0.016819629
0,0.067519794
0,0.047725709
0,0.067942387
0,0.057542672
0,0.052908134
0,0.047329083
0,0.065360478
0,0.061350919
0,0.009960141
0,0.081348959
0,0.000900088
0,0.057903216
0,0.001928722
0,0.039836288
0,0.086832933
0,0.029412913
0,0.004415255
0,0.008460102
0,0.069336409
0,0.096641894
0,0.015396835
0,0.051885018
0,0.06118519
0,0.089504112
0,0.032064647
0,0.069953061
0,0.064568555
0,0.003605351
0,0.088598445
0,0.056245564
0,0.092048857
0,0.015318523
0,0.069664021
0,0.035951796
0,0.070592642
0,0.035856016
0,0.014697134
0,0.085998177
0,0.041507544
0,0.01402091
0,0.072040322
0,0.030244227
0,0.071699179
0,0.069420599
0,0.002815073
0,0.055009952
0,0.047442789
0,0.078520293
0,0.028536493
0,0.052940587
0,0.007882633
0,0.07580906
0,0.041393841
0,0.008148126
0,0.006431479
0,0.048919375
0,0.036452628
0,0.04012338
0,0.012237314
0,0.07907484
0,0.008461222
0,0.049268829
0,0.060536225
0,0.045610604
0,0.082517399
0,0.049608911
0,0.036472716
0,0.099066543
0,0.045945102
0,0.080169325
0,0.020654383
0,0.049130683
0,0.040526318
0,0.095743794
0,0.026422095
0,0.041330832
0,0.063104271
0,0.006483737
0,0.062140056
0,0.030844789
0,0.023401539
0,0.043798984
0,0.061601659
0,0.049490966
0,0.012323039
0,0.086826313
0,0.019028645
0,0.06160966
0,0.014778075
0,0.071077197
0,0.021983916
0,0.072999043
0,0.019564107
0,0.090206592
0,0.041855532
0,0.02772211
0,0.065877017
0,0.057188811
0,0.001268211
0,0.016971965
0,0.078502029
0,0.091118747
0,0.021846784
0,0.018543926
0,0.086796692
0,0.068826988
0,0.011705105
0,0.036299224
0,0.093116166
0,0.005116428
0,0.027833205
0,0.096625889
0,0.023425179
0,0.078619949
0,0.09336779
0,0.039122998
0,0.009281348
0,0.092557996
0,0.023579705
0,0.075467203
0,0.003170742
0,0.070494972
0,0.004068335
0,0.028013677
0,0.004590777
0,0.024894254
0,0.093571532
0,0.002008093
0,0.066902523
0,0.077024886
0,0.006594555
0,0.085060835
0,0.045240668
0,0.095727148
0,0.075107286
0,0.080514386
0,0.0575758
0,0.020020344
0,0.07362298
0,0.043824041
0,0.093785088
0,0.022989584
0,0.023220356
0,0.025013343
0,0.000741184
0,0.077797578
0,0.003801368
0,0.049083637
0,0.041199701
0,0.014534248
0,0.053193776
0,0.074713821
0,0.020886245
0,0.014659161
0,0.067094868
0,0.071375136
0,0.018604772
0,0.076710985
0,0.030768376
0,0.06916922
0,0.09365444
0,0.099069967
0,0.049128461
0,0.075625922
0,0.054247524
0,0.054459123
0,0.024679431
0,0.082286568
0,0.076042953
0,0.012189659
0,0.075097804
0,0.078496496
0,0.00224711
0,0.083666214
0,0.011158408
0,0.056916104
0,0.028883125
0,0.088824902
0,0.044909726
0,0.041028905
0,0.088936758
0,0.023492552
0,0.067377686
0,0.042743562
0,0.042442277
0,0.041327253
0,0.079577345
0,0.022377781
0,0.009780663
0,0.014759748
0,0.048864589
0,0.041382159
0,0.000318542
0,0.058058946
0,0.06808456
0,0.043493531
0,0.077937487
0,0.014574014
0,0.034914886
0,0.094631554
0,0.017146828
0,0.078749977
0,0.021339547
0,0.047544752
0,0.030539233
0,0.010218377
0,0.077065509
0,0.014030184
0,0.013947374
0,0.077556142
0,0.042410699
0,0.043024351
0,0.063273571
0,0.094216188
0,0.058028193
0,0.052355981
0,0.032126007
0,0.056048374
0,0.097246552
0,0.03730658
0,0.034912452
0,0.099210585
0,0.062576054
0,0.00853937
0,0.014741442
0,0.036697018
0,0.002988748
0,0.067353025
0,0.047356069
0,0.028381496
0,0.083497329
0,0.035051893
0,0.020011129

I am trying to call each .csv file within the coordinates{}, but it's not working. I am also trying to add two horizontal dashed lines at 1.5 and 0.1 with a dark green. I would greatly appreciate the community's feedback! Thank you!

EDIT:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}

\begin{tikzpicture}
\begin{axis}[y=1cm]
\addplot+[boxplot]
table[col sep=comma] {FocalAmp.csv};
\end{axis}
\end{tikzpicture}
\end{document}
11
  • 1
    May be you have to use ] table[x index=0,y index=1,col sep=comma] {FocalAmpRemaining.csv};. Is that what you want?
    – user11232
    Commented May 13, 2015 at 0:09
  • @HarishKumar Hi! Yes! :)
    – Johnathan
    Commented May 13, 2015 at 0:17
  • @HarishKumar Would you know a quick formula that creates a box plot and identifies outliers? Right now, I am using R and copying and pasting manual results, which is not that efficient... Thank you!
    – Johnathan
    Commented May 13, 2015 at 0:27
  • You need coordinates any how. How do you get them? Using R? What are you using R for? Box plot can be tweaked using pgfplots.
    – user11232
    Commented May 13, 2015 at 0:33
  • @HarishKumar Hi! I am using R because I am familiar with it, but not latex. I am still learning, and getting a better handle of pgfplots (e.g. plot histograms, curves). However, I have no clue on how to tweak it for box plots. Box plots are something that I use regularly for my research project.
    – Johnathan
    Commented May 13, 2015 at 0:37

1 Answer 1

1

coordinates {...} expects a manual list of coordinates, not a file name. The .csv files can be loaded via table[col sep=comma] {file.csv}.

The dark green lines can be drawn with \draw. The coordinate, especially the y value is available by the coordinate system axis cs:, the left and right borders are available via the node current axis.

Full example:

\documentclass{standalone}   
\usepackage[svgnames]{xcolor}
\usepackage{pgfplots}   
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
    boxplot/draw direction=y,
    xtick={1,2},
    xticklabels={Arm,Focal}
    ]

    % Horizontal dark green lines
    \draw[DarkGreen, dashed]
      (axis cs:0, 1.5) coordinate (tmp)
      (current axis.west |- tmp) -- (current axis.east |- tmp)
      (axis cs:0, 0.1) coordinate (tmp)
      (current axis.west |- tmp) -- (current axis.east |- tmp)
    ;

    \addplot+[
    boxplot prepared={
      lower whisker=0.1036, 
      lower quartile=0.1141,
      median=0.1301,
      upper quartile=0.2035,
      upper whisker=0.4313
    },
    ] table[col sep=comma] {ArmAmpRemaining.csv};
     \addplot+[
    boxplot prepared={
      median=0.2402,
      upper quartile=0.4032,
      lower quartile=0.1616,
      upper whisker=4.1380,
      lower whisker=0.1001
    },
    ] table[col sep=comma] {FocalAmpRemaining.csv};

  \end{axis}
\end{tikzpicture}
\end{document}

Result

Package pgfplots can also automatically calculate the values for the box plots via boxplot instead of boxplot prepared. Also the average calculation can be enabled. Outliers are automatically identified and shown. The following example uses the same data (the remaining points as full data set instead) as the question except that it adds two points (0, 0.105) and (0, 0.11) to ArmAmpRemaining.csv. The example also prints the calculated values to the screen and log file. Special coordinate systems as boxplot box cs: or boxplot whisker cs: can be used to annotate the box plot, see the manual of pgfplots for details.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\usepgfplotslibrary{statistics}

\newcommand*{\printboxplotdata}{%
  \pgfextra{%
    \typeout{Box plot values:}%
    \printboxplotvalue{lower whisker}%
    \printboxplotvalue{lower quartile}%
    \printboxplotvalue{median}%
    \printboxplotvalue{upper quartile}%
    \printboxplotvalue{upper whisker}%
    \printboxplotvalue{average}%
    \typeout{}%
  }%
}
\newcommand*{\printboxplotvalue}[1]{%
  \typeout{* #1 = \boxplotvalue{#1}}%
}

\begin{document}

\begin{tikzpicture}
  \begin{axis}
    [
    boxplot/draw direction=y,
    xtick={1,2},
    xticklabels={Arm,Focal}
    ]

    \addplot+[
      boxplot={average=auto},
    ] table[col sep=comma] {ArmAmpRemaining.csv}
    \printboxplotdata
    ;

    \addplot+[
      boxplot={average=auto},
    ] table[col sep=comma] {FocalAmpRemaining.csv}
    \printboxplotdata
    ;

  \end{axis}
\end{tikzpicture}
\end{document}

Result boxplot

The calculates values are:

Box plot values:
* lower whisker = 4.23168e-4
* lower quartile = 9.4510461e-3
* median = 2.0015736e-2
* upper quartile = 4.7414362e-2
* upper whisker = 9.2048857e-2
* average = 3.1158e-2

Box plot values:
* lower whisker = 3.18542e-4
* lower quartile = 2.0696782e-2
* median = 4.6548923e-2
* upper quartile = 7.2519682e-2
* upper whisker = 9.9210585e-2
* average = 4.6918e-2
2
  • Would you know a quick formula that creates a box plot and identifies outliers? Right now, I am using R and copying and pasting manual results, which is not that efficient... Thank you!
    – Johnathan
    Commented May 13, 2015 at 0:26
  • @Johnathan Package pgfplots can already do pretty much including identifying outliers, see the extended answer. Commented May 13, 2015 at 7:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .