Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't plot a scale bar or north arrow on ggplot2

Tags:

r

map

scale

ggplot2

Help! I have a data set of GPS x/y locations for trees in a forest, and with this codes into a pretty map in ggplot:

#ggmap!
library(ggmap)
library(mapproj)
map <- get_map(location = 'Madagascar', zoom = 10)
geocode("kianjavato")
#lon       lat
#47.86682 -21.38024
k <- "kianjavato"
myMap <- get_map(location=k, source="stamen", maptype="toner", crop=FALSE, zoom=16)
ggmap(myMap)  
m <- ggmap(myMap) + geom_point(aes(y = Lat, x = Lon, colour = Foraged, shape=Plot), data = GPS) 
n <- m + scale_colour_manual(values=c("blue", "red3")) +
     scale_shape_discrete(solid=F, legend=F) +
     scale_y_continuous(limits=c(-21.376,-21.3715)) +
     scale_x_continuous(limits=c(47.865,47.869))
plot(n)

However, no matter what types of code I try I can't get an arrow pointing north or scale bar to plot on my map! I've tried many solutions, some of which look like this:

trying to add scale bar

map.scale <- ggmap(new) + (ggmap, extent = "normal", maprange = FALSE) %+% sites.data +
    geom_point(aes(x = lon, y = lat, colour = colour)) + 
    geom_text(aes(x = lon, y = lat, label = label), hjust = 0, vjust = 0.5, size = 8/ptspermm) +
    geom_segment(data = sbar, aes(x = lon.start, xend = lon.end,
                                  y = lat.start, yend = lat.end)) + 
    geom_text(data = sbar, aes(x = (lon.start + lon.end)/2,
                               y = lat.start + 0.025*(bb$ur.lat - bb$ll.lat),
              label = paste(format(distance, digits = 4, nsmall = 2),'km')),
              hjust = 0.5, vjust = 0, size = 8/ptspermm)  + 
    coord_map(projection="mercator", xlim=c(bb$ll.lon, bb$ur.lon), 
              ylim=c(bb$ll.lat, bb$ur.lat))  

#library(SDMTools)
#Scalebar(x=47.868,y=-21.375,distance=100,unit='m') #show values in meters
#Error in map.scale(x = 50, y = -22) : argument "len" is missing, with no default

map.scale(x=47.868, y=-21.375, ratio=FALSE, relwidth=0.2)
#Error in map.scale(x = 47.868, y = -21.375, ratio = FALSE, relwidth = 0.2) : 
  unused argument(s) (ratio = FALSE, relwidth = 0.2)
map.scale(x=47.868, y=-21.375)
#Error in map.scale(x = 47.868, y = -21.375) : 
  argument "len" is missing, with no default

I need both the north arrow and the scale but neither will plot! Why?

like image 323
user3720904 Avatar asked Oct 08 '14 14:10

user3720904


Video Answer


1 Answers

The ggsn package is useful here. Without the GPS data, I can't execute all of your script, but this should add the north symbol and scale bar in reasonable locations, given the specified x and y limits.

#First, add a scale bar.
n <- n + scalebar(location="bottomright",y.min=-21.3755, y.max=-21.3715, 
             x.min=47.865, x.max=47.869, dist=.1, dd2km= TRUE, model='WGS84',
             st.dist=.04)

#Now add a north arrow and plot the map.
north2(n, x=.3, y=.8, symbol=9)

north symbol and scale bar example

like image 119
Jessica Beyer Avatar answered Oct 03 '22 07:10

Jessica Beyer