# McClean: Plotting Stacked Histograms

### From OpenWetWare

(→Example) |
(→Example) |
||

Line 84: | Line 84: | ||

</pre> | </pre> | ||

- | + | [Image:ExampleStackedHistograms.png|x60px] | |

Save your figure in a variety of formats for later use (recall that we made h our figure handle): | Save your figure in a variety of formats for later use (recall that we made h our figure handle): |

## Revision as of 18:21, 17 July 2013

## Contents |

# Summary

This explains the basics of plotting histograms stacked vertically (this allows you to see the shift, in for instance, fluorescence in a population of cells analyzed by flow cytometry).

# Example

Your data could be anything. In this example, the variable "Data" contains five (5) rows, each of which contain 9000 fluorescence readings from a FACS experiment. Each row represents a timepoint, with induction of GFP increasing with time.

%% Preliminaries: close all; clear all; load('Data.mat')

Chose bins (you probably want to use the same bin for every plot, since you will be stacking them along the same y-axis) and then bin your data using the Matlab "hist" command. We also keep track of the distributions' means since we use this to color the histograms later.

%Set up bins (we are making histograms of flow cytometry data so we chose logarithmically spaced bins): bins=logspace(0,4,60); x=bins; %Bin the data using "hist" and keep track of the number of elements "n" in each bin "x" for each row in "Data". Also keep track of the mean of each row of "Data": HistData=[]; Means=[]; for i=1:5 [n,x]=hist(Data(i,:),x); HistData=[HistData; n./sum(n)]; Means=[Means mean(Data(i,:))]; end

We set up a colormap so that our histograms change in color as the mean of their distribution increases:

%% Define a colormap for the histograms that will make the histograms brighter as the mean of the distribution increases % In this case we chose to make the histograms brighter green at higher % mean values since the flow cytometry data is of GFP. %Define a color map MMColorMap=zeros(5,3); %Define colors so that they scale with the difference between the mean %fluorescence at a given timepoint and the mean at time 0 MM=sort(Means); MMdiff=Means-Means(1); MMdiff=MMdiff./(max(MMdiff)); MMColorMap(1:end,2)=MMdiff; %Set up the figure and axis properties: h=figure; hold; colors=colormap; set(gca,'XScale','log') set(gca,'XLim',[10,2000]) set(gca,'PlotBoxAspectRatioMode','manual') set(gca,'PlotBoxAspectRatio',[1 3 1]) set(gca,'FontSize',12) set(gca,'XTick',[100 1000 10000 100000]) set(gca,'YTick',[0 1]) ylabel('Fraction of Cell Population','FontSize',14) xlabel('Fluorescence [a.u.]','FontSize',14)

Plot the histograms along the y-axis. We choose the spacing variable empirically so that the plot "looks good":

spacing=.15; %Spacing along the y-axis chosen empirically for i=1:5 fill([x(1);x'; x'],[i*spacing; (HistData(i,:)+i*spacing)'; ones(1,length(x))'*i*spacing],MMColorMap(i,:),'LineStyle','none') semilogx(x,HistData(i,:)+i*spacing,'LineWidth',3,'Color','k'); end

[Image:ExampleStackedHistograms.png|x60px]

Save your figure in a variety of formats for later use (recall that we made h our figure handle):

saveas(h,'ExampleStackedHistograms','fig') saveas(h,'ExampleStackedHistograms','png') saveas(h,'ExampleStackedHistograms','ai') saveas(h,'ExampleStackedHistograms','pdf')

# Code

You can copy and paste the code below into a Matlab m-file to run all of the examples shown above. You will also the "Data.mat" example data:

%% Preliminaries: close all; clear all; load('Data.mat') %% Define the bins to use for our data (you will need to adjust this depending on your data): %In this case we are using the same bins for each data set. You probably %want to do this when you are plotting stacked histograms. bins=logspace(0,4,60); x=bins; %% Bin your data using Matlabs "hist" function. %The variable "n" will be the number in each bin described by the variable %"x". HistData will become a matrix of the normalized bins (normalized to %the total number of elements). Means will become a vector of the mean %value for each distribution, which we will use when coloring our %histograms (so that colors roughly correspond to the mean of the %distribution). HistData=[]; Means=[]; for i=1:5 [n,x]=hist(Data(i,:),x); HistData=[HistData; n./sum(n)]; Means=[Means mean(Data(i,:))]; end %% Define a colormap for the histograms that will make the histograms brighter as the mean of the distribution increases % In this case we chose to make the histograms brighter green at higher % mean values since the flow cytometry data is of GFP. %Define a color map MMColorMap=zeros(5,3); %Define colors so that they scale with the difference between the mean %fluorescence at a given timepoint and the mean at time 0 MM=sort(Means); MMdiff=Means-Means(1); MMdiff=MMdiff./(max(MMdiff)); MMColorMap(1:end,2)=MMdiff; %Set up the figure and axis properties: h=figure; hold; colors=colormap; set(gca,'XScale','log') set(gca,'XLim',[10,2000]) set(gca,'PlotBoxAspectRatioMode','manual') set(gca,'PlotBoxAspectRatio',[1 3 1]) set(gca,'FontSize',12) set(gca,'XTick',[100 1000 10000 100000]) set(gca,'YTick',[0 1]) ylabel('Fraction of Cell Population','FontSize',14) xlabel('Fluorescence [a.u.]','FontSize',14) %% Plot the histograms along the y-axis spacing=.15; %Spacing along the y-axis chosen empirically for i=1:5 fill([x(1);x'; x'],[i*spacing; (HistData(i,:)+i*spacing)'; ones(1,length(x))'*i*spacing],MMColorMap(i,:),'LineStyle','none') semilogx(x,HistData(i,:)+i*spacing,'LineWidth',3,'Color','k'); end %% Save the histogram figure saveas(h,'ExampleStackedHistograms','fig') saveas(h,'ExampleStackedHistograms','png') saveas(h,'ExampleStackedHistograms','ai') saveas(h,'ExampleStackedHistograms','pdf')

# Notes

Please feel free to post comments, questions, or improvements to this protocol. Happy to have your input!

**Megan N McClean 17:27, 11 June 2012 (EDT)**: There are probably more elegant ways of doing this, but this solution has worked well for me so far. Please feel free to update and add information as you figure out better ways of doing this.

# References

Function xticklabel_rotate: xticklabel_rotate

Function barwitherr: barwitherr

# Contact

**Megan N McClean 14:01, 11 June 2012 (EDT)**

or instead, discuss this protocol.