Hi Ranjeet, I didn’t find out how to do it with sf, but you can get it using the mapsf package:
library(mapsf) brks <- mf_get_breaks(merge_yield$mod.yield.all, breaks = "pretty") nclass <- length(brks) - 1 # set a theme mf_theme("ink") # plot yield mf_map( x = merge_yield, var = "mod.yield.all", type = "choro", breaks = brks, pal = colorRampPalette(c("red", "white", "blue"))(nclass), border = "grey", lwd = 0.5, leg_val_rnd = 0, leg_pos = "topright", leg_title = "Yield (tons/ha)" ) # title etc. mf_layout(title = "Rice yield by district in India", credits = "", scale = FALSE, arrow = FALSE) Kind regards, Zivan On Thu, May 12, 2022 at 3:42 PM Ranjeet Kumar Jha < ranjeetjhaiit...@gmail.com> wrote: > Hi Zivan, Just a quick question - Can we add legend title - "Yield > (tons/ha) with this plot function in sf package. I could n't find so. > > Thanks, > Ranjeet > > On Thu, May 12, 2022 at 4:34 PM Ranjeet Kumar Jha < > ranjeetjhaiit...@gmail.com> wrote: > >> Thank you Zivan for your kind help and support ! Code really worked now >> and I understood the concept now! >> >> I again appreciate your help! >> >> Regards and Thanks, >> Ranjeet >> >> On Thu, May 12, 2022 at 4:06 PM Zivan Karaman <zivan.kara...@gmail.com> >> wrote: >> >>> 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 < >>> ranjeetjhaiit...@gmail.com> wrote: >>> >>>> Hello Roger, >>>> >>>> I really appreciate your support and help! Now I have uploaded all the >>>> required .shx, .dbf, and .prj files in the link folder. >>>> >>>> >>>> https://drive.google.com/drive/folders/1dNmGTI8_c9PK1QqmfIjnpbyzuiCXgxFC?msclkid=4816425dd04111ec912cf8b4175aa93c >>>> >>>> On Wed, May 11, 2022 at 5:42 PM Roger Bivand <roger.biv...@nhh.no> >>>> wrote: >>>> >>>> > On Tue, 10 May 2022, Ranjeet Kumar Jha wrote: >>>> > >>>> > > Hello Roger, >>>> > > >>>> > > Thanks for your kind reply! I am going through this. However, >>>> sharing the >>>> > > link of code, shape file and .rds data frame for better >>>> understanding and >>>> > > 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 >>>> > >>>> > > >>>> > > >>>> > >>>> https://drive.google.com/drive/folders/1dNmGTI8_c9PK1QqmfIjnpbyzuiCXgxFC?msclkid=4816425dd04111ec912cf8b4175aa93c >>>> > > >>>> > > Regards and Thanks, >>>> > > Ranjeet >>>> > > >>>> > > On Tue, May 10, 2022 at 1:34 PM Roger Bivand <roger.biv...@nhh.no> >>>> > wrote: >>>> > > >>>> > >> 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.biv...@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.biv...@nhh.no >>>> > https://orcid.org/0000-0003-2392-6140 >>>> > https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en >>>> > >>>> >>>> >>>> -- >>>> Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA) >>>> https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56 >>>> ----------------------------------------------------------- >>>> Email: *ranjeetjhaiit...@gmail.com <ranjeetjhaiit...@gmail.com>* >>>> >>>> >>>> *"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@r-project.org >>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo >>>> >>> >> >> -- >> Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA) >> https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56 >> ----------------------------------------------------------- >> Email: *ranjeetjhaiit...@gmail.com <ranjeetjhaiit...@gmail.com>* >> >> >> *"Simple Heart, Humble Attitude and Surrender to Supreme Being make our >> lives beautiful!"* >> >> > > -- > Ranjeet Kumar Jha, M.Tech. (IIT Kharagpur), Ph.D. (USA) > https://www.linkedin.com/in/ranjeet-kumar-jha-ph-d-usa-73a5aa56 > ----------------------------------------------------------- > Email: *ranjeetjhaiit...@gmail.com <ranjeetjhaiit...@gmail.com>* > > > *"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@r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo