如何在 Julia 的 Gadfly 中按升序或降序对条形图进行排序? (有谁知道一种不那么骇人听闻的方法吗?)
How do I sort a bar chart in ascending or descending order in Julia's Gadfly? (Does anyone know a less hacky way?)
我制作了一个 dict-of-counts 风格的 df 来制作条形图,我有兴趣按大小对条形进行降序排序。在 R 世界中,我会做类似 的事情,并且想知道 1) 做这件事的整洁方法是什么,以及 2) Gadfly 中是否存在类似的东西。
我的技巧是这样的:
using DataFrames
using DataStructures
using Gadfly
# test data
list = ["E", "E", "E", "E", "E",
"B", "B", "B", "B",
"C", "C", "C",
"D", "D", "D",
"A"]
# I am making a dict-of-counts to turn into a df
# empty string->int dict
countsDict = Dict{String,Integer}()
# a function to count occurences of a given string in an array
function countStrInArray(str::String, arr::Array{String,1})::Integer
findall(x -> x == str, arr) |> length
end
# for every item in the list
for item in list
# if we don't have it in the dict, add, with count as value
if !haskey(countsDict, item)
countsDict[item] = countStrInArray(item, list)
end
end
# this gives me the structure I want but I lose 'lookup' functionality
sortedTuples = sort(collect(zip(values(countsDict),
keys(countsDict))), rev = true)
# ...so I creaated an order-preserving dict
sortedCountsDict = OrderedDict{String,Integer}()
# map our tuples to it
for item in sortedTuples
sortedCountsDict[item[2]] = item[1]
end
# make it into a dataframe
df = DataFrame(group = [i for i in keys(sortedCountsDict)],
count = [i for i in values(sortedCountsDict)])
# plot it!
plot(df, x = :group, y = :count, Geom.bar) |> SVG("HackyButWorks.svg")
有人知道更简洁的方法吗?
df = DataFrame(
list = ["E", "E", "E", "E", "E","B", "B", "B", "B",
"C", "C", "C", "D", "D", "D","A"]
)
p1 = plot(df, x=:list, Geom.histogram)
p2 = plot(df, x=:list, Geom.histogram, Scale.x_discrete(levels=["A","D","C","B","E"]) )
我制作了一个 dict-of-counts 风格的 df 来制作条形图,我有兴趣按大小对条形进行降序排序。在 R 世界中,我会做类似
我的技巧是这样的:
using DataFrames
using DataStructures
using Gadfly
# test data
list = ["E", "E", "E", "E", "E",
"B", "B", "B", "B",
"C", "C", "C",
"D", "D", "D",
"A"]
# I am making a dict-of-counts to turn into a df
# empty string->int dict
countsDict = Dict{String,Integer}()
# a function to count occurences of a given string in an array
function countStrInArray(str::String, arr::Array{String,1})::Integer
findall(x -> x == str, arr) |> length
end
# for every item in the list
for item in list
# if we don't have it in the dict, add, with count as value
if !haskey(countsDict, item)
countsDict[item] = countStrInArray(item, list)
end
end
# this gives me the structure I want but I lose 'lookup' functionality
sortedTuples = sort(collect(zip(values(countsDict),
keys(countsDict))), rev = true)
# ...so I creaated an order-preserving dict
sortedCountsDict = OrderedDict{String,Integer}()
# map our tuples to it
for item in sortedTuples
sortedCountsDict[item[2]] = item[1]
end
# make it into a dataframe
df = DataFrame(group = [i for i in keys(sortedCountsDict)],
count = [i for i in values(sortedCountsDict)])
# plot it!
plot(df, x = :group, y = :count, Geom.bar) |> SVG("HackyButWorks.svg")
有人知道更简洁的方法吗?
df = DataFrame(
list = ["E", "E", "E", "E", "E","B", "B", "B", "B",
"C", "C", "C", "D", "D", "D","A"]
)
p1 = plot(df, x=:list, Geom.histogram)
p2 = plot(df, x=:list, Geom.histogram, Scale.x_discrete(levels=["A","D","C","B","E"]) )