Monday, December 5, 2022
HomeData ScienceCreate Customized Layouts in Your R Plots | by Dick Brown |...

Create Customized Layouts in Your R Plots | by Dick Brown | Nov, 2022


Photograph by Hal Gatewood on Unsplash

When plotting information, we’re typically making an attempt to create a single plot. However what do you do if you wish to create a single presentation of a set of associated plots? There are lots of methods of doing this in R. On this article, I’ll present you three of them, utilizing examples I’ve created in a few of my #TidyTuesday submissions. Full code may be discovered at my github pages.

Picture by writer

Probably the most primary strategies for making a multi-plot is utilizing the par() operate with the “mfrow =” attribute. On this mission, we had measurement information for six various kinds of fruits. I made a decision to create histograms for every of them, however I needed to place them multi functional chart.

My first try is proven above. That is completely made with base R graphics. The plots are easy hist() features and the annotations have been created with strains() and textual content() features.

The important thing to creating the multi-plot is by operating the next code chunk earlier than creating any plots:

par(mfrow = c(2,3))

This divides the display screen into two rows and three columns, and every plot will populate the display screen sequentially, row by row. If you wish to return to single-plot, simply take care to reset on the finish by operating the operate once more:

par(mfrow = c(1,1))

which will get us again to 1 row and one column.

The total code for this plot is at https://github.com/dikbrown/TTGiantPumpkins/blob/predominant/EDA.R

Picture by writer

For this #TidyTuesday submission, we have been simply requested to do some mapping. Since WalletHub had only recently revealed a narrative on range inside america, I made a decision to take a look at what I may do with the info.

The dataset included rankings on range in six predominant classes, together with an total range rating. I made a decision to create a map for every of those rankings, with the “total” range rating occupying a spot of priority on the prime.

Together with the ggplot2 and usmap libraries, I discovered the grid and gridExtra libraries useful right here. These libraries provide the capacity to separate up the display screen into separate home windows by making a matrix.

structure <- rbind(c(1, 2, 2, 2, 2, 3),
c(1, 2, 2, 2, 2, 3),
c(4, 4, 5, 5, 6, 6),
c(7, 7, 8, 8, 9, 9))

For the reason that matrix simply created has 4 rows and 6 columns, the display screen is split up in the identical approach:

Picture by writer

Nevertheless, the numbers that we use permit us to outline which sectors are going to be a part of every part of the display screen. As an illustration, all “1” sectors will mix into one part of the display screen, the place we are able to put one thing. This may create the next structure:

Picture by writer

Now that now we have the structure, we are able to put issues in it with the grid.organize() operate. We are able to solely put in sure forms of objects, nonetheless, equivalent to graphical objects (referred to as grobs) or ggplot objects. If we wish to put solely textual content in considered one of these sections, we do this with the textGrob() operate. The strains within the picture above have been created with the rectGrob() operate.

textobj1 <- textGrob("That is some textual content.", gp = gpar(fontsize = 24), hjust = 1)
...
create some plots
...
grid.organize(textobj1, plot1, textobj2,
plot2, plot3, plot4, plot5, plot6, plot7,
layout_matrix = structure)

The completely different objects that we’ve added will go into the sections above within the order indicated within the grid.organize() operate. So, textobj1 goes into part 1, plot1 goes into part 2, and so on.

All of my code to create the map initially of this part, together with the structure photographs, may be discovered on my GitHub web page at https://github.com/dikbrown/TTMappingDiversity21

You may get an summary of the grid package deal at https://bookdown.org/rdpeng/RProgDA/the-grid-package.html and discover the documentation at https://stat.ethz.ch/R-manual/R-devel/library/grid/html/00Index.html

Picture by writer

The cut up.display screen() operate is a part of the bottom graphics package deal in R. It’s used to separate the display screen into completely different areas. Moreover, every area may be additional cut up into nonetheless smaller areas (though that’s past the scope of this doc). This capacity permits for rather more complexity than the opposite strategies talked about beforehand. Additionally it is simpler to fine-tune than both of the opposite strategies. I used this methodology to redo the Big Pumpkins picture that I confirmed earlier.

To start out out, we create a structure, in the same method to the structure from the grid graphics. Nevertheless, as an alternative of merely labeling the completely different areas, we use the structure matrix to outline the boundaries of every area. The matrix will consist of 4 columns. These columns signify x1 (left edge), x2 (proper edge), y1 (backside edge), and y2 (prime edge). The matrix will then have one row for every part that you just wish to create. Listed here are some examples:

Easiest structure, with just one part:

structure <- matrix(c(0, 1, 0, 1),          # x1=0, x2=1, y1=0, y2=1
ncol = 4, byrow = TRUE)
picture by writer

Be aware that numbering begins with (0, 0) as the underside left, as you’ll count on from the Cartesian coordinate system, with the highest proper having coordinates of (1, 1).

Now let’s say you wish to cut up the display screen into two side-by-side halves. You might use:

structure <- matrix(c(0, 0.5, 0, 1,          # x1=0, x2=0.5, y1=0, y2=1
0.5, 1, 0, 1), # x1=0.5, x2=1, y1=0, y2=1
ncol = 4, byrow = TRUE)
picture by writer

Conversely, if you wish to cut up the display screen into two above-and-below halves, you could possibly use this structure:

structure <- matrix(c(0, 1, 0.5, 1,         # x1=0, x2=1, y1=0.5, y2=1
0, 1, 0, 0.5), # x1=0, x2=1, y1=0, y2=0.5
ncol = 4, byrow = TRUE)
picture by writer

You need to understand that the ordering of the rows is just not fastened, however it is very important preserve observe of how they’re ordered. As an illustration, the next structure can be qualitatively equal to the primary (i.e., prime and backside halves):

structure <- matrix(c(0, 1, 0, 0.5,
0, 1, 0.5, 1),
ncol = 4, byrow = TRUE)

The one distinction right here is that within the first model, the highest half is part 1 and the underside half is part 2, whereas within the second model, it’s the alternative. This turn into vital later once you wish to put one thing in a selected part. Because of this, I extremely suggest labeling the rows with feedback:

structure <- matrix(c(0, 1, 0.5, 1,          # prime half
0, 1, 0, 0.5), # backside half
ncol = 4, byrow = TRUE)

or

structure <- matrix(c(0, 1, 0, 0.5,        # backside half
0, 1, 0.5, 1), # prime half
ncol = 4, byrow = TRUE)

Let’s begin getting a bit fancier. As a substitute of equivalent halves, let’s make the highest part larger (2/3) than the underside part (1/3)

structure <- matrix(c(0, 1, 0.33, 1,        # prime part
0, 1, 0, 0.33), # backside part
ncol = 4, byrow = TRUE)

We are able to additionally cut up the display screen in half each vertically and horizontally:

structure <- matrix(c(0,   0.5, 0.5, 1,        # top-left part
0.5, 1, 0.5, 1, # top-right part
0, 0.5, 0, 0.5, # bottom-left part
0.5, 1, 0.5, 1), # bottom-right part
ncol = 4, byrow = TRUE)
picture by writer

To create the plot proven initially of this part, I made a decision that I needed eight sections: six sections for the six plots together with prime and backside strips for additional textual content. The title and subtitle go within the prime strip, whereas the quotation data (information supply and writer data) goes within the backside strip. On this method, I very merely obtained full management over the formatting of the textual content parts. Right here’s the structure I used:

structure <- matrix(c(0,    1,    0.9,  1,      #prime strip for title
0, 0.33, 0.47, 0.9, # row 1, plot 1
0.33, 0.67, 0.47, 0.9, # row 1, plot 2
0.67, 1, 0.47, 0.9, # row 1, plot 3
0, 0.33, 0.06, 0.47, # row 2, plot 1
0.33, 0.67, 0.06, 0.47, # row 2, plot 2
0.67, 1, 0.06, 0.47, # row 2, plot 3
0, 1, 0, 0.04), # backside strip for quotation
ncol = 4, byrow = TRUE)

To summarize, the highest strip represents the highest 10% of the display screen and the underside strip represents the underside 4% of the display screen. The remaining 86% of the peak was cut up in half (43%), which is how I got here up with the 0.47 values (0.04 + 0.43). I then gave every plot 1/3 of the horizontal house (therefore 0.33 and 0.67 because the dividing factors).

Now, to fill these sections, it’s important to add some code to inform the system easy methods to use the structure matrix and to point the place your output (plot or textual content) will go.

# inform the system to make use of the structure matrix to separate the display screen
cut up.display screen(structure)

# display screen(#) tells the system to place the upcoming output in part #
display screen(1)
textual content("Total title of plot", formatting parameters)

display screen(2) # use part 2: row 1, plot 1 from structure
hist(information, parameters) # add a histogram to part 2

...

display screen(8)
textual content("Different textual content to be added", parameters)

# The following step is essential, or you should have
# points making an attempt to re-run the code

shut.display screen(all = TRUE)

The total code to create the above plot is at https://github.com/dikbrown/TTGiantPumpkins/blob/predominant/Distribution.R

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments