6/30/2023 0 Comments R subplot parThe margins also have to be altered for the y label, the plots and the legend. The second and third parts will hold the plots. In the top row, the first part in the layout will hold the y label. The new layout should have 2 rows, but 3 columns.Some things to keep in mind while doing this: There are probably other ways to address this gap, but the solution that worked for me is creating a new layout to hold the common y axis label. You would think “Yes, that’s it! It’s done!”. Png ( "subplot8.png", height = 4, width = 8, units = "in", res = 300 ) layout ( matrix ( c ( 1, 2, 3, 3 ), ncol = 2, byrow = TRUE ), heights = c ( 0.9, 0.1 )) plot ( x, y, xlab = "X1", ylab = "Y", col = c1, pch = 16 ) plot ( x, y, xlab = "X2", ylab = "", yaxt = "none", col = c2, pch = 16 ) par ( mar = c ( 0, 0, 0, 0 )) # Make the margins 0 plot ( 1, type = "n", axes = FALSE, xlab = "", ylab = "" ) # Create empty plot legend ( "top", c ( "Blue Points", "Orange Points" ), horiz = TRUE, pch = c ( 16, 16 ), col = c ( c1, c2 )) dev.off () Then it makes sense to remove the y axis tick marks and the associated tick labels, we do this with the following snippet: Now suppose that the two plots share the same units on the y axis and also similar ranges of the data. The above command creates an empty plot, and the legend function can be called after this. Creating a legend is necessarily preceded by creating a plot in the first place. plot(1, type = "n", axes=FALSE, xlab="", ylab="") – This creates an empty plot.par(mar=c(0,0,0,0)) – The reason for the first part is that by default, the plot margins tend to be quite big.Png ( "subplot7.png", height = 4, width = 8, units = "in", res = 300 ) layout ( matrix ( c ( 1, 2, 3, 3 ), ncol = 2, byrow = TRUE ), heights = c ( 0.9, 0.1 )) plot ( x, y, xlab = "X1", ylab = "Y1", col = c1, pch = 16 ) plot ( x, y, xlab = "X2", ylab = "Y2", col = c2, pch = 16 ) par ( mar = c ( 0, 0, 0, 0 )) # Make the margins 0 plot ( 1, type = "n", axes = F, xlab = "", ylab = "" ) # Create empty plot legend ( "top", c ( "Blue Points", "Orange Points" ), horiz = TRUE, pch = c ( 16, 16 ), col = c ( c1, c2 )) dev.off ()
0 Comments
Leave a Reply. |