Search code examples
juliagadfly

Gadfly for Julia, plotting multiple dimension in Bar Chart


I have question regarding the plotting package Gadfly for Julia Language. Suppose I have a DataFrame that Looks Like This:

DataFrame:(DrinkType, Country, Month, Sales)
Coke, UK, April, 500
Coke, US, April, 500
Coke, UK, March, 400
Coke, US, March, 700

I want to generate a bar chart, for each DrinkType, where color divides to red and green for UK or US in a dodge style, and Month March and April for each country in Light green and light red (opaque colors) in a stacked style.

The result should show 2 bars for each Drink, something like:

Coke: bar1, stacked US(400 March (light red), 500 April (red))
      bar2, stacked UK(700 March (light greed), 500 April (green)) 

so far, I have came up with this:

t0 = plot(result, y=:Drink, x=:x1, color=:Country, Theme(minor_label_font_size= 12pt, bar_spacing = -0.15cm), Geom.bar(position=:dodge, orientation=:horizontal) 
                      ,Scale.color_discrete_manual(Green,Orange),
              Guide.title("Overall Drink Trends") , Guide.ylabel(nothing),Guide.xlabel(nothing))

This will generate the 4 bars individually...


Solution

  • If I understand the question right, you can generate the plot you're looking for (minus the change in alpha value for different months) with

    using DataFrames, Gadfly
    data = DataFrame()
    data[:DrinkType] =["Coke","Coke","Coke","Coke","Pepsi","Pepsi","Pepsi","Pepsi"]
    data[:Country] = ["UK", "US", "UK", "US","UK", "US", "UK", "US"]
    data[:Month] = ["April", "April", "March", "March","April", "April", "March", "March"]
    data[:Sales] = [500,500,400,700,340,120,990,620]
    data
    

    If you also want to show another dimension one way is to use a subplot grid like

    plot(data, xgroup="DrinkType", x="Month", y="Sales", color="Country", Geom.subplot_grid(Geom.bar(position=:dodge)),Scale.color_discrete_manual("red","green"))
    

    or

    plot(data, xgroup="Country", x="Month", y="Sales", color="DrinkType", Geom.subplot_grid(Geom.bar(position=:stack)),Scale.color_discrete_manual("red","green"))
    

    or similar, although at that point bar charts may not be your best option.