Multiple plots into single plot in R

Multiple plots into single plot in R, You will learn how to use the par function to set or query graphical parameters in this R lesson.

Example 1: Multiple plots into single plot in R

I’ll show you how to create a graphic in R that has several plot windows in Example 1. The mfrow argument of the par function must be used for this task:

par(mfrow = c(2, 3))             

Now, we can create a single image with numerous plots:

plot(1:10)                        # 1st plot
plot(1:5)                         # 2nd plot
plot(10:1)                        # 3rd plot
plot(1)                           # 4th plot
plot(1:3)                         # 5th plot
plot(5:1)                         # 6th plot

The prior R code produced a multi-plot image, as seen in Figure 1. The mfrow argument’s value 2 indicated that a graphic with two rows should be drawn, and the value 3 indicated that a graphic with three columns should be drawn.

After you are done using all of your plots, I suggest returning the par choices to their default settings.

Otherwise, until you restart RStudio, the modified par choices will be retained. By utilizing the function, we can clear the par options:                        

Machine Learning Archives – Data Science Tutorials

Example 2: Alternating the amount of white space around the plot’s borders

In this example, we’ll demonstrate how to use the mar argument of the par function to alter the size of the region surrounding a plot.

For the mar parameter, we must supply a vector of four values. White space is designated by the first value as being below the plot, the second value as being to the left, the third value as being to the top, and the fourth value as being to the right.

par(mar = c(5, 5, 10, 20))        # Change white space

We can create whatever Base R plot we desire after running the par function with the previously specified parameters:

plot(1:10)                        # Draw plot

The preceding syntax produced a scatterplot with altered amounts of white space surrounding the graph, as seen in Figure 2.

How to make a connected scatter plot in R? »

Let’s once more reset the par settings to their default state so that we may move on to the following illustration:                         # Set par options back to default

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

fifteen − 2 =