Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="versicolor"), xlab=" ", ylab=" ", xlim=c(min.width,max.width), ylim=c(min.length,max.length+0.15), bty="n") Text(()/2 + min.width, max.length+0.15, expression(italic("Iris virginica"))) Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="virginica"), xlab=" ", ylab=" ", I have also labeled each panel by species, which required some small adjustments to the y-axis scale. The mtext function allows you to place text in the outer margin. The omi argument to the par function specifies the outer margins (in inches) with the sides listed in the same order as for mai. We just need to add a little extra space around the outer margin to place our axis labels. The panels are now well placed within the figure. Xlim=c(min.width,max.width), ylim=c(min.length,max.length), yaxt="n") Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="setosa"), xlab=" ", ylab=" ", Xlim=c(min.width,max.width), ylim=c(min.length,max.length)) Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="versicolor"), xlab=" ", ylab=" ", Xlab=" ", ylab=" ", xlim=c(min.width,max.width), ylim=c(min.length,max.length), xaxt="n") In our example, the sum is 0.4 for both, but bottom+top does not need to equal left+right. If you want all of your panels to have plots that fill the same area, then the bottom+top and left+right margins need to be the same in all panels. Again, you simply use trial-and-error to get the margins how you want them, but there is one little catch. In the bottom left panel, we want larger margins on the bottom and left because we need to include the axis scales on those sides. In the top left panel, we want a smaller bottom margin because we are going to drop the numbers from the x-axis scale. The mai argument specifies the margin by sides, i.e., c(bottom, left, top, right). Instead of including only a single call to the par function to change the mai argument, we will call par before plotting each panel to customize the size of the margin on each side of the plot for each panel. But we want to use as much white space as possible for plotting. When we drop the numbers from those panels, it will create more white space. Not bad, but we still have redundancy by including numbers on the x-axis scale in the top left panel and the y-axis scale in the bottom right panel. Xlab=" ", ylab=" ", xlim=c(min.width,max.width), ylim=c(min.length,max.length)) We can start to reduce the redundancy in the figure by removing the labels for the x- and y-axes. It takes a little trial-and-error to hit on margins that produce the desired spacing. We can use the mai argument to the par function to specify the margin (in inches) of each panel in the figure. Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="setosa"), Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="versicolor"), Xlab="Sepal Width", ylab="Sepal Length", xlim=c(min.width,max.width), Plot(Sepal.Length~Sepal.Width, data=iris, subset=(Species="virginica"), The empty panel can be placed in any of the 4 positions, but it is redundant to use plot.new for the bottom right panel because mfrow fills the graphics device by row, moving left to right along the row. An empty panel is created by calling plot.new. The next block of code plots 3 panels in a 2x2 arrangement 1 with mostly default options. To plot all the data on the same scale, we need to extract the max and min values of the variables that we are plotting. In this case, the data would be more effectively plotted in a single panel with different colors or symbols for each species, but with larger data sets the different colors/symbols can create a jumbled mess and multi-panel figures illustrate patterns in the data more clearly. The data set includes flower measurements for 3 iris species. Below I will walk through how to adjust the spacing of the panels when using mfrow.įor this example, we will use Edgar Anderson’s iris data, which is distributed with R. The figures in that post were ugly because they used the default panel spacing associated with the mfrow argument of the par function. In my testing I found that even if only a single plot obj was added to the subplot() function, the plot would be blank, which is why I constructed the REPREX in the above manner.In a previous post, I showed how to keep text and symbols at the same size across figures that have different numbers of panels. When you un-comment out the anchor = "free" argument the entire plot disappears, regardless of where position is set.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |