File Exchange Pick of the Week

Our best user submissions

Treemap

Sean's pick this week isTreemapbyJoe Hicklin.

Contents

Single Treemap

I've recently been working with one of our marketing teams to visualize where MATLAB is used across an organization or university based on department. One nice way to do this is with a treemap because it allows for hierarchical visualization. Let's take a look at a single layer treemap for a synthetic university. The data is stored in table calledactivationswith variablesCollegeandDepartment.

collcounts = countcats(activations.College);% How many in each college?r = treemap(collcounts);% Build tree rectanglesplotRectangles(r,categories(activations.College))% Plot tree map

Hierarchical Treemap

That looks pretty good, but it's just one layer. We also have information on the departments within a college. To include this, I will use thefindgroups/splitapplyworkflow to split the departments based on their colleges.

% Which collegecollegeidx = findgroups(activations.College);% Count the number of activations in each department and bin by college部tcounts = splitapply(@(x){nonzeros(countcats(x))},activations.Department,collegeidx);% Keep the names for labeling部tnames = splitapply(@(x){categories(removecats(x))},activations.Department,collegeidx);

Now we'll copy and modify Joe's example code to plug in my new data.

m = 39;% Department colorsfor2 = 1:元素个数(deptcounts)颜色= (3 * repmat(兰德(1,3),m,1) + rand(m,3))/4; rNew = treemap(deptcounts{ii}',r(3,ii),r(4,ii)); rNew(1,:) = rNew(1,:) + r(1,ii); rNew(2,:) = rNew(2,:) + r(2,ii); plotRectangles(rNew,deptnames{ii},colors)end% Outline college boxesoutline(r)

Interactive Treemap

On my large monitor blown up that looks good. But it's kind of busy compressed. I'll enhance it to make it so that you can expand one school or "Drill down". First, repeat all of the above steps but leave the labels off the departments.

figure plotRectangles(r,categories(activations.College));for2 = 1:元素个数(deptcounts)颜色= (3 * repmat(兰德(1,3),m,1) + rand(m,3))/4; rNew = treemap(deptcounts{ii}',r(3,ii),r(4,ii)); rNew(1,:) = rNew(1,:) + r(1,ii); rNew(2,:) = rNew(2,:) + r(2,ii); plotRectangles(rNew,[],colors)endoutline(r)

Second, I'll disable the ability for the patches to interact by turning off their'HitTest'property.

patches = findall(gca,'Type','patch'); set(patches,'HitTest','off')

Finally, add a button down callback to the axes which calls my custom explode function.

functionexplodeBlock(evt,r,nsubs,sublabels)% Blow up one block in a tree map% Where was hitxyhit = evt.IntersectionPoint(1:2);% Which rectangle?inx = xyhit(1) > r(1,:) & (xyhit(1) < r(1,:)+r(3,:)); iny = xyhit(2) > r(2,:) & (xyhit(2) < r(2,:)+r(4,:)); idx = inx & iny;% Blow up that rectangle in new figurernew = treemap(nsubs{idx}); figure plotRectangles(rnew,sublabels{idx})end
set(gca,'Visible','on') set(gca,'ButtonDownFcn',@(~,evt)explodeBlock(evt,r,deptcounts,deptnames))

Feedback

Using the treemap utility is pretty straight-forward and Joe has provided nice examples of all of the features. I do have a couple of enhancements though:

  1. plotRectanglesandoutlinecould return handles to the patches they create. This would allow me to skip the sloppy call tofindobjthat I have above.
  2. The text, as you probably noticed, tends to overlap a bit. It would be nice for the text to be jittered or rotated so as to avoid the collision.

Comments

Give it a try and let us know what you thinkhereor leave acommentfor Joe.




Published with MATLAB® R2016b

|
  • print
  • send email

Comments

To leave a comment, please clickhereto sign in to your MathWorks Account or create a new one.