# How to produce nice boxplots

**URL:** <https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345>\
**Category:** General Usage\
**Tags:** question, plotting, statsplots\
**Created:** [February 26, 2018, 5:24pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345 "2018-02-26T17:24:11Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![burfel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/burfel/32/3314_2.png) [@burfel](https://discourse.julialang.org/u/burfel)\
**Post date:** [February 26, 2018, 5:24pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/1 "2018-02-26T17:24:11Z")

</div>

Does anyone know how to produce nice boxplots out of a dataframe of this form?

```julia
│ Row │ Smarcc1 │ Socs3 │ Sox1 │ Sox2 │ Stat3 │ T │ Tbx3 │ Tcf7l1 │
├─────┼─────────┼─────────┼──────┼─────────┼─────────┼─────┼─────────┼─────────┤
│ 1 │ 14.0695 │ 9.93292 │ 0.0 │ 10.5082 │ 12.7531 │ 0.0 │ 12.2547 │ 12.2895 │
│ 2 │ 13.4571 │ 12.5378 │ 0.0 │ 9.98157 │ 14.3046 │ 0.0 │ 12.5239 │ 13.5173 │
│ 3 │ 13.6976 │ 10.9552 │ 0.0 │ 10.555 │ 13.2031 │ 0.0 │ 12.1651 │ 13.641 │
│ 4 │ 13.592 │ 11.3802 │ 0.0 │ 11.3047 │ 13.6553 │ 0.0 │ 13.3942 │ 13.9413 │
│ 5 │ 14.4982 │ 11.5432 │ 0.0 │ 11.5915 │ 13.8775 │ 0.0 │ 12.6017 │ 14.9448 │
│ 6 │ 13.7879 │ 12.3087 │ 0.0 │ 11.5723 │ 13.2337 │ 0.0 │ 13.379 │ 15.1738 │
│ 7 │ 12.8521 │ 11.2392 │ 0.0 │ 3.46462 │ 13.45 │ 0.0 │ 12.9797 │ 12.068 │
│ 8 │ 13.8368 │ 0.0 │ 0.0 │ 8.79081 │ 12.2692 │ 0.0 │ 12.8426 │ 15.0319 │

```

I’d like to have a boxplot per column.  
I tried tips on  
\_[https://github.com/JuliaPlots/Plots.jl/issues/210\_](https://github.com/JuliaPlots/Plots.jl/issues/210_) and  
\_[http://johnmyleswhite.github.io/Vega.jl/boxplot.html\_](http://johnmyleswhite.github.io/Vega.jl/boxplot.html_).

But nothing seemed to be running or produce a nice plot.

I am using Julia version 0.6.2.

---

<div class="post-metadata">

**Author:** ![amellnik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amellnik/32/137_2.png) [@amellnik](https://discourse.julialang.org/u/amellnik)\
**Post date:** [February 26, 2018, 5:55pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/2 "2018-02-26T17:55:40Z")

</div>

Have you tried PlotlyJS.jl? [http://spencerlyon.com/PlotlyJS.jl/examples/box\_plots/](http://spencerlyon.com/PlotlyJS.jl/examples/box_plots/)

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 26, 2018, 6:08pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/3 "2018-02-26T18:08:20Z")

</div>

I think the recipes are in StatPlots.jl.

[https://github.com/JuliaPlots/StatPlots.jl](https://github.com/JuliaPlots/StatPlots.jl)

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [February 26, 2018, 6:10pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/4 "2018-02-26T18:10:08Z")

</div>

You can use ECharts.jl, though it only supports Jupyter Notebook and Juno at the moment.

[http://randyzwitch.com/ECharts.jl/box#multiple-series](http://randyzwitch.com/ECharts.jl/box#multiple-series)

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [February 26, 2018, 6:52pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/5 "2018-02-26T18:52:18Z")

</div>

Chris Rackauckas is correct, this is in StatPlots.jl.

```julia
@df yourdataframe boxplot(:Smarcc1)

@df yourdataframe boxplot!(:Socs3)

```

will put two boxplots side by side. Notice the ! in the second line

---

<div class="post-metadata">

**Author:** ![Mattriks](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattriks/32/351_2.png) [@Mattriks](https://discourse.julialang.org/u/Mattriks)\
**Post date:** [February 26, 2018, 8:09pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/6 "2018-02-26T20:09:45Z")

</div>

And `Gadfly` (like ggplot2). Start with the following links:  
[http://gadflyjl.org/stable/man/plotting.html#Plotting-wide-formatted-data-1](http://gadflyjl.org/stable/man/plotting.html#Plotting-wide-formatted-data-1)  
[http://gadflyjl.org/stable/lib/scales/scale\_x\_discrete.html](http://gadflyjl.org/stable/lib/scales/scale_x_discrete.html)  
[http://gadflyjl.org/stable/lib/geometries.html](http://gadflyjl.org/stable/lib/geometries.html)  
[http://gadflyjl.org/stable/lib/geoms/geom\_boxplot.html](http://gadflyjl.org/stable/lib/geoms/geom_boxplot.html)

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [February 27, 2018, 12:03am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/7 "2018-02-27T00:03:32Z")

</div>

One can even do:

`@df yourdataframe boxplot([:Smarcc1 :Socs3])`

Or, to do all the columns:

```julia
cn = names(yourdataframe)
@df yourdataframe boxplot(cols(cn))

```

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [February 27, 2018, 9:41am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/8 "2018-02-27T09:41:24Z")

</div>

Most of such plotting code will generally assume you to have [tidy data](http://r4ds.had.co.nz/tidy-data.html#tidy-data-1), i.e.

```julia
Variable | Value
---------|---------
Smarcc1 | 14.0695
Smarcc1 | 12.4571
...
Tcf7l1 | 15.0319

```

and then

```julia
using StatPlots
@df yourdataframe boxplot(:Variable, :Value)

```

Use `DataFrames.stack` to reshape the DataFrame.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [June 29, 2022, 2:07pm UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/9 "2022-06-29T14:07:28Z")

</div>

Since this comes up quite high on google: `StatPlots.jl` has been renamed to `StatsPlots.jl`.

---

<div class="post-metadata">

**Author:** ![indymnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/indymnv/32/212594_2.png) [@indymnv](https://discourse.julialang.org/u/indymnv)\
**Post date:** [November 12, 2022, 12:26am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/10 "2022-11-12T00:26:18Z")

</div>

I am trying to do the same to plot all the columns in the dataframe (If I am selecting one by one it works), but this code doesn´t work for me. The error message I got is this:

```julia
ERROR: UndefVarError: cols not defined
Stacktrace:
 [1] top-level scope

```

Is now a new way to select all the columns?

---

<div class="post-metadata">

**Author:** ![indymnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/indymnv/32/212594_2.png) [@indymnv](https://discourse.julialang.org/u/indymnv)\
**Post date:** [November 12, 2022, 2:03am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/11 "2022-11-12T02:03:47Z")

</div>

At the end I could solve it using this

```julia
N = ncol(df)
@df float_df boxplot(cols(1:N))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 12, 2022, 9:47am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/12 "2022-11-12T09:47:56Z")

</div>

> [@indymnv](#):
>
> ```julia
> N = ncol(df)
> @df float_df boxplot(cols(1:N))
> 
> ```

To plot all the columns, simply:

```julia
@df float_df boxplot(cols())

```

---

<div class="post-metadata">

**Author:** ![indymnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/indymnv/32/212594_2.png) [@indymnv](https://discourse.julialang.org/u/indymnv)\
**Post date:** [November 12, 2022, 10:03am UTC](https://discourse.julialang.org/t/how-to-produce-nice-boxplots/9345/13 "2022-11-12T10:03:56Z")

</div>

good to know, Thank u!
