# How to use secondary axis for specific series in a groupedbar plot?

**URL:** https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173
**Category:** Visualization
**Tags:** plots, statsplots
**Created:** [August 14, 2024, 10:52am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173 "2024-08-14T10:52:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 14, 2024, 10:52am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/1 "2024-08-14T10:52:08Z")

</div>

Is it possible to specify a secondary y-axis for some of the series in a `groupedbar` plot ? (I am using Plots.jl/StatsPlots.jl)

I have tried to split it in two separate calls with `twinxs()`, but it doesn’t work:

```julia
x = [1 3 120 270; 3 4 280 240; 2 5 220 270; 4 4 380 320]
groupedbar(x[:,[1,2]])
groupedbar!(twinx(),x[:,[3,4]]) # MethodError: no method matching grouped_xy(::Plots.Subplot{Plots.GRBackend}, ::Matrix{Int64})

```

---

<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: [August 14, 2024, 7:17pm UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/2 "2024-08-14T19:17:09Z")

</div>

Would a result like this be OK:

 ![StatsPlots_groupedbar_twinx](https://global.discourse-cdn.com/julialang/original/3X/d/6/d67fdd48bc9353d190ada9659ebb3f62c09566b4.png)

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 15, 2024, 7:57am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/3 "2024-08-15T07:57:25Z")

</div>

Thank you, but in your plot the two groups of series have different domain, while in my case the domain (the rows in the table, that is the value in the x axis) would be the same across all the series, just some series have much higher values…

---

<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: [August 15, 2024, 8:10am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/4 "2024-08-15T08:10:42Z")

</div>

Ah, OK. The same domain case is less complicated. Check if this is alright:

 ![StatsPlots_groupedbar_twinx_same_domain](https://global.discourse-cdn.com/julialang/original/3X/6/9/69f6a6d18aa99aa439a4006e2d03ca4844afe13a.png)

> **StatsPlots groupedbar secondary y-axis**
>
> ```julia
> using StatsPlots; gr(dpi=600)
> 
> x = [1 3 120 270.; 3 4 280 240; 2 5 220 270; 4 4 380 320.]
> 
> ix1, ix2 = [1,2], [3,4]
> x1, x2 = x[:, ix1], x[:, ix2]
> scaler = 4 * maximum(x1) / maximum(x2) # adjust as esthetically desired
> xx = copy(x)
> xx[:, ix2] .*= scaler
> labels = string.(2012:4:2024)
> p3 = groupedbar(xx, labels=true, ylabel="Series 1 & 2", xticks=(1:size(x,1), labels), foreground_color_legend=nothing)
> plot!(twinx(), ylims=ylims(p3)./scaler, ylabel="Series 3 & 4")
> 
> ```

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 15, 2024, 8:27am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/5 "2024-08-15T08:27:00Z")

</div>

ok, now give me the code 😉  
Makie?

---

<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: [August 15, 2024, 8:36am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/6 "2024-08-15T08:36:37Z")

</div>

I have posted the Plots.jl / StatsPlots.jl code below the plot.

It quickly illustrates how to do it for your specific example, but the scaling logic should need some more work.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [August 15, 2024, 9:04am UTC](https://discourse.julialang.org/t/how-to-use-secondary-axis-for-specific-series-in-a-groupedbar-plot/118173/7 "2024-08-15T09:04:32Z")

</div>

Ok, so the trick is to plot all the 4 series, with the 3rd and 4th ones “prescaled” on the y1 axis but then add an opportunity scaled and labeled secondary y axis with not actual series…  
Actually this approach could be useful in many cases, as twinx() often gives me problems…  
I could manualy prescale the series that should go on the secondary y axis and then just add the empty axis…  
Thank you…
