create a bar with different colors representing different categorical data

HI,
I have a list of data with cancer (c) and normal samples (n) sorted to certain order. I want to make a figure to show a colored bar, with red representing the position of cancer samples and green for normal samples on that bar, then I will repeat this for multiple lists to generate a bar for each list. Could anyone let me know how to do this?
My lists looks like:
1. c c c c c c n n c n n c n n n
2. c n c c n c c n n c n c c c n
...
Many thanks!
Jie

 Accepted Answer

Run this:
% Your samples:
S1 = 'c c c c c c n n c n n c n n n';
S2 = 'c n c c n c c n n c n c c c n';
% Remove spaces:
S1(S1==32) = [];
S2(S2==32) = [];
% Concatenate all samples:
S = double(vertcat(S1,S2));
% Display:
imagesc(S)
% Change colors:
colormap([1 0 0;0 1 0])
% Label stuff:
set(gca,'ytick',1:size(S,1))
ylabel('sample number')
xlabel('position')
cb = colorbar('southoutside');
set(cb,'ytick',[99 110],'yticklabel',{'cancer','normal'})
caxis([94 114])

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!