I have 3 figures of which I would like to plot in the same place in R. I would like to have 2 columns, which would make the 3rd figure plotted alone in the second row. Using par(mfrow=c(2,2)) functions in R, is there a way to have the bottom figure plotted in the centre of the plot, as opposed to underneath the top figure?
I don't think you can do this using par(mfrow = ...)
However, you can use layout().
Try this:
par(mai=rep(0.5, 4))
layout(matrix(c(1,2,3,3), ncol = 2, byrow = TRUE))
plot(1:10)
plot(1:20)
plot(1:30)
So you can see the idea is to create a matrix where each cell indicates which graph to plot. You can extend the logic as follows:
par(mai=rep(0.5, 4))
layout(matrix(c(1,1, 2,2, 0, 3,3, 0), ncol = 4, byrow = TRUE))
plot(1:10)
plot(1:20)
plot(1:30)
Related
I would like to rotate the labelling of the y-labs to horizontal and can't find an answer without ggplot.
Is there a way to rotate them in plot.zoo?
The labels I mean are those ones called Series 1:5 and I have outlined them in red.
data <- xts(matrix(rnorm(1000), ncol = 5), order.by = as.Date(1:200))
plot.zoo(data)
Use las=1 like this:
plot.zoo(data, las = 1)
Update
The question later clarified that it was referring to the ylab. plot.zoo uses mtext for that and hard codes it; however, we could hack it using trace:
library(xts)
trace(plot.zoo,
quote(mtext <- function(...) graphics::mtext(..., cex = 0.7, las = 1)))
plot.zoo(data, oma = c(6, 5, 5, 0))
untrace(plot.zoo)
I am new to R
I want to create a layout using grid library in R as show below.
How can I do it?
My sample code:
library(grid)
library(gridBase)
grid.newpage()
ly = grid.layout(3, 3)
grid.show.layout(ly)
You can achieve this with grid.arrange() from the gridExtra-library.
They have a good vignette detailing the process here, but in essence you create a matrix detailing your layout, like this:
grid.arrange(
grobs = gl,
widths = c(2, 1, 1),
layout_matrix = rbind(c(1, 2, NA),
c(3, 3, 4))
)
This allows for great flexibility, but it can be a hassle to work out the exact matrix by hand.
Sorry this example is not entirely reproducible (I do not provide exact input data), but hopefully the example will be clear.
In short, I would like to save three maps in a wide pdf format, so All three maps can be shown with a desired extent + there is an overarching title above (but I don't want it to take up half of the page).
I am really struggling with setting it up properly:
pdf("plots1.pdf",width = 30/2.54,height = 20/2.54)
par(mfrow = c(2,3))
layout(matrix(c(1,1,1,2,3,4), 2, 3, byrow = TRUE))
plot.new()
text(0.5,0.5,"Africa, Params_1",cex=2,font=2)
# plot.new()
# plot.new()
plot(r2_list[[1]], xlim = Region[[g]][1:2], ylim = Region[[g]][3:4],
breaks=cuts, col = plasma(21), main = variable[1],legend=FALSE)
plot(wrld_simpl,add=TRUE)
plot(r2_list[[2]], xlim = Region[[g]][1:2], ylim = Region[[g]][3:4],
breaks=cuts, col = plasma(21), main = variable[2],legend=FALSE)
lines(wrld_simpl)
plot(r2_list[[3]], xlim = Region[[g]][1:2], ylim = Region[[g]][3:4],
breaks=cuts, col = plasma(21), main = variable[3])
lines(wrld_simpl)
dev.off()
I would also like to try to print 6 plots, again - with overarching titles but I fail miserably.
Help will be much appreciated.
Add heights= to your layout call.
layout(matrix(c(1,1,1, 2,3,4), byrow=TRUE, nr=2), heights = c(1, 8))
opar <- par(mar=c(0,0,0,0))
plot.new()
text(0.5,0.5,"Africa, Params_1",cex=2,font=2)
par(opar)
plot(1:20)
plot(3:99)
plot(1:2)
(Blue boundaries below added externally, for reference only.)
Without heights
If I remove the heights= from the code above, I see this:
With heights
(You will want to play with the actual values based on your canvas size, etc.)
I'm trying to add a legend to a plot which is the top plot of a two plot figure. However, the legend is getting massively stretched out so that nothing can be seen and it obscures the end of the plot. Here is the code to reproduce the problem:
cars = c(1, 3, 6, 4, 9)
par(mfrow = c(2, 1), mar=c(5,5,2,2))
plot(cars)
legend("topright", legend=c("label1", "label2"))
And here is what it looks like:
Is there some way to fix this?
When I plot several 3D images using plot3d from the rgl package, the images are displayed separately. I want to show them in one plot, as when using, e.g., par(mfrow=c(2, 2)) to display four 2D images in a single plot window.
Is this possible?
The command layout3d might be useful. Maybe this code can help:
shapes <- list(Tetrahedron = tetrahedron3d(), Cube = cube3d(), Octahedron = octahedron3d(),
Icosahedron = icosahedron3d(), Dodecahedron = dodecahedron3d(),
Cuboctahedron = cuboctahedron3d())
col <- rainbow(6)
open3d()
mat <- matrix(1:4, 2, 2)
mat <- rbind(mat, mat + 4, mat + 8)
layout3d(mat, height = rep(c(3, 1), 3), sharedMouse = TRUE)
for (i in 1:6) {
next3d()
plot3d(shapes[[i]], col = col[i])
next3d()
text3d(0, 0, 0, names(shapes)[i])
}
To deactivate the rotation of all the solids together, it is enough to put sharedMouse = FALSE.