# Apparent inconsistent plotting of histograms in Gadfly

**URL:** https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953
**Category:** Visualization
**Tags:** question, gadfly
**Created:** [March 16, 2022, 8:29am UTC](https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953 "2022-03-16T08:29:04Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)
#### Post date: [March 16, 2022, 8:29am UTC](https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953/1 "2022-03-16T08:29:04Z")

</div>

Why is the histogram for the WildType normalised data differently when plotted with other data compared to being plotted alone?  
This plot command

```julia
plot(df[df.genotype .== "WildType", :], x=:level, Geom.histogram(bincount=200), Scale.x_log10, color=:genotype, Coord.cartesian(xmin=-2, xmax=1, ymin=0, ymax=8000)

```

produces this plot

 ![Screen Shot 2022-03-16 at 19.23.49](https://global.discourse-cdn.com/julialang/original/3X/7/1/71986ce876c36b22d1acb5a75a8ac3d808597dc3.png)

while this plot

```julia
plot(df, x=:level, Geom.histogram(bincount=200), Scale.x_log10, color=:genotype, Coord.cartesian(xmin=-2, xmax=1, ymin=0, ymax=8000))

```

produces

 ![Screen Shot 2022-03-16 at 19.24.56](https://global.discourse-cdn.com/julialang/original/3X/8/4/84dc1496ecfc2ef669af7f22f4a58abfdd0d07be.png)

The WildType data are roughly 3x larger in the second plot. This does not happen to the KO data if I plot it by itself. Am I missing somethong about how Gadfly histograms work?

---

<div class="post-metadata">

### Author: ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)
#### Post date: [March 16, 2022, 10:54pm UTC](https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953/2 "2022-03-16T22:54:00Z")

</div>

Here is a useable example the reproduces the behaviour.

```julia
using Distributions, Gadfly, DataFrames

d1 = Normal(0, 1)
d2 = Normal(1, 0.5)

x = rand(d1, 10000)
df = DataFrame(x=x, class="Class 1")

x = 0.5 .* rand(d2, 10000)

append!(df, DataFrame(x=x, class="Class 2"))

spike = -2.0 .* ones(500)
append!(df, DataFrame(x=spike, class="Class 2"))

h = plot(df[df.class .== "Class 1", :], 
  x=:x, 
  color=:class, 
  Geom.histogram(bincount=200), 
  Coord.cartesian(xmin=-4, xmax=4, ymin=0, ymax=1000)
  )
display(h)

h = plot(df,
  x=:x, 
  color=:class, 
  Geom.histogram(bincount=200), 
  Coord.cartesian(xmin=-4, xmax=4, ymin=0, ymax=1000)
  )
display(h)

```

This produces:

 ![Screen Shot 2022-03-17 at 09.46.57](https://global.discourse-cdn.com/julialang/original/3X/c/1/c10597419901705d1f7cb8f0756749d1b4b0168e.png)  
 ![Screen Shot 2022-03-17 at 09.47.25](https://global.discourse-cdn.com/julialang/original/3X/1/c/1c7ce162a147956f2bacd6a28ffcfde2238370d9.png)

It looks like it has plotted Class 1 as the sum of Class 1 and Class 2. Is this to be expected? Is there a way to turn this behaviour off?

---

<div class="post-metadata">

### Author: ![juliohm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juliohm/32/215266_2.png) [@juliohm](https://discourse.julialang.org/u/juliohm)
#### Post date: [March 16, 2022, 10:58pm UTC](https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953/3 "2022-03-16T22:58:09Z")

</div>

@evan-wehi try to reach the Gadfly.jl maintainers in their community channels. They use Gitter instead of Discourse.

---

<div class="post-metadata">

### Author: ![evan-wehi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evan-wehi/32/32692_2.png) [@evan-wehi](https://discourse.julialang.org/u/evan-wehi)
#### Post date: [March 18, 2022, 2:27am UTC](https://discourse.julialang.org/t/apparent-inconsistent-plotting-of-histograms-in-gadfly/77953/4 "2022-03-18T02:27:39Z")

</div>

My bad - I missed it in the documentation. This behaviour is intended and is controlled by the `position` argument in `Geom.histogram`. I need to use `position=:dodge`.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/6/f6b9a824065e45f7ec00f1567970da1592e061dd.png)
