0

我对 Julia 语言的绘图包 Gadfly 有疑问。假设我有一个如下所示的 DataFrame:

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

我想为每个 DrinkType 生成一个条形图,其中颜色分为英国或美国的红色和绿色以闪避风格,以及每个国家的月 3 月和 4 月为浅绿色和浅红色(不透明颜色)以堆叠风格.

结果应该为每种饮料显示 2 个酒吧,例如:

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

到目前为止,我想出了这个:

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))

这将分别生成 4 个条形...

4

1 回答 1

3

如果我理解正确的问题,您可以生成您正在寻找的图(减去不同月份的 alpha 值的变化)

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

如果您还想显示另一个维度,一种方法是使用子图网格,例如

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

或者

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

或类似的,尽管此时条形图可能不是您的最佳选择。

于 2015-05-01T15:57:08.743 回答