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 <
ranjeetjhaiitkgp at 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 <
ranjeetjhaiitkgp at 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.karaman at 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 <
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,
link of code, shape file and .rds data frame for better
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
corresponding to each GDMID. I needed to first merge the Yield
respect to each GDMID from the .RDS file with the district
India. Each district is represented by a GDMID. After merging,
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
represents state.ID and GDMID as numeric, they should be
GDMID, so unless you provide a download link to your boundaries,
can help. Also, your data.frame has 694 observations, GADM has
My guess is that you have found a blog somewhere, probably very
out-of-date (you use rgdal::readOGR() not sf::st_read()), and
causing lots of problems, because it complicates things.
Use the sf package, avoid tidyverse which can convert
without asking, make sure that the IDs are character not numeric
preserve any leading zeros), and use tmap or mapsf not ggplot
have control of the IDs (for example try match() to see if they
in type and value). sf has a merge method, which gives a good
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]]