简体   繁体   中英

How to plot a extracted raster in proper xy coordinates?

I have a ndvi raster. I had to extract the values using a threshold conditon.When I plot the graph of the extracted raster, the xy coordinated are not lat long.

I have tried the ggplot() , also the used rastertopoints and rasterfromXYZ to plot the raster but in vain. The code is below. Unable to plot s. xy coordiantes are not lat long.

library(raster)
library(rgdal)
raster <- ("D:/Project/ndvisoybean.tif")
plot(ndvi)
ndvi[is.na(ndvi)] <-0
s <- ndvi[ndvi@data@values<maxValue(ndvi) & ndvi@data@values>0.3*maxValue(ndvi)]

I want a graph of the extracted raster s with proper lat long coordinates in x y.

As fa as I know there is no appropriate case to plot lon/lat xy coordinate map if the raster is not WGS84 projection. One way is that converting raster layer to WGS84 projection.So that the plot coordiante is lon/lat. It is only appreciate for small map or world map. The other way is using coord_map function in ggplot.But the import data must be lon/lat dataframe rather than ohter projection coordinate. So it is low efficiency to plot map. More serous it is very slow when converting data.frame point coordinate to another. this way is only suitable for small width/height raster. Code example:

Primaly you should convert your raster to WGS84 coordiante.

df=rasterToPoint(raster_WGS84)
ggplot(df, aes(y=lon, x=lat, color=values)) +
   geom_point(size=2, shape=15) +
   theme() + 
   scale_color_distiller(palette='Spectral') +
   coord_map('lambert', lat0=30, lat1=65, xlim=c(-20, 39), ylim=c(19, 75)) 

Summary: If your raster is projected coordinate,such as Albers , lambert rather than lon/lat WGS84 coordiate, it is inconvenient to plot a map with lon/lat xy coodiate.

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