Converting Axes to Lat/Lon?

Hi,

I generated some graphs using RStudio, but my axes are in terms of meter. How can I convert axes to Latitude and Longitude values?

April7th

I'm not sure, but the following SO thread may be useful:

Thank you but I'm pretty new in R, so I tried but it did not help :frowning:
Is there anyone who can help me on this?

Can you share the code you used to make the plot above? There is no hard rule for meters >> long/lat without some extra context...

Here is the code that I'm using:

Ok that's a good start. Could actually copy and paste the code as text instead of an image. The reason is I would like to try and reproduce it in my RStudio IDE and without text I would have to re-type everything and https://media.giphy.com/media/10PcMWwtZSYk2k/giphy.gif

#set projection of coordinates to lat/long
proj4string(points)<-CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
#crs(points)<-"+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
#tranform lat/lon to EASE-Grid 2.0 (original EASE, global cylindrical equal area, WGS84 projection)
points<-spTransform(points,CRS("+proj=cea +lat_0=0 +lon_0=0 +lat_ts=30 +ellps=WGS84 +datum=WGS84 +units=m"))

data<-as.data.frame(SMSM)
data<-unlist(data)    #unlist data frame to get vector of soil moisture
data <- SpatialPointsDataFrame(points, data.frame(data))    #add soil moisture data to spatial object

###instead, used "SpatialPixelsDataFrame" with tolerance limit for irregularity
###see bottom of: http://gis.stackexchange.com/questions/79062/how-to-make-raster-from-irregular-point-data-without-interpolation
data <- SpatialPixelsDataFrame(data, tolerance = 0.000163453, data@data) # 8.17194e-05
data <- raster(data[,'data'])

plot(data)

x2 <- c(8200000)
y2 <- c(3450000)

points(x2, y2, pch=8, col="blue")
plot(yourshapefile, add = T)

1 Like

I am a biased fan of ggspatial for quick-and-dirty plotting of anything spatial, with the downside that it takes much longer for large rasters. Probably an easier solution if you don't care about keeping the original projection is to transform the raster to lat/lon coordinates using raster::projectRaster().

library(raster)
#> Loading required package: sp
ggspatial::load_longlake_data()

# I think this is the plot function that you are currently using
plot(longlake_depth_raster)


# you can transform the raster to EPSG:4326 (lat/lon)
longlake_lat_lon <- projectRaster(longlake_depth_raster, crs = crs("+init=epsg:4326"))
plot(longlake_lat_lon)


# you could also use ggspatial + ggplot, which keeps the projection
# but has axis labels in lat/lon
library(ggspatial)
#> Loading required package: ggplot2
ggplot() +
  layer_spatial(longlake_depth_raster, aes(fill = stat(band1))) +
  scale_fill_continuous(na.value = NA)
#> Warning: Removed 8792 rows containing missing values (geom_raster).

Created on 2019-03-28 by the reprex package (v0.2.1)

3 Likes

Thank you for your response. I think I can use ggspatial+ggplot function to generate the graphs because 3rd image is exactly what I'm looking for.

So, I installed ggspatial. Should I remove the "plot(data)" line from my code and write "ggplot () +..." line instead?

If your original line was plot(data), with data as an object from the raster package, then ggplot() + layer_spatial(data) should get you started with ggspatial.

To do much else with ggspatial, you will have to learn to plot using ggplot2. Luckily, the resources for learning it are excellent!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.