gabboshow
gabboshow

Reputation: 5569

superimpose marker on barplot

I create a figure using a barplot that represents means of groups.

I have a matrix that tells me whether the means are statistically different.

sign_diff =

     0     0     0     1
     0     0     0     1
     0     0     0     0
     1     1     0     0

In this case the means of the first group and second group are significantly different from the mean of the 4th group.

How to read the matrix:

first rows: there is a 1 in the last column -> first bar is different from bar 4 so bar 1 and bar 4 get a star.

second row: there is a 1 in the last column -> second bar is different from bar 4 so bar 2 and bar 4 get a star. In more since bar 1 and bar 2 are not different between them the stars in bar 1 and bar 2 should be at the same high

How can I add a marker on top of the bars that are different? I would like to have something like this:

Please note that the first two stars should be at the same levels indicating that bar1 and bar2 do not differ, but they both differ from bar4 (then the star on top of bar 4 should be higher)

Hope you can help me

enter image description here

Upvotes: 1

Views: 89

Answers (1)

sco1
sco1

Reputation: 12214

I'm still not sure I quite grasp the height logic (and we don't have a functioning example) but in the meantime there's a simple answer the superimposition question. You can use line to superimpose the stars onto your plot.

For example:

y = [1 2 3 4];
bar(y);
ylim([0 6]);

sign_diff = [0 0 0 1; 0 0 0 1; 0 0 0 0; 1 1 0 0];

needs_star = (sum(sign_diff) ~= 0); % See which bars need a star
star_heights = sum(sign_diff).*0.75;

star_x = 1:length(y);
star_y = max(y) + star_heights;

star_x = star_x(needs_star);
star_y = star_y(needs_star);


line(star_x, star_y, ...
    'linestyle', 'none', ...
    'marker', 'p', ...
    'markersize', 15 ...
    );

Produces the following:

enter image description here

line accepts XY inputs, so if you can create coordinates for your stars based on your sign_diff matrix you can use them in the line call.

Edit: I have updated with my stab at figuring out the logic. Some tweaks will be needed based on your data. The ylim and max calls will need to be adjusted based on the maximum height of the data in your graph in order to fit everything into the axes and to make sure there is no overlap. You can tweak the 0.75 value to whatever you would like in order to show the differences adequately. This is probably not the most efficient method but the behavior is at least explicit.

Upvotes: 2

Related Questions