1

I'm trying to convert my shapefile with spTransform(), but it's not successful. I don't know why it's changing only one axis. I want to change the axis into decimal degrees.

# Libraries ---------------------------------------------------------------
library(rgdal)
library(rgeos)
library(ggplot2)
library(ggthemes)
library(ggsn)
library(maps)
library(maptools)
library(grid)
library(plyr)    

URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
fil <- "gal.json"
if (!file.exists(fil)) download.file(URL, fil)
gal <- readOGR(fil, "OGRGeoJSON")
topo <- readOGR("public.geojson.json", layer ="OGRGeoJSON")
so here if you change CRS("+init=epsg:31983"), it's working, but I don't know why it's not working with only 4326...
gal <- gSimplify(gUnaryUnion(spTransform(gal, CRS("+init=epsg:4326")), id=NULL), tol=0.001)
topo <- spTransform(topo, CRS("+init=epsg:4326")) # epsg:4326 is decimal degree, epsg:31983 is meters only

# double check that they match
identical(proj4string(topo),proj4string(gal))    ## [1] TRUE
gal_map <- fortify(gal)
topo_map <- fortify(topo)
topo_map <- join(topo_map, topo@data, by="id")
names(topo_map)

# Complete Galápagos Island!!!! -------------------------------------------
gg <- ggplot()
gg <- gg + geom_map(map=gal_map, 
                    data=gal_map,
                    aes(x=long, 
                        y=lat, 
                        map_id=id),
                    color="black", 
                    fill="#8FBC8F", #comment# #FFFFFF is white, # the colour #8FBC8F is a standard green for map's background
                    alpha = 0.4,
                    size= .1) # was .5 
gg <- gg + coord_equal()
gg <- gg + ggsn:::scalebar(gal_map, # scale bar 
                           dist=50, 
                           location="bottomleft", 
                           st.size=5)
gg <- gg + #theme_map() + # veryyyyyyyyyyyyyyyyyy sober
  theme(legend.position = "right",
        legend.text = element_text(size = 12, 
                                   colour = "black", 
                                   angle = 0),
        axis.text.x = element_text(size=12),
        axis.ticks.length = unit(.2, "cm"), 
        panel.grid.major = element_line(colour = "blue",linetype = "dotted",size = .1),
        panel.background = element_rect(fill = "white"), # lightsteelblue2 for water # remove the default grey, you can add blue color for water (# background making illusion that this is water)
        panel.grid.major = element_blank(), # remove grid
        panel.grid.minor = element_blank(), # remove grid
        strip.background = element_blank(), # remove grid
        panel.border = element_rect(colour = "black", # border to the map
                                    fill=NA, 
                                    size=1)) 
gg <- gg + geom_path(data = topo_map, # adding the elevation line
                     aes(long, lat, group=group, colour = elev), 
                     size = 0.1 , 
                     alpha=0.4, 
                     linetype = 1) + 
#         geom_polygon(data = topo_map, # adding the elevation polygon
#                      alpha=0.01,
#                      aes(long,
#                          lat, 
#                          fill=elev, 
#                          group=group)) + 
  coord_equal() + 
  scale_color_gradient(low = "aquamarine", # guide = "none" if you don't want
                       high = "darkgreen", 
                       guide = "colourbar") + 
  labs(title = "Galápagos Islands", 
       y = "lat", 
       x = "long", 
       colour = "Elevation") 
gg

enter image description here

In the end, I want that the axis are shown in decimal degrees. Not in meters (31983). I don't want to do this manually (by writing the axis myself), I want to transform the scale.

  • maybe you should try something like spTransform(topo, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ")" or could you include in your post the output of proj4string(topo) and proj4string(gal) – MLavoie Mar 13 '16 at 14:00
  • I've tried something like this: gal <- gSimplify(gUnaryUnion(spTransform(gal, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ")), id=NULL), tol=0.001) topo <- spTransform(topo, CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs ")) # epsg:4326 is decimal degree, epsg:31983 is meters only, but it's still not working... – M. Beausoleil Mar 13 '16 at 18:12
  • proj4string(gal) "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" proj4string(topo) "+init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" – M. Beausoleil Mar 13 '16 at 18:20
  • Galápagos Island are west of South America, not Africa! – MLavoie Mar 13 '16 at 18:37
  • Sorry, I'm quite new with that kind of EPSG. If I understand correctly, it's not the right coordinate reference system. I just want to change the coordinate system to put it in decimal degree. But I want the Galápagos to be shown. Where could I find this information? – M. Beausoleil Mar 13 '16 at 18:47
1

I suspect something went wrong with your geojson. You should not need to reproject at all.

library(rgdal)
library(ggplot2)

URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
fil <- "gal.json"
if (!file.exists(fil)) download.file(URL, fil)
gal <- readOGR(fil, "OGRGeoJSON")

# this is in EPSG 4326
proj4string(gal)
> [1] "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"

gal_map <- fortify(gal)
ggplot() + 
  geom_map(map=gal_map,data=gal_map, aes(x=long, y=lat, map_id=id)) + 
  coord_equal()

enter image description here

| improve this answer | |
  • So the problem was the scale at the bottom. Thanks for your help! I figure it out with your advice. – M. Beausoleil Mar 15 '16 at 4:44
0

The problem is the scalar function.

Should be:

ggsn:::scalebar(gal_map, # scale bar 
                           dist=50, 
                           location="bottomleft", 
                           model = "WGS84", 
                           dd2km = TRUE,
                           st.size=5)

Full code

# Libraries ---------------------------------------------------------------
library(rgdal)
library(rgeos)
library(ggplot2)
library(ggthemes)
library(ggsn)
library(maps)
library(maptools)
library(grid)
library(plyr)    

URL <- "https://osm2.cartodb.com/api/v2/sql?filename=public.galapagos_islands&q=select+*+from+public.galapagos_islands&format=geojson&bounds=&api_key="
fil <- "gal.json"
if (!file.exists(fil)) download.file(URL, fil)
gal <- readOGR(fil, "OGRGeoJSON")
topo <- readOGR("public.geojson.json", layer ="OGRGeoJSON")
topo <- spTransform(topo, proj4string(gal))

# double check that they match
identical(proj4string(topo),proj4string(gal))    ## [1] TRUE
gal_map <- fortify(gal)
topo_map <- fortify(topo)
topo_map <- join(topo_map, topo@data, by="id")
names(topo_map)

# Complete Galápagos Island!!!! -------------------------------------------
gg <- ggplot()
gg <- gg + geom_map(map=gal_map, 
                    data=gal_map,
                    aes(x=long, 
                        y=lat, 
                        map_id=id),
                    color="black", 
                    fill="#8FBC8F", #comment# #FFFFFF is white, # the colour #8FBC8F is a standard green for map's background
                    alpha = 0.4,
                    size= .1) # was .5 
gg <- gg + coord_equal()
gg <- gg + ggsn:::scalebar(gal_map, # scale bar 
                           dist=50, 
                           location="bottomleft", 
                           model = "WGS84", 
                           dd2km = TRUE,
                           st.size=5)
gg <- gg + #theme_map() + # veryyyyyyyyyyyyyyyyyy sober
  theme(legend.position = "right",
        legend.text = element_text(size = 12, 
                                   colour = "black", 
                                   angle = 0),
        axis.text.x = element_text(size=12),
        axis.ticks.length = unit(.2, "cm"), 
        panel.grid.major = element_line(colour = "blue",linetype = "dotted",size = .1),
        panel.background = element_rect(fill = "white"), # lightsteelblue2 for water # remove the default grey, you can add blue color for water (# background making illusion that this is water)
        panel.grid.major = element_blank(), # remove grid
        panel.grid.minor = element_blank(), # remove grid
        strip.background = element_blank(), # remove grid
        panel.border = element_rect(colour = "black", # border to the map
                                    fill=NA, 
                                    size=1)) 
gg <- gg + geom_path(data = topo_map, # adding the elevation line
                     aes(long, lat, group=group, colour = elev), 
                     size = 0.1 , 
                     alpha=0.4, 
                     linetype = 1) + 
#         geom_polygon(data = topo_map, # adding the elevation polygon
#                      alpha=0.01,
#                      aes(long,
#                          lat, 
#                          fill=elev, 
#                          group=group)) + 
  coord_equal() + 
  scale_color_gradient(low = "aquamarine", # guide = "none" if you don't want
                       high = "darkgreen", 
                       guide = "colourbar") + 
  labs(title = "Galápagos Islands", 
       y = "lat", 
       x = "long", 
       colour = "Elevation") 
gg
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.