Question

I have zipped shape files. I want to change the xlables and ylables into equivalent longitude and latitude values and not Eastings and Northings. Please suggest me how can i display latitude and longitude in place of this northings and eastings.It works when i change the projection of shapefile into WGS1984 but that distort the area and i do not want to do so. My code are

library(raster)
library(RColorBrewer)
library(ggplot2)
library(ncdf)
poly<-shapefile("Test_poly.shp", stringsAsFactors=FALSE, verbose=FALSE)
ggplot(poly) +
  aes(long,lat,group=group, fill = id) +
  geom_polygon() +
  geom_path(color="white") +
  coord_equal() +
  scale_fill_brewer("Bydeler")
Was it helpful?

Solution

(Thanks for updating the shapefiles. However I also found that I needed to have proj on my system and the rgdal R package installed. Maybe worth mentioning? Or perhaps this just reflects the fact that I don't do a lot of spatial work in R)

Ultimately, I don't think that ggplot2 has a built in system for using different mapping units in the axes. What I present here are workarounds, somewhat ugly, but which should work.

To get Lat and Long axes, one option you have is to convert your shapefile to lat and long directly. E.g.:

poly <- shapefile("Test_poly.shp", stringsAsFactors=FALSE, verbose=FALSE)
poly <- spTransform(poly, CRS("+proj=longlat +ellps=GRS80"))

ggplot(poly2) +
    aes(long,lat,group=group, fill = id) +
    ylim(c())+
    xlim(c(1324794,1341700)) + 
    geom_polygon() +
    geom_path(color="white") +
    coord_equal() +
    scale_fill_brewer("Bydeler")

As you pointed out, this distorts the map. I think this is because the difference between one unit of lat is not the same as between one unit of lon but you have told ggplot to use equal coordinates.

The best work around I can see is to figure out what the ratio between lat and lon is in this case and specify that with coord_fixed(ratio = X) (where X is the ratio) instead of using coord_equal().

You should be able to figure out this ratio by printing the poly objects and comparing the 'extents' between the untransformed and transformed shape objects:

> poly
    class       : SpatialPolygonsDataFrame 
    features    : 7 
    extent      : 1211747, 1466285, 728771.9, 999422.3  (xmin, xmax, ymin, ymax)
    coord. ref. : +proj=aea +lat_1=50 +lat_2=58.5 +lat_0=45 +lon_0=-126 +x_0=1000000 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0 
    variables   : 1
    names       : Id 
    min values  :  0 
    max values  :  0

Alternatively you could use the original shapefile and then use manual axis labels to get lat and lon, e.g., scale_y_continuous(breaks = c(700000, 800000, 900000,1000000), labels = c(51,52,53,54))

These is NOT an elegant solution, but should work well enough for the purposes of making figures for presentation or publication.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top