# Changing orientation of plots in gadfly

**URL:** https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506
**Category:** General Usage
**Tags:** question, plotting, gadfly
**Created:** [January 25, 2023, 12:34pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506 "2023-01-25T12:34:26Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 12:34pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/1 "2023-01-25T12:34:26Z")

</div>

Sorry to reopen this issue, but I am trying to recreate this same plotting style in Gadfly as this issue [Plotting histogram on the y-axis at the end of a time series](https://discourse.julialang.org/t/plotting-histogram-on-the-y-axis-at-the-end-of-a-time-series/5381). I have these two plots and want to plot side by side with the second plot being horizontal, however, unlike the previous issue I cannot just change the orientation of the histogram because I have a layer on it, I think I can achieve this by changing the orientation of the layer but I’m not sure how to do that.

```julia

tsteps = range(tspan[1], tspan[2], length = datasize)
GBM_Means = S₀.*exp.(r.*tsteps)
tsteps1 = tsteps[:,1]*30
GBM_Means = convert(Vector{Float32}, GBM_Means)
layer1 = Gadfly.layer(x = tsteps1,y = GBM_Means, Geom.line,linestyle=[:dash],color=[colorant"black"])
pa = Gadfly.layer(y_sort, x=Row.index, y=Col.value, color=Col.index, Geom.line)
plt1 = Gadfly.plot(layer1,pa,Guide.xlabel("Time"),Guide.ylabel("Stock Price"),Guide.yticks(ticks = [50 75 100 125 150 175]), Scale.ContinuousColorScale(p -> get(ColorSchemes.:YlOrBr_6, p)),Theme(key_label_font_size=6pt,major_label_font_size=7pt,key_position = :none),Guide.manual_color_key("", ["Neural SDE Stock Price Path","Mean"],["#FFA500","#000000"],pos=[0.05w,-0.45h],shape=[Gadfly.Shape.hline,Gadfly.Shape.hline]),Coord.cartesian(xmin=0, xmax=30, ymin=50, ymax=182))

```

 ![Screenshot 2023-01-25 at 02.16.20](https://global.discourse-cdn.com/julialang/original/3X/0/d/0d6cff71edb2a1cb23bf985422d42be21a7e4e9a.jpeg)

and

```julia
lambda = -(4*0.08*exp(0.08))/(4*(1-exp(0.08)))*100
#LN = LogNormal(0.08,0.2)
LN = NoncentralChisq(1,lambda)*(-4(1-exp(0.08))/(4*0.08))
s = 40:0.005:200
s = s[:,1]
LNPDF = pdf.(LN,s)
layer1 = Gadfly.layer(x= s,y= LNPDF, ymin= zeros(size(LNPDF)), Theme(alphas=[0.3],default_color=colorant"black") , ymax= LNPDF, Geom.line ,Geom.ribbon)
p = Gadfly.plot(layer1)
plt = Gadfly.plot(layer1,x = y1,Guide.xlabel("Frequency"),Guide.ylabel(nothing),Theme(key_label_font_size=6pt,major_label_font_size=7pt,key_position = :none,default_color=colorant"orange"),Guide.manual_color_key("", ["Neural SDE Terminal Stock Price","Non-central Chi-squared Density Function"],["#FFA500","#000000"],pos=[0.6w,-0.45h],shape=[Gadfly.Shape.hline,Gadfly.Shape.hline]),xintercept=[m], Geom.vline(color=["black"],style=[:dash]),Geom.histogram(density=:true, bincount=20),Guide.xticks(ticks = [40 50 75 100 125 150 175 200]), Guide.xlabel(nothing))

```

 ![Screenshot 2023-01-25 at 02.17.18](https://global.discourse-cdn.com/julialang/original/3X/e/f/efa5fc44a57b338af6a0e8aa26bd98d5accdf3d7.jpeg)

I would like to plot these as stated in the question. Essentially I want it to look like this:

 ![Screenshot 2023-01-25 at 03.08.17](https://global.discourse-cdn.com/julialang/original/3X/6/a/6a2dae41b2ce08b9b0bdf69c9c8fe2afa9f5fa35.jpeg)

Also as a side question, is it possible to change the x-axis of the first plot to not use Row.index?

---

<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: [January 25, 2023, 10:32pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/2 "2023-01-25T22:32:45Z")

</div>

For the side plot, does this work for you? (just an example):

```julia

layer1 = layer(y=s, x=LNPDF, xmin=zeros(size(LNPDF)), Theme(alphas=[0.3], default_color=colorant"black") , xmax= LNPDF, Geom.line ,Geom.ribbon)
layer2 = layer(yintercept=[m], Geom.hline(color=["black"],style=[:dash]))
plt = plot(layer2, layer1,
    y=y1, Geom.histogram(density=true, bincount=20, orientation=:horizontal), 
    Guide.xlabel("Frequency"), Guide.ylabel(nothing),
    Theme(key_label_font_size=6pt, major_label_font_size=7pt, key_position=:none, default_color=colorant"orange"))

```

![NcChisq](https://global.discourse-cdn.com/julialang/original/3X/8/8/88a0906820f30f344a8b6ff9d695129012b9b9f1.png)

---

<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: [January 25, 2023, 10:46pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/3 "2023-01-25T22:46:01Z")

</div>

For your `Row.index` question, see the last example on the [Gadfly docs Plotting page](http://gadflyjl.org/stable/man/plotting/)

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:03pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/4 "2023-01-25T23:03:21Z")

</div>

I get the following error message:

```julia
Couldn't process recipe args: (Vector{Layer}, Vector{Layer}, Gadfly.Geom.BarGeometry, Gadfly.Guide.XLabel, Gadfly.Guide.YLabel, Theme)

```

---

<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: [January 25, 2023, 11:08pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/5 "2023-01-25T23:08:48Z")

</div>

Plots.jl uses recipes, not Gadfly. Have you accidentally imported Plots, or another package you’re using?

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:13pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/6 "2023-01-25T23:13:04Z")

</div>

you are correct, sorry I was using both packages. I did what you suggested by switching the x and y axis and get this:

 ![Screenshot 2023-01-26 at 01.11.04](https://global.discourse-cdn.com/julialang/original/3X/6/e/6e010eaea7b548a903c732fb233985d3b5119ef7.png)  
I think the problem is that Geom.line is connecting the points on the same x values.

---

<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: [January 25, 2023, 11:17pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/7 "2023-01-25T23:17:15Z")

</div>

Did you change `xmin` and `xmax`? I can’t diagnose the issue without seeing code.

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:18pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/8 "2023-01-25T23:18:59Z")

</div>

I coped and pasted this code.  
Here’s the full code:

```julia
prob1 = result3.u
u2 = repeat(reshape(S₀, :, 1), 1, 1000)
y = predict_neuralsde(prob1, u2)[1,:,:]'
y1 = y[end,:]
m = S₀.*exp.(r.*1)
y_sort = y[:, sortperm(y[end,:])];
m = m[1]
lambda = -(4*0.08*exp(0.08))/(4*(1-exp(0.08)))*100
LN = NoncentralChisq(1,lambda)*(-4(1-exp(0.08))/(4*0.08))
s = 40:0.005:200
s = s[:,1]
LNPDF = pdf.(LN,s)

layer1 = layer(y=s, x=LNPDF, xmin=zeros(size(LNPDF)), Theme(alphas=[0.3], default_color=colorant"black") , xmax= LNPDF, Geom.line ,Geom.ribbon)
layer2 = layer(yintercept=[m], Geom.hline(color=["black"],style=[:dash]))
plt = Gadfly.plot(layer2, layer1,
    y=y1, Geom.histogram(density=true, bincount=20, orientation=:horizontal), 
    Guide.xlabel("Frequency"), Guide.ylabel(nothing),
    Theme(key_label_font_size=6pt, major_label_font_size=7pt, key_position=:none, default_color=colorant"orange"))

```

---

<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: [January 25, 2023, 11:21pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/9 "2023-01-25T23:21:26Z")

</div>

What happens if you take out `Geom.line`?

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:23pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/10 "2023-01-25T23:23:23Z")

</div>

It just displays the ribbon, not the outline of the distribution.

 ![Screenshot 2023-01-26 at 01.23.03](https://global.discourse-cdn.com/julialang/original/3X/0/c/0cd361f079e809d73bde10977373f74888e3206e.png)

---

<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: [January 25, 2023, 11:35pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/11 "2023-01-25T23:35:57Z")

</div>

Try `Geom.path` in `layer1`, rather than `Geom.line`. The reason I don’t see the solid black lines is because I’m on Gadfly#master branch, which now has `alpha` for `Geom.line` ([add alpha aesthetic to Geom.line by bjarthur · Pull Request #1603 · GiovineItalia/Gadfly.jl · GitHub](https://github.com/GiovineItalia/Gadfly.jl/pull/1603))

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:39pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/12 "2023-01-25T23:39:32Z")

</div>

> [@Mattriks](#):
>
> `Geom.path`

Works perfectly, thank you.

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:41pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/13 "2023-01-25T23:41:23Z")

</div>

Now just finally on how to hstack the two plots while making this 2nd plot smaller as you showed in the previous issue. I used your exact code and got this error:

```julia
MethodError: no method matching (Array{Context})(::Int64, ::Int64)

```

---

<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: [January 25, 2023, 11:49pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/14 "2023-01-25T23:49:38Z")

</div>

Yes the [empty `Array` constructor](https://docs.julialang.org/en/v1/base/arrays/#Core.Array-Tuple%7BUndefInitializer,%20Any%7D) has changed. Probably easier to do now:

```julia
Array{Compose.Context}(undef, 1, 2)

```

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 25, 2023, 11:57pm UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/15 "2023-01-25T23:57:21Z")

</div>

So the following:

```julia
M = Array{Compose.Context}[]
M[1] = compose(context(0,0,2/3,1), render(plt1))
M[2] = compose(context(0,0,1/3,1), render(plt))
draw(PNG(6.6inch, 3.3inch), hstack(M...))

```

?

Becuase this results in the following error:

```julia
MethodError: Cannot `convert` an object of type 
  Context to an object of type 
  Array{Context}

```

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 26, 2023, 12:06am UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/17 "2023-01-26T00:06:02Z")

</div>

It now creates the array but:

```julia
BoundsError: attempt to access 0-element Vector{Context} at index [1]

```

---

<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: [January 26, 2023, 12:08am UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/18 "2023-01-26T00:08:58Z")

</div>

Edited it again!

---

<div class="post-metadata">

### Author: ![ChrisVL1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvl1/32/45240_2.png) [@ChrisVL1](https://discourse.julialang.org/u/ChrisVL1)
#### Post date: [January 26, 2023, 12:12am UTC](https://discourse.julialang.org/t/changing-orientation-of-plots-in-gadfly/93506/19 "2023-01-26T00:12:55Z")

</div>

> [@Mattriks](#):
>
> `Array{Compose.Context}(undef, 1, 2)`

Works! Thank you so much for all the help!
