<div dir="ltr">Hello sir/madam,<div><br></div><div>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)</div><div><br></div><div>**************************************************************************************************</div><div><br></div><div><br><div>library(sp)<br>library(raster)<br>library(rgdal)<br>library(leaflet)<br><br>#read.csv<br>read.csv ("C://Users/Documents/data.csv") -> data.df<br>head(data.df)<br><br>hist(data.df$Year, breaks=20)<br><br>#remove NA valuues in the spatial Data Frame<br>data.df <- na.omit(data.df)<br>View(data.df)<br><br>plot(data.df$long, data.df$lat,<br>     ylab = "Latitude", xlab="Longitude") #boring!<br><br># Use the cex function to plot circle size as a function of a variable<br>plot(data.df$long, data.df$lat,<br>     cex = data.df$Year.7 * 0.045,<br>     ylab = "Latitude", xlab="Longitude")<br><br>data.df_SPDF <- SpatialPointsDataFrame(coords = data.df[,c("long", "lat")],<br>                                                data = data.df[,c("Year", "Year.1", "Year.2","Year.3","Year.4")],<br>                                                proj4string = CRS("+init=epsg:4326")) # sets the projection to WGS 1984 using lat/long. Optional but good to specify<br><br># Summary of object<br>data.df_SPDF<br><br># SPDFs partition data elements, e.g. the coordinates are stored separately from the data<br>head(data.df_SPDF@coords)<br><br>head(data.df_SPDF@data)<br><br><br># You can quickly access the data frame as per a standard data frame, e.g.<br>head(data.df_SPDF$Year)<br><br><br># You can use the plot or spplot function to get quick plots<br>plot(data.df_SPDF)<br><br>spplot(data.df_SPDF, zcol = "Year")<br><br>FIJ_Adm_2 <- readOGR("FIJ_Adm_2_shapefile", "FIJ_Adm_2")<br><br># You first need the ISO3 codes for the country of interest. <br><br># The getData function then allows you to retrieve the relevant admin level boundaries from GADM.<br>FJI_Adm_2 <- raster::getData("GADM", country="FJI", level=2) <br><br># Plot both country and data points<br>plot(FJI_Adm_2)<br>points(data.df$long, data.df$lat,<br>       cex = data.df$Year * 0.045,<br>       ylab = "Latitude", xlab="Longitude",<br>       col="red")<br><br>basemap <- leaflet() %>% addProviderTiles("CartoDB.Positron")<br><br>basemap %>% addPolygons(data=FJI_Adm_2)<br><br># to change the colors/line weight<br>basemap %>% addPolygons(data=FJI_Adm_2, color = "red", <br>                        weight = 1, fillOpacity = 0.2)<br><br><br># If you want to add points as well<br>basemap %>% addPolygons(data=FJI_Adm_2, weight = 2,<br>                        popup = FJI_Adm_2$NAME_2) %>%<br>  <br>  addCircleMarkers(data=data.df_SPDF,<br>                   color="red", radius = 2)<br><br>library(wesanderson) # for a nice color palette<br>colorPal <- colorNumeric(wes_palette("Zissou1")[1:5], data.df_SPDF$Year, n = 5)<br><br><br># colorPal is now a function you can apply to get the corresponding<br># color for a value<br>colorPal(0.6)<br><br>basemap %>% addPolygons(data=FJI_Adm_1, weight = 2, fillOpacity=0,<br>                        popup = FJI_Adm_1$NAME_1) %>%<br>  <br>  addCircleMarkers(data=data.df_SPDF,<br>                   color = colorPal(data.df$Year), <br>                   radius = 2,<br>                   popup = as.character(data.df$Year))%>%<br>  <br>  addLegend(pal = colorPal, <br>            title = "Average Temp for Year",<br>            values = data.df_SPDF$Year)<br></div></div><div><br></div><div><br></div><div><br></div><div>*************************************************************************************************</div><div><br></div><div>Thanking you in advance </div><div>sownalc</div></div>