如何使用Julia包Gadfly在箱图中排序类别变量

问题描述

Gadfly似乎没有使用分类变量的(级别)顺序:

using CSV
using DataFrames
using Gadfly
using HTTP

url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv"

tips = CSV.File(HTTP.get(url).body) |> DataFrame
categorical!(tips,:day)
ordered!(tips.day,true)
levels!(tips.day,["Thur","Fri","Sat","Sun"])

Gadfly.plot(tips,x=:day,y=:total_bill,color=:smoker,Geom.Boxplot)

enter image description here

图应该不继承分类变量中指定的顺序吗?

我找到了一种排序类别值的方法,但是由于再次指定了顺序,因此感觉有点“笨拙”。

Gadfly.plot(tips,Geom.Boxplot,Scale.x_discrete(levels=levels(tips.day)))

enter image description here

任何建议如何解决这个问题?

解决方法

在Gadfly中,对于离散的x,值的顺序由其在数据帧中的顺序确定(因此,当前不支持CategoricalArray中的级别顺序)。将来可能不支持它,因为DataFrames计划删除CategoricalArrays(https://github.com/JuliaData/DataFrames.jl/issues/2321)。