2 boxplots overlayed - how to
3 ビュー (過去 30 日間)
I have two boxplots created from information that the code is reading from different tables named "dataraw_BPM_XXXX".
Each boxplot is reading information from two columns ('subject ID' and 'Value') of one of the tables.
Code below for reference:
boxplotMeanRelaxing_SubjectID = dataraw_BPM_RELAXED.SUBJECT_ID
boxplotMeanRelaxing_Value = dataraw_BPM_RELAXED.VALUE
title('BPM readings per subject condition A')
ylabel('BPM readings during condition A')
boxplotMeanAROUSING_SubjectID = dataraw_BPM_AROUSED.SUBJECT_ID
boxplotMeanAROUSING_Value = dataraw_BPM_AROUSED.VALUE
title('BPM readings per subject condition B')
ylabel('BPM readings during condition B')
I'd like to overlay the two boxplots in a way that allows me to make comparisons between data obtained for conditions A and B. (a side by side option would be great - I've looked at some examples in the forum but unfortunately they were not what I need)
So far I only managed to overlay them and create a mess of overlayed boxes unfortunately.
Anyone knows how to help please?
Thanks very much.
Walter Roberson 2022 年 11 月 2 日
You can use hold on and then boxplot() again.
The second boxplot may use the same colors as the first, which is probably going to be confusing. However there is a 'colors' option that can be used to change the box colors. There is also a 'GroupColor' option to allow the colors to be controlled per group.
See also help boxplot which contains the useful hint:
To modify the properties of box components, use findobj using tags to
find their handles as in one of the examples below. The tag names
depend on the plotstyle and are:
all styles: 'Box', 'Outliers'
traditional: 'Median', 'Upper Whisker', 'Lower Whisker',
'Upper Adjacent Value', 'Lower Adjacent Value',
compact: 'Whisker', 'MedianOuter', 'MedianInner'
when 'notch' is 'marker':
To make it easier to distinguish which handle is from which boxplot(), you can use an additional trick that is not documented in help or doc but which is present in the source code. If you record the output of boxplot,
h = boxplot(boxplotMeanAROUSING_Value ,boxplotMeanAROUSING_SubjectID)
then you will get back what appears to be a numeric array. However if you then handle(h) then the numeric array will turn into an array of line() objects, one column per group. The source code describes the exact order of the handles, but I would suggest that you instead use that array of handles as the starting point for the findobj() described in help boxplot instead of counting on the order remaining the same.