Hi Ranjeet,
Perhaps you could try the following script:
#####Yield data
#average rice yield
dist_yield <- readRDS(file = "dist_yield.RDS")
#India shape file for districts
library(sf)
shp_dist <- read_sf("District_sim.shp")
#merging using merge function
merge_yield <- merge(shp_dist, dist_yield)
#plot
nbrks <- 10
plot(merge_yield["mod.yield.all"], nbreaks = nbrks, pal =
colorRampPalette(c("red", "white", "blue"))(nbrks), main = "yield by
district")
The plotting is done by the plot from the sf package, you can consult the
help page for more options.
You can, of course, change the color palette, the number of breaks, title,
and other decorations as you wish.
Kind regards,
Zivan
On Thu, May 12, 2022 at 9:03 AM Ranjeet Kumar Jha <
ranjeetjhaiitkgp at gmail.com> wrote:
On Tue, 10 May 2022, Ranjeet Kumar Jha wrote:
Hello Roger,
Thanks for your kind reply! I am going through this. However, sharing
link of code, shape file and .rds data frame for better understanding
helping through the real files.
District_sim.shp must of course be accompanied by District_sim.shx,
District_sim.dbf and most likely District_sim.prj, which are absent.
Roger
Regards and Thanks,
Ranjeet
On Tue, May 10, 2022 at 1:34 PM Roger Bivand <Roger.Bivand at nhh.no>
On Tue, 10 May 2022, Ranjeet Kumar Jha wrote:
Hello Everyone,
I am trying to plot an Indian district shapefile filled with modeled
corresponding to each GDMID. I needed to first merge the Yield data
respect to each GDMID from the .RDS file with the district
India. Each district is represented by a GDMID. After merging, the
district map filled with modeled yield values needs to be plotted
to an example attached figure. Shape file has five features, and
The code is not a reproducible example; it needs to run with no
to local directories. The use of tibbles or other misunderstandings
represents state.ID and GDMID as numeric, they should be character,
GDMID, so unless you provide a download link to your boundaries,
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
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
have control of the IDs (for example try match() to see if they do
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
ggplot.
I have attached herewith my code, and RDS data-frame.
It would be great if you could help me with merging issue and
map, showing yield value with legend in tile format.
Regards and Thanks,
Ranjeet
--
Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA)
https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56
-----------------------------------------------------------
Email: *ranjeetjhaiitkgp at gmail.com <ranjeetjhaiitkgp at gmail.com>*
*"Simple Heart, Humble Attitude and Surrender to Supreme Being make our
lives beautiful!"*
[[alternative HTML version deleted]]