Violinplot-Matlab icon indicating copy to clipboard operation
Violinplot-Matlab copied to clipboard

Two half violinplot

Open jon6295 opened this issue 3 years ago • 6 comments

Hi

A short question. Would it be possible to

Would it be possible to tell us how you have managed to do the Italy vs Italy 1.5 of the second example. When I try to produce a violin plot which have different distributions in each half I can not make it so that they are separated.

Thanks!

jon6295 avatar Jun 16 '22 11:06 jon6295

Ofc! I just called directly to the violin function, skipping the violinplot. Here goes an example of the code:

n= 4; % The original position, in this case 4 was Italy

vsl = Violin({MPG(strcmp(Origin, grouporder{n}))},...         %Your data
    n-0.05,...                                                                          % 0.05 is the offset
    'HalfViolin','left',...                                                           % just left side violin plot
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true,...
    'ShowMedian', true);                                               

% Same for the right violin
vsr = Violin({MPG(strcmp(Origin, grouporder{n}))*1.15},...
    n+0.05,... % Note here that I add the offset
    'HalfViolin','right',...
    'QuartileStyle','shadow',...
    'DataStyle', 'scatter',...
    'ShowNotches', false,...
    'ShowMean', true);

mikelgg93 avatar Jun 16 '22 12:06 mikelgg93

Hello,

When I try to replicate the above example, I get the following error:

Index in position 1 is invalid. Array indices must be positive integers or logical values.

Error in Violin (line 375)
                    ViolinColor{1} = args.ViolinColor{1}(pos,:);

Am I missing something?

Best,

Michael

GrimmSnark avatar Jul 29 '22 09:07 GrimmSnark

Michael

Hi Michael,

I was having the same issue. I fixed it. Replace the following 2 lines in Violin.m code:

Original line in code: ViolinColor{1} = args.ViolinColor{1}(pos,:); Replace this line with: ViolinColor{1} = args.ViolinColor{1}(round(pos),:)

Also, right below it is another instance that you need to replace. Original line in code: ViolinColor{2} = args.ViolinColor{2}(pos,:) Replace this line with: ViolinColor{2} = args.ViolinColor{2}(round(pos),:)

These are around Line# 373 in the code. I hope this helps!

Thanks, Vivek

atromist avatar Sep 13 '22 12:09 atromist

Thank you so much for the suggestion!

GrimmSnark avatar Sep 15 '22 13:09 GrimmSnark

@mikelgg93, are you able to explain how one can get the text instead of values, as is shown in the original picture?

Screen Shot 2022-12-12 at 3 53 42 PM

Using your example code, you get the position that you are explicitly putting down as n (which is 4). Using Violin (not violinplot), I cannot figure out how to use category labels as names for the individual violins and then allow for an offset. Violin's code explicitly requires scalar number inputs for each violin's 'ID'.

Screen Shot 2022-12-12 at 3 53 20 PM

EitNickS avatar Dec 12 '22 21:12 EitNickS

Hi! I might have used violinplot if I recall it properly due to its flexibility. Unfortunately, I no longer have access to Matlab to test it.

For the labels, you can define xticks and xticklabels in Matlab posthoc

mikelgg93 avatar Dec 13 '22 08:12 mikelgg93