Skip to content

Instantly share code, notes, and snippets.

@kescobo
Created August 23, 2017 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kescobo/799bf484e96a854857cb65f2c5685693 to your computer and use it in GitHub Desktop.
Save kescobo/799bf484e96a854857cb65f2c5685693 to your computer and use it in GitHub Desktop.
function hclustplot(hc::Hclust, useheight::Bool)
o = indexmap(hc.order)
n = [x for x in 1:length(o)]
pos = treepositions(hc, useheight)
xs = []
ys = []
for i in 1: size(hc.merge, 1)
x1 = pos[hc.merge[i,1]][1]
x2 = pos[hc.merge[i,2]][1]
append!(xs, [x1,x1,x2,x2])
y1 = pos[hc.merge[i,1]][2]
y2 = pos[hc.merge[i,2]][2]
useheight ? h = hc.height[i] : h = 1
newy = maximum([y1,y2]) + h
append!(ys, [y1,newy,newy,y2])
end
return (reshape(xs, 4, size(hc.merge, 1)), reshape(ys, 4, size(hc.merge, 1)))
end
function treepositions(hc::Hclust, useheight::Bool)
order = indexmap(hc.order)
positions = Dict{}()
for (k,v) in order
positions[-k] = (v, 0)
end
for i in 1:size(hc.merge,1)
xpos = mean([positions[hc.merge[i,1]][1], positions[hc.merge[i,2]][1]])
if hc.merge[i,1] < 0 && hc.merge[i,2] < 0
useheight ? ypos = hc.height[i] : ypos = 1
else
useheight ? h = hc.height[i] : h = 1
ypos = maximum([positions[hc.merge[i,1]][2], positions[hc.merge[i,2]][2]]) + h
end
positions[i] = (xpos, ypos)
end
return positions
end
@jingpengw
Copy link

I found that the indexmap was in DataFrames package. it is easy to make a customized one though.

@srgk26
Copy link

srgk26 commented Apr 5, 2019

indexmap and mean are now in StatsBase and Statistics packages respectively. So simply add using StatsBase, Statistics to the start of the recipe. Or simply using StatsKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment