On Thu, 9 Jun 2022, J. Marius RAKOTONDRAMANGA wrote:
Hi Ranjeet,
Have a look to this function cowplot::plot_grid(), from
Marius,
Thanks for trying to help, but cowplot is designed to combine plots
created using grid, not base, graphics. To use it (or gridExtra), sf
plots, which are base graphics, must be converted to grid objects (grobs),
for example using the gridGraphics package.
Since Ranjeet did not provide a reproducible example, this may not
correspond well, but given the lack of information, it's hard to be more
precise:
library(sf)
nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"))
plot(nc["BIR74"])
creates output on the default device, but no object that can be
manipulated. Using:
par(mfrow=c(2,2))
plot(nc["BIR74"])
gives exactly the same output, because sf's plot() itself takes control of
the graphics device, so ignoring the request for multiple panels.
par(mfrow=c(1,1))
plot(nc["BIR74"])
gridGraphics::grid.echo()
library(grid)
g <- grid.grab()
grid.newpage()
gridExtra::grid.arrange(g, b, ncol=2)
gives two copies of our initial output after conversion to grid graphics.
However, it is easier to choose alternatives:
library(tmap)
tm_shape(nc) + tm_fill(c("BIR74", "BIR79"))
and probably library(mapsf) too, but it, like sf, uses base graphics; it
respects par(mfrow=):
library(mapsf)
par(mfrow=c(1,2))
mf_map(x=nc, var=c("BIR74"), type="choro")
mf_map(x=nc, var=c("BIR79"), type="choro")
Hope this clarifies,
Roger
It has mainy features and permit you to save also the combined plots :
here
Regards,
Marius,
---------
J. Marius RAKOTONDRAMANGA, MSc
PhD Student in Biostatistics/Biomathematics
ED 393 Pierre Louis de Sant? Publique : Epid?miologie et Sciences de
l'Information Biom?dicale
Sorbonne Universit? (ex. UPMC)
Le jeu. 9 juin 2022 ? 08:29, Ranjeet Kumar Jha <
ranjeetjhaiitkgp at gmail.com>
Hi,
I like to plot all my 5 spatial maps of drought indices in a single
using the sf plot function in R. I am able to plot all maps separately
unable to plot altogether. All indexes are showing drought indices.
I really appreciate your help!
Here is the code:
#merging using merge function
merge_drought_indices <- merge(shp_dist, drought_indices)
view(merge_drought_indices)
#plot in a single window
library(sf)
par(mfrow=c(2,2))
plot(merge_drought_indices["r_count_lt_p05_harvest_end"],
pal = colorRampPalette(c("red", "white", "blue","green")),
main = "End harvest index variation in India during drought year 2000",
key.pos = 4, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)
plot(merge_drought_indices["r_count_lt_p05_harvest"],
pal = colorRampPalette(c("red", "white", "blue","green")),
main = "Harvest variation index in India during drought year 2000",
key.pos = 4, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)
plot(merge_drought_indices["r_count_lt_p05_main"],
pal = colorRampPalette(c("red", "white", "blue","green")),
main = "Main growth stage index variation in India during drought year
2000",
key.pos = 4, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)
plot(merge_drought_indices["r_count_lt_p05_plant.start"],
pal = colorRampPalette(c("red", "white", "blue","green")),
main = "Plant start index variation in India during drought year 2000",
key.pos = 4, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)
plot(merge_drought_indices["r_count_lt_p05_plant"],
pal = colorRampPalette(c("red", "white", "blue","green")),
main = "main plant stage index variation in India during drought year
2000",
key.pos = 4, axes = TRUE, key.width = lcm(1.3), key.length = 1.0)
Regards and Thanks,
Ranjeet
--
*"Simple Heart, Humble Attitude and Surrender to Supreme Being make our
lives beautiful!"*
[[alternative HTML version deleted]]