4

I have made the following graph:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [title = {Lengths of Amplification and Deletion Regions},ylabel = {$log_{10}(Length)$},
    boxplot/draw direction=y,
    xtick={1,2,3},
    xticklabels={Amplification, Deletion, Both},
    x tick label style={font=\footnotesize, text width=2.5cm, align=center}
    ]
    \addplot+[mark = *, mark options = {red},
    boxplot prepared={
      lower whisker=2.214844,
      lower quartile=3.608312,
      median=3.895478,
      upper quartile=4.447298,
      upper whisker=4.666284
    }, color = red
    ] coordinates{(0,4.832228)(0,5.513942)(0,6.29165)(0,5.216712)(0,5.677036)(0,4.981995)(0,5.172095)(0,4.056417)};
    \addplot+[mark = *,mark options = {blue},
    boxplot prepared={
      lower whisker=3.508799,
      lower quartile=5.079821,
      median=5.481519,
      upper quartile=5.971588,
      upper whisker=6.250831
    }, color = blue
    ] coordinates{(0,6.508115)(0,6.486354)(0,6.860059)(0,6.620663)(0,7.312391)(0,7.357306)(0,6.421694)
    (0,6.479597)(0,6.690945)(0,6.661593)(0,7.271025)(0,6.396931)(0,7.035161)(0,7.371248)(0,7.033689)
    (0,7.002645)(0,6.590617)(0,7.171933)(0,6.416259)(0,5.552438)};
    \addplot+[mark = *,mark options = {green},
    boxplot prepared={
      lower whisker=2.437751,
      lower quartile=3.334956,
      median=4.336029,
      upper quartile=5.068459,
      upper whisker=5.265037
    }, color = green
    ] coordinates{(0,5.826492)(0,5.819791)(0,6.21436)(0,4.47166)};
    \end{axis}
\end{tikzpicture}
\end{document}

which gives:

enter image description here

The circles near the median lines of each graph represents the average (log-transformed) for that respective group. I would like to make these dots into stars and to write the respective averages in parentheses, black colour: 4.06, 5.55, and 4.47.

  • I think you want the average key, which you can use in the same way as you are using (for example) the median key. Then you can use the normal controls to customize its appearance. By default it will appear as a diamond. – Thruston May 23 '15 at 22:10
  • You also want to change that y-axis label to \log_10{\hbox{Length}} – Thruston May 23 '15 at 22:11
  • @Thruston Hi! Could you please upload a small example? Could I insert text as well? Thanks for the advice! – Johnathan May 23 '15 at 22:12
2

To show the mean value on your box plot you can use the average key with boxplot.

I've only changed the middle bw plot, but you can see how to do the others. I've also corrected the y-axis label. Read the manual, p.430-450 for how to add labels to the averages, and change the colour.

enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepgfplotslibrary{statistics}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    [title = {Lengths of Amplification and Deletion Regions},ylabel =
        {$\log_{10}(\hbox{Length})$},
    boxplot/draw direction=y,
    xtick={1,2,3},
    xticklabels={Amplification, Deletion, Both},
    x tick label style={font=\footnotesize, text width=2.5cm, align=center}
    ]
    \addplot+[mark = *, mark options = {red},
    boxplot prepared={
      lower whisker=2.214844,
      lower quartile=3.608312,
      median=3.895478,
      upper quartile=4.447298,
      upper whisker=4.666284
    }, color = red
    ] coordinates{(0,4.832228)(0,5.513942)(0,6.29165)(0,5.216712)(0,5.677036)(0,4.981995)(0,5.172095)(0,4.056417)};
    \addplot+[mark = *,mark options = {blue},
    boxplot prepared={
      lower whisker=3.508799,
      lower quartile=5.079821,
      median=5.481519,
      average=5.442438,
      upper quartile=5.971588,
      upper whisker=6.250831
    }, color = blue
    ] coordinates{(0,6.508115)(0,6.486354)(0,6.860059)(0,6.620663)(0,7.312391)(0,7.357306)(0,6.421694)
    (0,6.479597)(0,6.690945)(0,6.661593)(0,7.271025)(0,6.396931)(0,7.035161)(0,7.371248)(0,7.033689)
    (0,7.002645)(0,6.590617)(0,7.171933)(0,6.416259)};
    \addplot+[mark = *,mark options = {green},
    boxplot prepared={
      lower whisker=2.437751,
      lower quartile=3.334956,
      median=4.336029,
      upper quartile=5.068459,
      upper whisker=5.265037
    }, color = green
    ] coordinates{(0,5.826492)(0,5.819791)(0,6.21436)(0,4.47166)};
    \end{axis}
\end{tikzpicture}
\end{document}
  • Is there a way to add average to a boxplot that is made from a .csv? I get an unknown key error when I use \addplot+[boxplot, average = 0.869, yticklabels={System 1}] – Hans Jul 23 '18 at 12:15
  • @Hans feel free to ask a new question. I think that the prepared={...} key is pretty essential to this solution. – Thruston Jul 23 '18 at 16:05
  • @Hans, add /boxplot/average={auto} – badroit May 24 '19 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.