[R-sig-Geo] Shapefile and Basemap

sownal chand @own@|ch@nd|m@ @end|ng |rom gm@||@com
Fri May 13 11:47:56 CEST 2022


Hello sir/madam,

I am working with shape file of my country and the issue I am facing is the
shapefile is scattered while plotting it using basemap. I am using my
sample point data which is attached to this email. I hope that some expert
in this area would help in correcting the codes below to show the shapefile
in one location ( its a pacific centered map)

**************************************************************************************************


library(sp)
library(raster)
library(rgdal)
library(leaflet)

#read.csv
read.csv ("C://Users/Documents/data.csv") -> data.df
head(data.df)

hist(data.df$Year, breaks=20)

#remove NA valuues in the spatial Data Frame
data.df <- na.omit(data.df)
View(data.df)

plot(data.df$long, data.df$lat,
     ylab = "Latitude", xlab="Longitude") #boring!

# Use the cex function to plot circle size as a function of a variable
plot(data.df$long, data.df$lat,
     cex = data.df$Year.7 * 0.045,
     ylab = "Latitude", xlab="Longitude")

data.df_SPDF <- SpatialPointsDataFrame(coords = data.df[,c("long", "lat")],
                                                data = data.df[,c("Year",
"Year.1", "Year.2","Year.3","Year.4")],
                                                proj4string =
CRS("+init=epsg:4326")) # sets the projection to WGS 1984 using lat/long.
Optional but good to specify

# Summary of object
data.df_SPDF

# SPDFs partition data elements, e.g. the coordinates are stored separately
from the data
head(data.df_SPDF using coords)

head(data.df_SPDF using data)


# You can quickly access the data frame as per a standard data frame, e.g.
head(data.df_SPDF$Year)


# You can use the plot or spplot function to get quick plots
plot(data.df_SPDF)

spplot(data.df_SPDF, zcol = "Year")

FIJ_Adm_2 <- readOGR("FIJ_Adm_2_shapefile", "FIJ_Adm_2")

# You first need the ISO3 codes for the country of interest.

# The getData function then allows you to retrieve the relevant admin level
boundaries from GADM.
FJI_Adm_2 <- raster::getData("GADM", country="FJI", level=2)

# Plot both country and data points
plot(FJI_Adm_2)
points(data.df$long, data.df$lat,
       cex = data.df$Year * 0.045,
       ylab = "Latitude", xlab="Longitude",
       col="red")

basemap <- leaflet() %>% addProviderTiles("CartoDB.Positron")

basemap %>% addPolygons(data=FJI_Adm_2)

# to change the colors/line weight
basemap %>% addPolygons(data=FJI_Adm_2, color = "red",
                        weight = 1, fillOpacity = 0.2)


# If you want to add points as well
basemap %>% addPolygons(data=FJI_Adm_2, weight = 2,
                        popup = FJI_Adm_2$NAME_2) %>%

  addCircleMarkers(data=data.df_SPDF,
                   color="red", radius = 2)

library(wesanderson) # for a nice color palette
colorPal <- colorNumeric(wes_palette("Zissou1")[1:5], data.df_SPDF$Year, n
= 5)


# colorPal is now a function you can apply to get the corresponding
# color for a value
colorPal(0.6)

basemap %>% addPolygons(data=FJI_Adm_1, weight = 2, fillOpacity=0,
                        popup = FJI_Adm_1$NAME_1) %>%

  addCircleMarkers(data=data.df_SPDF,
                   color = colorPal(data.df$Year),
                   radius = 2,
                   popup = as.character(data.df$Year))%>%

  addLegend(pal = colorPal,
            title = "Average Temp for Year",
            values = data.df_SPDF$Year)



*************************************************************************************************

Thanking you in advance
sownalc

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20220513/f09fac6c/attachment-0001.html>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: DataR.csv
Type: text/csv
Size: 611 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20220513/f09fac6c/attachment-0001.csv>


More information about the R-sig-Geo mailing list