[R-sig-Geo] Need to merge .RDS dataframe and India Shape file, and then plot yield map with tile legend

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Tue May 10 10:04:25 CEST 2022


On Tue, 10 May 2022, Ranjeet Kumar Jha wrote:

> Hello Everyone,
>
> I am trying to plot an Indian district shapefile filled with modeled yield,
> corresponding to each GDMID. I needed to first merge the Yield data with
> respect to each GDMID from the .RDS file with the district shapefile of
> India. Each district is represented by a GDMID. After merging, the Indian
> district map filled with modeled yield values needs to be plotted similar
> to an example attached figure. Shape file has five features, and one of
> them is "GDMID".

The code is not a reproducible example; it needs to run with no reference 
to local directories. The use of tibbles or other misunderstandings 
represents state.ID and GDMID as numeric, they should be character, you 
cannot do arithmetic on them. The level 2 boundaries from 
https://geodata.ucdavis.edu/gadm/gadm4.0/gpkg/gadm40_IND.gpkg do not use 
GDMID, so unless you provide a download link to your boundaries, no-one 
can help. Also, your data.frame has 694 observations, GADM has 666.

My guess is that you have found a blog somewhere, probably very 
out-of-date (you use rgdal::readOGR() not sf::st_read()), and which is 
causing lots of problems, because it complicates things.

Use the sf package, avoid tidyverse which can convert representations 
without asking, make sure that the IDs are character not numeric (to 
preserve any leading zeros), and use tmap or mapsf not ggplot (much 
simpler and more intuitive interface for mapping).

See https://geocompr.robinlovelace.net/ and the sf vignettes. Once you 
have control of the IDs (for example try match() to see if they do match 
in type and value). sf has a merge method, which gives a good deal of 
control.

Hope this helps,

Roger

>
> I feel there is something wrong with my code for merging and plotting by
> ggplot.
>
> I have attached herewith my code, and RDS data-frame.
>
> It would be great if you could help me with merging issue and plotting the
> map, showing yield value with legend in tile format.
>
> Regards and Thanks,
> Ranjeet
>
>

-- 
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