Hello sir/madam,
I have been writing codes using R and was trying to overlay contour lines
on the map using the data set provided in the attachment. The code which I
am working on is attached below. The problem is the overlay of the contours
is not showing in the final map. If someone can assist in correcting the
codes would be really appreciated .
# Packages
#if (!require("rspatial")) remotes::install_github('rspatial/rspatial')
library(rspatial)
library(sp)
library(rgdal)
library(rgoes)
library(raster)
library(ggplot2)
# Fiji geo data
# download
#Draft map of Fiji
world <- rnaturalearth::ne_countries(scale = "Large", returnclass = "sf")
# map of Fiji Islands
Fiji <- ggplot(data=world) +
geom_sf() +
coord_sf(
crs = 3832, # https://epsg.io/3832
xlim = c(2984265.06, 3539584.72), # limits are taken from projected
bounds
ylim = c(-2224162.41, -1811688.88) # of EPSG:3832
)+ theme_bw()
#Plot map of Fiji
Fiji
#read csv data from excel file for contoure analysis
data.Fdf <- read.csv("C://Users/Sownal/Documents/data.csv")
View(data.Fdf)
class(data.Fdf)
#remove NA values in the spatial Data Frame
data.dfclean <- na.omit(data.Fdf)
data.dfclean
data.dfclean$long <- as.numeric(data$Longitude)
data.dfclean$lat <- as.numeric(data$Latitude)
# convert data to spatial data fame for spatial analysis and raster analysis
data.FSP <- SpatialPointsDataFrame(data= data.dfclean, coords =
data.dfclean$lat, data.dfclean$long)
data.FSP
#select one years data and overlay contours on the Map of Fiji
Fiji <- ggplot(data=world) +
geom_sf() +
coord_sf(
crs = 3832, # https://epsg.io/3832
xlim = c(2984265.06, 3539584.72), # limits are taken from projected
bounds
ylim = c(-2224162.41, -1811688.88) # of EPSG:3832
)+ theme_bw() + filled.contour(data.dfclean$Year)
# plot the map of Fiji with the contours lines
Fiji
******************************************************************************************************
Thanking you in advance
sownalc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20220405/31904f5b/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DataR.csv
Type: text/csv
Size: 628 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20220405/31904f5b/attachment.csv>
Contour plot
3 messages · sownal chand, Micha Silver
An HTML attachment was scrubbed... URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20220405/c6c465ca/attachment.html>
Thanks Micha, I would also try out your suggestion on interpolation of data. Kind regards Sownal
On Wed, Apr 6, 2022, 00:39 Micha Silver <tsvibar at gmail.com> wrote:
Hi:
I would point to two issues with your code. First, we should be switching
totally to sf. You have some spatial vectors as sf classes and some as sp.
This should be avoided today.
Here's my version for plotting
library(ggplot2)
library(sf)
library(rnaturalearth)
#Draft map of Fiji
world <- ne_countries(returnclass = "sf")
Fiji <- ne_countries(country="Fiji", returnclass="sf")
data.Fdf <- read.csv("DataR.csv")
# Get rid of that last summary row with no coordinates!
data.Fdf <- data.Fdf[complete.cases(data.Fdf),]
data.sf <- st_as_sf(data.Fdf, coords=c("long", "lat"), crs="EPSG:4326")
# map of Fiji Islands
Fiji.plot <- ggplot() +
geom_sf(data=world) +
geom_sf(data=Fiji) +
geom_sf(data=data.sf, aes(color=Year.1965)) +
coord_sf(crs = 3832, # https://epsg.io/3832
xlim = c(2984265.06, 3539584.72), # limits are taken from projected
bounds
ylim = c(-2224162.41, -1811688.88) # of EPSG:3832
) +
theme_bw()
#Plot map of Fiji
Fiji.plot
But more to your question: filled.contour() is not a ggplot2 function, so
you cannot include it in a string of ggplot elements. filled.contour()
takes three main variables: a sequence of 'x' and 'y' values for the grid
on which to plot the contours, and a matrix 'z' of values from which
contours are derived. You are working with a (very) small set of *point*
data. No matrix of values.... Typically the 'z' matrix would be created by
interpolating between the points to get a continuous raster, then use that
for the 'z' parameter to filled.contour().
Given that you are covering an area of several 100 km, with only 13 data
points (and some are almost exactly overlapping), and much of the area is
ocean, with no measurements and probably totally different precipitation
behavior, I don't see any clear way to get a reliable precipitation
distribution, so no isohyetal contour lines.
Sorry :-(
To continue with this, I would first deal with only the main island of
Fiji, then do a kriging interpolation of annual rainfall, year by year, and
from those interpolated rasters, you can derive contours. You would use the
`contour` function from `terra` package for this.
On 05/04/2022 11:10, sownal chand wrote:
Hello sir/madam,
I have been writing codes using R and was trying to overlay contour lines
on the map using the data set provided in the attachment. The code which I
am working on is attached below. The problem is the overlay of the contours
is not showing in the final map. If someone can assist in correcting the
codes would be really appreciated .
# Packages
#if (!require("rspatial")) remotes::install_github('rspatial/rspatial')
library(rspatial)
library(sp)
library(rgdal)
library(rgoes)
library(raster)
library(ggplot2)
# Fiji geo data
# download
#Draft map of Fiji
world <- rnaturalearth::ne_countries(scale = "Large", returnclass = "sf")
# map of Fiji Islands
Fiji <- ggplot(data=world) +
geom_sf() +
coord_sf(
crs = 3832, # https://epsg.io/3832
xlim = c(2984265.06, 3539584.72), # limits are taken from projected
bounds
ylim = c(-2224162.41, -1811688.88) # of EPSG:3832
)+ theme_bw()
#Plot map of Fiji
Fiji
#read csv data from excel file for contoure analysis
data.Fdf <- read.csv("C://Users/Sownal/Documents/data.csv")
View(data.Fdf)
class(data.Fdf)
#remove NA values in the spatial Data Frame
data.dfclean <- na.omit(data.Fdf)
data.dfclean
data.dfclean$long <- as.numeric(data$Longitude)
data.dfclean$lat <- as.numeric(data$Latitude)
# convert data to spatial data fame for spatial analysis and raster
analysis
data.FSP <- SpatialPointsDataFrame(data= data.dfclean, coords =
data.dfclean$lat, data.dfclean$long)
data.FSP
#select one years data and overlay contours on the Map of Fiji
Fiji <- ggplot(data=world) +
geom_sf() +
coord_sf(
crs = 3832, # https://epsg.io/3832
xlim = c(2984265.06, 3539584.72), # limits are taken from projected
bounds
ylim = c(-2224162.41, -1811688.88) # of EPSG:3832
)+ theme_bw() + filled.contour(data.dfclean$Year)
# plot the map of Fiji with the contours lines
Fiji
******************************************************************************************************
Thanking you in advance
sownalc
_______________________________________________ R-sig-Geo mailing listR-sig-Geo at r-project.orghttps://stat.ethz.ch/mailman/listinfo/r-sig-geo -- Micha Silver Ben Gurion Univ. Sde Boker, Remote Sensing Lab cell: +972-523-665918