# Bar chart with rotated x label for each bar

**URL:** https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554
**Category:** Visualization
**Created:** [September 9, 2019, 11:58am UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554 "2019-09-09T11:58:10Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [September 9, 2019, 11:58am UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/1 "2019-09-09T11:58:10Z")

</div>

I’m having a hard time plotting a simple bar chart with Plots.jl that has a label below each bar. The data are stored in a DataFrame that looks like this:

```julia
julia> merged[:, [1,5,6]]
85×3 DataFrame
│ Row │ naics │ % of total estabs │ industry │
│ │ Int64 │ Float64 │ String │
├─────┼───────┼───────────────────┼────────────────────┤
│ 1 │ 113 │ 0.00112943 │ Forestry and Lo... │
│ 2 │ 114 │ 0.000362778 │ Fishing, Huntin... │
│ 3 │ 115 │ 0.00142785 │ Support Activit... │
│ 4 │ 211 │ 0.000947334 │ Oil and Gas Ext... │
⋮
│ 81 │ 721 │ 0.00863121 │ Accommodation │
│ 82 │ 722 │ 0.0822932 │ Food Services a... │
│ 83 │ 811 │ 0.0277618 │ Repair and Main... │
│ 84 │ 812 │ 0.0297605 │ Personal and La... │
│ 85 │ 813 │ 0.0399547 │ Religious, Gran... │

```

I can achieve a plot that has vertical labels, but the labels don’t show up for every bar and there are other issues with the plot:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/9/e99dde5b1b389e35dcd41a7ac811837e0eb7b9a6.png)

The other issues are that there is a space to the left and right of the first/last bars that I would like to eliminate and the labels aren’t centered under the bars.

The code to produce the above is:

```julia
bar(string.(merged[:, 1]), merged[:, 4], xrotation=90, legend=:topleft)

```

Any ideas?

Thanks!!

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 9, 2019, 12:29pm UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/2 "2019-09-09T12:29:14Z")

</div>

Try setting xticks manually. I think something like `xticks=minimum(df.naics):maximum(df.naics)` (assuming that’s the column that provides the x values. Compare:

```julia
julia> df = DataFrame(idx=1:20, val = rand(20), label = [randstring(4) for _ in 1:20]);

julia> bar(df.idx, df.val)

julia> bar(df.idx, df.val, legend=false)

```

 ![50%20AM](https://global.discourse-cdn.com/julialang/original/3X/5/b/5b0f67fb72f75f3c73b146d5493ccee09181fb8c.jpeg)

to

```julia
bar(df.idx, df.val, legend=false, xticks=1:20)

```

 ![48%20AM](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2d31aad720628a27c4b2b3a2aabaeb9bd7eadde.jpeg)

You can also set the labels to be strings:

```julia
bar(df.idx, df.val, legend=false, xticks=(1:20, df.label), xrotation=90)

```

 ![45%20AM](https://global.discourse-cdn.com/julialang/original/3X/9/3/937655a5a23c87b47c35beeba83101eb8ef726d8.jpeg)

As for centering text, I’m guessing it’s something with `xfontvalign` or `xfonthalign`, but I can’t figure it out. And for removing the spaces, take a look at the various `framestyle`s, and I think there are arguments involving margins, but I don’t know for sure. See the bottom of [this page](http://docs.juliaplots.org/latest/examples/gr/#portfolio-composition-maps) for all the attributes you can play with.

Would be nice if the possible arguments for all of those attributes lived somewhere in the docs too.

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [September 9, 2019, 12:37pm UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/3 "2019-09-09T12:37:08Z")

</div>

This worked (except for the label alignment), thanks!

```julia
bar(
    string.(merged[:, 1]),
    merged[:, 4],
    xrotation=90,
    legend=:topleft,
    fillalpha=0.25,
    label="% of total estabs",
    xticks=(1:size(merged)[1], merged.naics)
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/b/dbfee44667e0edfad3b031e6167e6713faa5fb8b.png)

I’m not sure that all the arguments listed as supported actually work. I’ve tried using the `bottom_margin` argument, for example, to no avail.

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [September 9, 2019, 1:27pm UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/4 "2019-09-09T13:27:11Z")

</div>

FYI, I was able to align the ticks to the center of the bars by doing this:

```julia
bar(
    string.(merged[:, 1]),
    merged[:, 4],
    xrotation=45, # changing the rotation to 45 degrees makes the labels look centered
    legend=:topleft,
    fillalpha=0.25,
    label="% of total estabs",
    xticks=(0.5:size(merged)[1], merged.naics) # notice I changed 1 to 0.5
)

```

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/9/7980d28600c58421525ebebeeb531b397ba54361.png)

For the labels, the 45-degree rotation isn’t an ideal solution, but it’ll do for now. 😃

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 9, 2019, 3:09pm UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/5 "2019-09-09T15:09:35Z")

</div>

> [@mthelm85](#):
>
> I’m not sure that all the arguments listed as supported actually work. I’ve tried using the `bottom_margin` argument, for example, to no avail.

If so, might be worth opening an issue.

---

<div class="post-metadata">

### Author: ![davidanthoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/davidanthoff/32/223493_2.png) [@davidanthoff](https://discourse.julialang.org/u/davidanthoff)
#### Post date: [September 9, 2019, 5:32pm UTC](https://discourse.julialang.org/t/bar-chart-with-rotated-x-label-for-each-bar/28554/6 "2019-09-09T17:32:38Z")

</div>

Here is the [VegaLite.jl](https://github.com/queryverse/VegaLite.jl) version:

```julia
df |> @vlplot(:bar, x="idx:n", y=:val)

```

And that produces:  
 ![example](https://global.discourse-cdn.com/julialang/original/3X/b/e/be204295696d3825bf0da8b0fdb31e01299f6a9e.png)

The trick is to configure the x axis as a nominal, not quant, scale by adding `:n` to the column name. For a nominal scale, it will print all values.
