简体   繁体   中英

Plotting raster data and points

I have a dataframe of points and plotted it using plot . Now I want to add a RasterLayer of AnnualTemp. Can you please assist in completing the code?

library(raster)

MinTemp_plots <- data.frame(
  Station = c( "Labasa", "Laucala", "Lautoka", "Levuka", "Matei", "Matuku",
               "Nabouwalu", "Nacocolevu",  "Nadi", "Nausori",  "Ono-I-Lau",  "Penang",
               "Savusavu",   "UduPoint",   "Viwa", "Yasawa"),
  
  Latitude = c(-16.43, -18.15, -17.6, -17.68, -16.69, -19.15, -16.98,
               -18.1,  -17.75, -18.03, -20.65, -17.37, -16.78, -16.11,
               -17.14, -16.78 ),
  
  Longitude = c(179.36, 178.45, 177.45, 178.83, 180, 179.76, 178.7, 177.55,
                177.45, 178.56, 178.7, 178.15, 179.34, 180, 176.93,
                177.5),
  
  AnnualTempMin = c(1.722, 1.711, 0.042, 0.135, 0.264, 0.276, 0.625, 1.215,  
                    1.522, 0.917,  0.617, 0.072, 0.509, 1.057, 1.201, 0.123))


plot(y= MinTemp_plots$Latitude, x= MinTemp_plots$Longitude)

r <- raster(xmn=1790828.61, xmx=2337149.40, ymn=3577110.39, ymx=4504717.19, res=100000)
crs(r) = crs("+init=epsg:3460")
    

I am not entirely sure what your question is, but here goes:

Add values to the example RasterLayer for plotting

values(r) <- 1:ncell(r)

Create a SpatialPointsDataFrame

x <- MinTemp_plots
coordinates(x) <- ~ Longitude + Latitude
crs(x) <- "+proj=longlat"

Transform it to the same coordinate reference system as the raster

y <- spTransform(x, crs(r))

And now plot

plot(r)
points(y)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM