[R-sig-Geo] Need to plot all 5 spatial maps together in a single window
Roger Bivand
Roger@B|v@nd @end|ng |rom nhh@no
Thu Jun 9 15:04:06 CEST 2022
On Thu, 9 Jun 2022, Ranjeet Kumar Jha wrote:
> Hello Roger,
>
> Thanks for your kind response! I have provided the reproducible data and
> code now in this shared link.
>
> https://drive.google.com/drive/folders/1dNmGTI8_c9PK1QqmfIjnpbyzuiCXgxFC?msclkid=4816425dd04111ec912cf8b4175aa93c
>
> Please now look at it! I appreciate your help.
No, now it is up to you to take the reproducible example, and modify the
alternative you choose to suit your needs. You can keep your existing
code but convert each sf plot to a grob in turn, use tmap, or use mapsf,
but if I do that, you will not learn from the process. That is why
reproducible examples are crucial; they provide a shared platform for
learning. Providing access to your data may be sensible in an off-list
setting when no reproducible example permits a bug to be found, but
preferably not otherwise.
Roger
>
> Regards and Thanks,
> Ranjeet
>
> On Thu, Jun 9, 2022 at 2:48 PM Roger Bivand <Roger.Bivand using nhh.no> wrote:
>
>> 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
>>
>>
>>>
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwilkelab.org%2Fcowplot%2Farticles%2Fplot_grid.html&data=05%7C01%7CRoger.Bivand%40nhh.no%7C01ce113052f9492ab51d08da49ec9930%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637903577780613163%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=vrH2iUZeaQw6TgzuYAYLXS6Yv%2Bt0hVXgu6PPDYcriEg%3D&reserved=0
>>> It has mainy features and permit you to save also the combined plots :
>>> here
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fsearch.r-project.org%2FCRAN%2Frefmans%2Fcowplot%2Fhtml%2Fsave_plot.html&data=05%7C01%7CRoger.Bivand%40nhh.no%7C01ce113052f9492ab51d08da49ec9930%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637903577780613163%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=gRj%2BPj%2B29W0mrOtT0Vp5HNEigvBQUsOL4VAdF%2B64fe0%3D&reserved=0
>>> or
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fwilkelab.org%2Fcowplot%2Freference%2Fggsave2.html&data=05%7C01%7CRoger.Bivand%40nhh.no%7C01ce113052f9492ab51d08da49ec9930%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637903577780613163%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=Idsy6TN5bxrqZtqgi6CGpqzRKjcofl71cR9EBTc9jvY%3D&reserved=0
>>>
>>> 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 using gmail.com>
>>> a écrit :
>>>
>>>> Hi,
>>>>
>>>> I like to plot all my 5 spatial maps of drought indices in a single
>> window
>>>> using the sf plot function in R. I am able to plot all maps separately
>> but
>>>> 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]]
>>>>
>>>> _______________________________________________
>>>> R-sig-Geo mailing list
>>>> R-sig-Geo using r-project.org
>>>>
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C01%7CRoger.Bivand%40nhh.no%7C01ce113052f9492ab51d08da49ec9930%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637903577780613163%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=awwczk%2Bpse%2B5tTKYB1ecwtuNE3C6AjgtgvzbE7Aga6w%3D&reserved=0
>>>>
>>>
>>> [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo using r-project.org
>>>
>> https://eur02.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-sig-geo&data=05%7C01%7CRoger.Bivand%40nhh.no%7C01ce113052f9492ab51d08da49ec9930%7C33a15b2f849941998d56f20b5aa91af2%7C0%7C0%7C637903577780613163%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=awwczk%2Bpse%2B5tTKYB1ecwtuNE3C6AjgtgvzbE7Aga6w%3D&reserved=0
>>>
>>
>> --
>> Roger Bivand
>> Emeritus Professor
>> Department of Economics, Norwegian School of Economics,
>> Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
>> e-mail: Roger.Bivand using nhh.no
>> https://orcid.org/0000-0003-2392-6140
>> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>
>
>
>
--
Roger Bivand
Emeritus Professor
Department of Economics, Norwegian School of Economics,
Postboks 3490 Ytre Sandviken, 5045 Bergen, Norway.
e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
More information about the R-sig-Geo
mailing list