主要内容

raincloudplot

Rain cloud plot

Since R2026a

  • Rain cloud plot

Description

Vector and Matrix Data

raincloudplot(ydata) creates a rain cloud plot for each column of the matrix ydata. If ydata is a vector, then raincloudplot creates a single rain cloud plot.

example

raincloudplot(xgroupdata,ydata) groups the data in ydata according to the unique values in xgroupdata and plots each group of data as a separate rain cloud plot. xgroupdata determines the position of each rain cloud plot along the y-axis.

example

Table Data

raincloudplot(tbl,yvar) creates a rain cloud plot for each variable in yvar from the table tbl.

example

raincloudplot(tbl,xvar,yvar) creates a rain cloud plot of the data in yvar grouped by the data in xvar, where xvar and yvar are variables from the table tbl. To plot one data set, specify one variable for xvar and one variable for yvar. To plot multiple data sets, specify multiple variables for xvar, yvar, or both. If both arguments specify multiple variables, they must specify the same number of variables.

example

Additional Options

raincloudplot(___,Name=Value) sets plot properties using one or more name-value arguments in addition to any of the input argument combinations in the previous syntaxes. For example, you can specify the plot orientation and marker symbol. For a list of properties, see RainCloudPlot Properties.

raincloudplot(ax,___) plots into the axes specified by ax instead of into the current axes (gca). The argument ax can precede any of the input argument combinations in the previous syntaxes.

example

r = raincloudplot(___) returns a RainCloudPlot object or a vector of RainCloudPlot objects. Use r to set properties of the rain cloud plots after creating them. For a list of properties, see RainCloudPlot Properties.

example

Examples

collapse all

Create a single rain cloud plot from a vector of patient ages. Use the rain cloud plot to analyze the distribution of ages.

Load the patients data set. The Age vector contains the ages of 100 patients. Create a rain cloud plot to visualize the distribution of ages.

load patients
raincloudplot(Age)
xlabel("Age (years)")

Figure contains an axes object. The axes object with xlabel Age (years) contains an object of type raincloudplot.

The upper half of the rain cloud plot is a violin plot whose outline is determined by the kernel density estimate (kde) for the probability density function. The kde is bell-shaped with a peak near 40 on the vertical axis. The plot shows that the data in Age is approximately normally distributed and the median patient age is around 40. The lower half of the rain cloud plot is a swarm chart, which is a scatter plot with the points offset in the y-direction.

Generate a matrix of normally distributed random numbers. Create a rain cloud plot for the data in each column of the matrix.

ydata = randn(100,3);
raincloudplot(ydata)

Figure contains an axes object. The axes object contains 3 objects of type raincloudplot.

The three rain cloud plots have group labels 1, 2, and 3. The shapes of the rain cloud plots are slightly different due to randomness in the data. However, each rain cloud plot has the bell-shape characteristic of a normal distribution.

Generate a vector of normally distributed random numbers and a vector of grouping data.

ydata = randn(100,1);
xgroupdata = categorical(repelem(["group1";"group2";"group3"],[20,50,30]));

Create a set of rain cloud plots for the data in ydata grouped by the unique values in xgroupdata. Each rain cloud plot in the figure corresponds to a unique value in xgroupdata.

raincloudplot(xgroupdata,ydata)

Figure contains an axes object. The axes object contains an object of type raincloudplot.

Load the tsunami data set into a table. Create rain cloud plots for the Longitude and Latitude variables.

tbl = readtable("tsunamis.xlsx");
raincloudplot(tbl,["Longitude","Latitude"])

Figure contains an axes object. The axes object contains 2 objects of type raincloudplot.

Generate two vectors of normally distributed data and three vectors of grouping data.

ydata1 = randn(100,1);
ydata2 = randn(100,1)+5;
xgroupdata1 = categorical(repelem(["group1";"group2"],[90,10]));
xgroupdata2 = categorical(repelem(["group1";"group2"],[10,90]));
xgroupdata3 = categorical(repelem(["group3";"group4"],[25,75]));

The variables ydata1 and ydata2 contain normally distributed data with means of 0 and 5, respectively. xgroupdata1 and xgroupdata2 contain different sequences of the same group labels. xgroupdata3 contains different group labels from those in xgroupdata1 and xgroupdata2.

Create a table from the sample data and grouping data.

tbl = table(xgroupdata1,xgroupdata2,xgroupdata3, ...
    ydata1,ydata2,VariableNames=["X1","X2","X3","Y1","Y2"]);

Create a set of overlaid rain cloud plots using the data in Y1 grouped by X1 and X2.

raincloudplot(tbl,["X1","X2"],"Y1")

Figure contains an axes object. The axes object with xlabel Y1 contains 2 objects of type raincloudplot.

The figure shows two sets of overlaid rain cloud plots. The blue rain cloud plots represent the data in Y1 grouped by X1, and the orange rain cloud plots represent the same data grouped by X2. The overlaid plots show that the different groupings do not significantly change the median values for each group. However, the different groupings have a visible effect on the shape of the rain cloud plots corresponding to each group. This result suggests that the distribution of the data in each group is affected by how the data is grouped.

Create another set of rain cloud plots using the data in Y1 grouped by X1 and the data in Y2 grouped by X3.

raincloudplot(tbl,["X1","X3"],["Y1","Y2"])

Figure contains an axes object. The axes object contains 2 objects of type raincloudplot.

The blue plots corresponding to groups 1 and 2 represent the data in Y1, and the orange plots corresponding to groups 3 and 4 represent the data in Y2. The plots show that the median values for the data in groups 3 and 4 are larger than those for groups 1 and 2.

Create rain cloud plots from patient data to compare the blood pressure of smokers and nonsmokers. The plots show that smokers have higher average systolic and diastolic blood pressure than nonsmokers.

Load the patients data set.

load patients

Create a 1-by-2 tiled chart layout using the tiledlayout function. Create the first set of axes ax1 by calling the nexttile function. In the first set of axes, display two rain cloud plots representing the systolic blood pressure values, one for smokers and the other for nonsmokers. Create the second set of axes ax2 within the tiled chart layout by calling the nexttile function. In the second set of axes, do the same for diastolic blood pressure.

tiledlayout(1,2)

% Left axes
ax1 = nexttile;
raincloudplot(ax1,categorical(Smoker),Systolic)
xlabel(ax1,"Systolic Blood Pressure")
ylabel(ax1,"Smoker")

% Right axes
ax2 = nexttile;
raincloudplot(ax2,categorical(Smoker),Diastolic)
xlabel("Diastolic Blood Pressure")
ylabel(ax2,"Smoker")

Figure contains 2 axes objects. Axes object 1 with xlabel Systolic Blood Pressure, ylabel Smoker contains an object of type raincloudplot. Axes object 2 with xlabel Diastolic Blood Pressure, ylabel Smoker contains an object of type raincloudplot.

Load the patients data set.

load patients

The variables Diastolic and Smoker contain data for patient diastolic blood pressure and smoker status.

Create two vectors containing the diastolic blood pressure data for smokers and nonsmokers, respectively.

diastolicSmoker=Diastolic(Smoker==1);
diastolicNonSmoker=Diastolic(Smoker==0);

Create a set of rain cloud plots using the diastolic blood pressure data for smokers and nonsmokers.

figure
hold on
r1 = raincloudplot(diastolicSmoker);
r2 = raincloudplot(diastolicNonSmoker);

You can modify the rain cloud plots by specifying the properties of the RainCloudPlot objects in r1 and r2. For more information, see RainCloudPlot Properties.

Update the colors of the raincloud plots so that the plot for smokers is green and the plot for nonsmokers is magenta.

r1.FaceColor="g";
r2.FaceColor="m";
legend("Smoker","Nonsmoker")

Input Arguments

collapse all

Sample data, specified as a numeric vector or matrix.

  • If ydata is a matrix, then raincloudplot creates a rain cloud plot for each column of ydata.

  • If ydata is a vector and you do not specify xgroupdata, then raincloudplot creates a single rain cloud plot.

  • If ydata is a vector and you specify xgroupdata, then raincloudplot creates a rain cloud plot for each unique value in xgroupdata.

Data Types: single | double

Positional grouping data, specified as a numeric or categorical vector, or a numeric or categorical matrix.

  • If ydata is a vector of length n, then xgroupdata must be a vector of length n or a matrix with n rows.

  • If ydata is a matrix, then xgroupdata must be a matrix of the same size or a vector of length equal to the number of rows or columns in ydata.

raincloudplot groups the data in ydata according to the unique values in xgroupdata and creates a rain cloud plot for each group. By default, raincloudplot horizontally orients the rain cloud plots and displays the xgroupdata values along the y-axis. You can change the rain cloud plot orientation by using Orientation.

Data Types: single | double | categorical

Source table containing the data to plot, specified as a table or timetable.

Table variables containing the sample data, specified as one or more table variable indices. The table variables you specify must contain numeric values.

Specifying Table Indices

Use any of the following indexing schemes to specify the intended variable or variables.

Indexing SchemeExamples

Variable names:

  • A string array, character vector, or cell array.

  • A pattern object.

  • "A" or 'A' — A variable named A

  • ["A","B"] or {'A','B'} — Two variables named A and B

  • "Var"+digitsPattern(1) — Variables named "Var" followed by a single digit

Variable indices:

  • An index number that refers to the location of a variable in the table.

  • A vector of numbers.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [2 3] — The second and third variables from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects variables of a specified type.

  • vartype("categorical") — All the variables containing categorical values

Plotting Your Data

To plot one set of rain cloud plots, specify one variable for xvar and one variable for yvar. For example, create a table with two categorical grouping variables and one variable containing normally distributed random values. Plot the data in Y grouped by the data in X1.

X1 = categorical(repelem(["a1";"b1"],50));
X2 = categorical(repelem(["a2";"b2";"c2"],[33,33,34]));
Y = randn(100,1);
tbl = table(X1,X2,Y);

raincloudplot(tbl,"X1","Y")

To plot multiple data sets together, specify multiple variables for xvar, yvar, or both. If you specify multiple variables for both arguments, the number of variables for each argument must be the same.

For example, create a set of rain cloud plots from the data in Y using X1 as the grouping variable, and a set using X2 as the grouping variable.

raincloudplot(tbl,["X1","X2"],"Y")

You can also use different indexing schemes for xvar and yvar. For example, specify xvar as a variable name and yvar as an index number.

raincloudplot(tbl,"X1",3)

Table variables containing the positional grouping data, specified as one or more table variable indices. The table variables you specify must contain numeric or categorical values.

Specifying Table Indices

Use any of the following indexing schemes to specify the intended variable or variables.

Indexing SchemeExamples

Variable names:

  • A string array, character vector, or cell array.

  • A pattern object.

  • "A" or 'A' — A variable named A

  • ["A","B"] or {'A','B'} — Two variables named A and B

  • "Var"+digitsPattern(1) — Variables named "Var" followed by a single digit

Variable indices:

  • An index number that refers to the location of a variable in the table.

  • A vector of numbers.

  • A logical vector. Typically, this vector is the same length as the number of variables, but you can omit trailing 0 or false values.

  • 3 — The third variable from the table

  • [2 3] — The second and third variables from the table

  • [false false true] — The third variable

Variable type:

  • A vartype subscript that selects variables of a specified type.

  • vartype("categorical") — All the variables containing categorical values

Plotting Your Data

To plot one set of rain cloud plots, specify one variable for xvar and one variable for yvar. For example, create a table with one categorical grouping variable and two variables containing normally distributed random values. Plot the data in Y1 grouped by the data in X.

X = categorical(repelem(["a1";"b1"],50));
Y1 = randn(100,1);
Y2 = 5*randn(100,1)+10;
tbl = table(X,Y1,Y2);

raincloudplot(tbl,"X","Y1")

To plot multiple data sets together, specify multiple variables for xvar, yvar, or both. If you specify multiple variables for both arguments, the number of variables for each argument must be the same.

For example, create a set of rain cloud plots from the data in Y1 using X as the grouping variable, and a set using Y2 as the data.

raincloudplot(tbl,"X",["Y1","Y2"])

You can also use different indexing schemes for xvar and yvar. For example, specify xvar as a variable name and yvar as an index number.

raincloudplot(tbl,"X",3)

Target axes, specified as an Axes object. If you do not specify the axes, then raincloudplot uses the current axes (gca).

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: raincloudplot(ydata,Orientation="vertical",MarkerEdgeColor="g",MarkerFaceColor="g") uses the data in ydata to create a vertical rain cloud plot with green markers.

Note

The RainCloudPlot properties listed here are only a subset. For a complete list, see RainCloudPlot Properties.

Maximum rain cloud plot width, specified as a positive scalar. DensityWidth has the same units as the positional grouping data specified by xgroupdata or xvar.

Example: DensityWidth=0.5

Data Types: single | double

Orientation of the rain cloud plots, specified as "horizontal" or "vertical". By default, the rain cloud plots are horizontally oriented so that the rain cloud plots align with the x-axis. Regardless of the orientation, raincloudplot stores the sample data in the YData property of the RainCloudPlot object.

Example: Orientation="vertical"

Data Types: string | char

Output Arguments

collapse all

Rain cloud plots, returned as a RainCloudPlot object or a vector of RainCloudPlot objects.

  • If you specify ydata as a matrix, raincloudplot returns one object per column.

  • If you specify tbl, raincloudplot returns one object per index in xvar or yvar, whichever has the most elements.

Use r to set properties of the rain cloud plots after creating them. For a list of properties, see RainCloudPlot Properties.

More About

collapse all

Version History

Introduced in R2026a