Often you may want to plot a two-dimensional non-parametric distribution in R.

The easiest way to do so is by using the **plotMP()** function from the **gamlss.mx** package, which is designed to perform this exact task.

The **plotMP()** function uses the following basic syntax:

**plotMP(x, y, prob, theta = 20, phi = 20, expand = 0.5, col = “lightblue”, xlab = “intercept”, ylab = “slope”, …)**

where:

**x**: Vector containing points in the x-axis**y**: Vector containing points in the y-axis**prob**: Vector containing probabilities (should add to 1)**theta**,**phi**,**expand**,**col**: Arguments to pass to ‘persp’ function**xlab**: The x label of the plot**ylab**: The y label of the plot

The following example shows how to use the **plotMP()** function in practice.

**Note**: You may need to first install the **gamlss.mx** package before being able to use the **plotMP()** function. You can use the following syntax to do so:

**install.package('gamlss.mx')**

Once **gamlss.mx** is successfully installed, you can proceed to use the **plotMP()** function without encountering any errors.

**Example: How to Use the plotMP() Function in R**

First, we will create three vectors that contains the values for the x-axis, y-axis and probabilities that we will eventually plot:

#create three vectors x <- c(-5, -3, -2, 0, 1, 3, 5, 7, 8, 8) y <- c(3, -2, 4, 4, 3, 0, 5, -2, 7, 3) p <- c(.1, .2, .05, .1, .05, .05, .1, .5, .1, .2)

Now suppose that we would like to use the **plotMP()** function to plot these values.

We can use the following syntax to do so:

library(gamlss.mx) #plot values from vectors plotMP(x, y, p)

This produces the following plot:

The x-axis contains the values from the vector named **x**.

The y-axis contains the values from the vector named **y**.

The z-axis (the vertical axis) contains the values from the vector named **p**.

Note that the higher the value for the probability vector, the taller the vertical red line in the plot.

Note that the color used for the “floor” of the plot is light blue by default but you can use the **col** argument within the **plotMP()** function to use a different color instead.

For example, we could use the col argument to specify that we would like to use a purple color for the floor of the plot instead:

library(gamlss.mx) #plot values from vectors plotMP(x, y, p, col='purple')

This produces the following plot:

Notice that the floor of the plot is now shown in purple, just as we specified by using the **col** argument.

Note that the **expand** argument can also be used to control the overall height of the plot.

For example, the default value for **expand** is **0.5** but we can provide a value smaller than this to shorten the overall height of the plot:

library(gamlss.mx) #plot values from vectors plotMP(x, y, p, expand=0.2, col='purple')

This produces the following plot:

Notice that the overall height of the plot is much smaller since we specified a value of **0.2** for the **expand** argument.

Feel free to play around with the values for the different arguments in the **plotMP()** function to create the exact plot that you would like.

**Additional Resources**

The following tutorials explain how to perform other common operations in R:

How to Change Font Size in ggplot2

How to Rotate Axis Labels in ggplot2

How to Remove a Legend in ggplot2

How to Remove Axis Labels in ggplot2