# How to correctly draw a violin-boxplot combination with gramm?

30 visualizzazioni (ultimi 30 giorni)
jostro il 4 Apr 2022
Commentato: Jaynik il 24 Gen 2024
Hi there,
I am trying to plot a violin plot while also drawing boxplots for data with a 2x2 design. My data is a cell array with 3 columns in a long format, where the first column contains my dependent variable (reaction times, continuous), the second column is factor 1 and the third column is factor 2. Basically like this:
%{
dummy_rt data
YVar fac1 fac2
val1 a c
val2 a c
val3 a d
. a d
. b c
. b c
. b d
. b d
%}
I use the following code to plot the violin plot with boxplots
figure;
g(1,1) = gramm('x', dummy_rt(:,2), 'y', dummy_rt(:,1), 'color', dummy_rt(:,3));
g(1,1).stat_violin('fill', 'transparent', 'dodge', 0.75, 'half', false);
g(1,1).stat_boxplot('width',0.2, 'notch', true, 'dodge', 0.75);
g(1,1).axe_property('Ylim', [750 3000]);
g(1,1).set_order_options('x', 0, 'color', -1);
g(1,1).set_color_options('map', 'brewer2');
g(1,1).set_title('Reaction Times for Pair Recognition (N = 48)', 'FontSize', 10);
g(1,1).set_names('x', 'Type of Pairing', 'y', 'Reaction time in ms', 'color', 'Response');
g.draw();
and get the following error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in gramm/stat_boxplot>my_boxplot (line 81)
outliersy=[outliersy ysel(sel_outlier)'];
Error in gramm/stat_boxplot>@(dobj,dd)my_boxplot(dobj,dd,p.Results) (line 24)
obj.geom=vertcat(obj.geom,{@(dobj,dd)my_boxplot(dobj,dd,p.Results)});
Error in gramm/draw (line 548)
obj.geom{geom_ind}(obj,draw_data);
The results look like this:
Curiously, I used the same code to successfully produce a plot before. The only thing I changed for the data in this example are the values of the dependent variable. It worked with accuracy values, but does not work with reaction times anymore. It looks like this:
I checked the arrays in both cases, and the format and dimensions are the same. Even the factor levels are the same. So something is not working with the boxplot and the 'color'argument.
I'd appreciate any help on this!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

Jaynik il 23 Gen 2024
Hi Jostro,
I created a dummy data based on the "dummy_rt" you provided and used the code provided. It worked and generated a "violin" plot. Following is the code used for data generation:
dummy_rt = cell(10, 3);
dummy_rt(:, 1) = num2cell(750 + (3000-750).*rand(10, 1)); % Values selected from gramm code
dummy_rt(1:5, 2) = {'a'};
dummy_rt(6:10, 2) = {'b'};
dummy_rt([1,2,5,6,7], 3) = {'c'};
dummy_rt([3,4,8,9,10], 3) = {'d'};
The error you are facing might be due to data problems. You can try these as troubleshooting steps:
1. Check that the data within "dummy_rt" is consistent in terms of size and format. Ensure that the first vector is numeric and the remaining vectors are categorical or cell arrays of strings with consistent dimensions across all rows.
2. If "dummy_rt" contains cell arrays of strings for the factors, consider converting them to categorical variables, which might be handled more consistently by the plotting functions.
3. Instead of changing values of the dependent variable, try simplifying your plot by removing the 'color' argument or by plotting only the violin or boxplot to see if one of these elements is causing the issue.
As a workaround, you could try creating the boxplot manually using MATLAB's built-in "boxplot" function and "violin" from file-exchange to see if the issue persists outside of "Gramm". Following is a sample code you can tinker around:
fac1_str = string(dummy_rt(:, 2));
fac2_str = string(dummy_rt(:, 3));
groups = fac1_str + "_" + fac2_str;
YVar = cell2mat(dummy_rt(:, 1));
figure;
violin(YVar, groups); % File Exchange function
hold on;
boxplot(YVar, groups, 'Widths', 0.2, 'Notch', 'on', 'Colors', 'k');
hold off;
ylim([750 3000]);
title('Reaction Times for Pair Recognition (N = 48)', 'FontSize', 10);
xlabel('Type of Pairing');
ylabel('Reaction time in ms');
You can check the following documentation to learn more about each function:
You can try this file exchange link to plot a "violinplot":
Hope this helps!
##### 2 CommentiMostra NessunoNascondi Nessuno
jostro il 23 Gen 2024
Hi Jaynik,
thanks so much for your reply! To be honest, I forgot about posting the question...
I just dug into my code from that time and actually found the problem: It had something to do with line 81 in the boxplot function (see also the error message that I posted). My past self did a good job documenting the solution, as it apparently started working flawlessly when I removed the ' after (sel_outlier), and has been working without problems since. However, I did not fully grasp why it produced the error for one array, but not for the other. Maybe it was something about the values of the outliers in that data that the boxplot function could not handle.
Jaynik il 24 Gen 2024
It is great to hear that you've found the solution! Outliers can sometimes cause issues with plotting.

Accedi per commentare.

### Categorie

Scopri di più su Box Plots in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by