juliagadfly

Plotting dataset with non-numerical axis


I have a dataset like this:

| Name  | 2017 | 2018 |
-----------------------
| Alice |  12  |  15  |
| Bob   |  11  |  10  |  

I want to plot it with horizontal stacked bar chart such that on the Y axis I have Alice and Bob and on the X axis there are numbers from the table (Alice's bar has 12 and then 15-proportional length, similarly for Bob).

I tried to get data for 2017 first, so I did

using DataFrames, Gadfly
df = readtable("a.csv")
plot(df, x="2017", Geom.bar)

where a.csv is

Name,2017,2018
Alice,12,15
Bob,11,10

But I got

Error showing value of type Gadfly.Plot:

ERROR: UndefVarError: unshift! not defined

I also tried

plot(df, x="2017", y="Name", Geom.bar)

And got

Error showing value of type Gadfly.Plot:

ERROR: MethodError: no method matching zero(::Type{String})

Here the problem seems to be with the fact that the data on Y axis is non-numeric.

I have Julia 0.6.2.


Solution

  • You can do this

    df_tr = stack(df, [:x2017,:x2018])
    Gadfly.plot(df_tr, color = "variable", x="value", y="Name",
                 Geom.bar(position=:stack, orientation= :horizontal))
    

    i got something like this:

    enter image description here