# Plotting coefplot

**URL:** <https://discourse.julialang.org/t/plotting-coefplot/45779>\
**Category:** Visualization\
**Tags:** plotting\
**Created:** [August 30, 2020, 4:13am UTC](https://discourse.julialang.org/t/plotting-coefplot/45779 "2020-08-30T04:13:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerytyui1234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qwerytyui1234/32/31952_2.png) [@qwerytyui1234](https://discourse.julialang.org/u/qwerytyui1234)\
**Post date:** [August 30, 2020, 4:13am UTC](https://discourse.julialang.org/t/plotting-coefplot/45779/1 "2020-08-30T04:13:17Z")

</div>

Hello everyone,

I am trying to plot something like this (attached below), but I am not able to find how to plot this particular way. How would you plot something like this? Thanks in advance.

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/a/c/ac9cbc9a9344631d91a1f06725f0290bb1844af1.png)

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [August 30, 2020, 6:39am UTC](https://discourse.julialang.org/t/plotting-coefplot/45779/2 "2020-08-30T06:39:07Z")

</div>

I’ve used [Coefplot](https://boris.unibe.ch/48527/1/jann-2013-coefplot.pdf): it’s a Stata program to plot statistics & confidence intervals.

I’m not aware of this kind of package in Julia.  
First we need more uniformity in the statistics/econometrics package APIs.  
Suppose someone writes `Coefplot.jl`: it should plot estimates & conf intervals from `GLM.jl`, [FixedEffects](https://github.com/FixedEffects/FixedEffectModels.jl).jl, `QuantileRegressions.jl` etc

Currently, there is not enough coordination/cooperation.  
Eg: if I wanna run a [quantile regression w/ fixed effects](https://discourse.julialang.org/t/regression-fixed-effects-quantiles/45427), the two existing packages don’t work together…  
Other parts of the Julia ecosystem are very modular & cohesive.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [August 30, 2020, 7:51am UTC](https://discourse.julialang.org/t/plotting-coefplot/45779/3 "2020-08-30T07:51:38Z")

</div>

I prefer a horizontal arrangement with more information (a thicker line for the IQR, a dot for the median) — here is some code using PGFPlotsX, with five quantiles. Should be easy to customize.

```julia
using PGFPlotsX

function horiz_q5(x5, y; color = "black")
    plots = @pgf [Plot({ thin, color = color }, Table(x5[[1,5]], [y, y])),
                  Plot({ very_thick, color = color }, Table(x5[[2,4]], [y, y])),
                  Plot({ black, only_marks, color = color }, Table(x5[3:3], [y]))]
end

function horiz_q5_plots(name_q5_pairs; color = "blue")
    @pgf axis = Axis({ xmajorgrids, ytick = .-collect(axes(name_q5_pairs, 1)),
                       yticklabels = first.(name_q5_pairs) })
    for (i, (name, q5)) in enumerate(name_q5_pairs)
        append!(axis, horiz_q5(q5, -i; color = color))
    end
    axis
end

horiz_q5_plots(["Fuel consumption (l/100km)" => range(5, 7; length = 5),
                "Length (m)" => range(4.2, 5; length = 5),
                "Turn circle (m)" => range(7, 12; length = 5)])

```

 ![x](https://global.discourse-cdn.com/julialang/original/3X/6/6/661f1488f94a4d4e920956b3c9db294071a99392.png)

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [August 30, 2020, 2:55pm UTC](https://discourse.julialang.org/t/plotting-coefplot/45779/4 "2020-08-30T14:55:49Z")

</div>

Here’s a pretty trivial example to get you started. This is definitely feasible.

```julia
julia> function coefplot(m)
       n = coefnames(m)[2:end] # no intercept
       vals = coef(m)[2:end]
       errors = stderr(m)[2:end]
       scatter(
           n,
           vals,
           legend = false,
           yerror = 1.96 .* errors,
           title = "Coefficient plot"
       )
       end

julia> df = DataFrame(y = rand(100), x1 = rand(100), x2 = rand(100));

julia> m = lm(@formula(y ~ x1 + x2), df);

julia> coefplot(m)

```

 ![Screen Shot 2020-08-30 at 10.55.11 AM](https://global.discourse-cdn.com/julialang/original/3X/6/8/68e0f07241f5022e6c60c7f80d41bf5d9fa05aa3.png)

---

<div class="post-metadata">

**Author:** ![qwerytyui1234](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/qwerytyui1234/32/31952_2.png) [@qwerytyui1234](https://discourse.julialang.org/u/qwerytyui1234)\
**Post date:** [August 30, 2020, 3:25pm UTC](https://discourse.julialang.org/t/plotting-coefplot/45779/5 "2020-08-30T15:25:26Z")

</div>

Wow this is perfect. Thank you so much. And thank you to everyone else for your helpful replies.

Edit: cant believe there was an option under yerror. I guess I didnt search for the right terms.
