north arrow and scale bar in ggplot2

1,290 views
Skip to first unread message

Manuel Spínola

unread,
May 17, 2011, 10:52:05 AM5/17/11
to ggp...@googlegroups.com
Dear list members,

I am doing spatial plot with ggplot2 (data frame from a raster) and I am wonder if it is possible to include a north arrow and a scale bar in the plot.
Best,

Manuel

--
Manuel Spínola, Ph.D.
Instituto Internacional en Conservación y Manejo de Vida Silvestre
Universidad Nacional
Apartado 1350-3000
Heredia
COSTA RICA
mspi...@una.ac.cr
mspin...@gmail.com
Teléfono: (506) 2277-3598
Fax: (506) 2237-7036
Personal website: Lobito de río
Institutional website: ICOMVIS

David Kahle

unread,
May 17, 2011, 11:11:34 AM5/17/11
to Manuel Spínola, ggp...@googlegroups.com
Not build in. :) You could add one pretty easily with annotate() though...

david.

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Manuel Spínola

unread,
May 17, 2011, 11:21:50 AM5/17/11
to David Kahle, ggp...@googlegroups.com
Thank you David.

How I do that?

Best,

Manuel

2011/5/17 David Kahle <david...@gmail.com>

Osmo Salomaa

unread,
May 19, 2011, 3:39:38 AM5/19/11
to ggplot2
For anyone interested, here are two functions I wrote a while back to
simplify adding a very simple scalebar to a plot.

hscale_segment = function(breaks, ...)
{
y = unique(breaks$y)
stopifnot(length(y) == 1)
dx = max(breaks$x) - min(breaks$x)
dy = 1/30 * dx
hscale = data.frame(ix=min(breaks$x), iy=y, jx=max(breaks$x),
jy=y)
vticks = data.frame(ix=breaks$x, iy=(y - dy), jx=breaks$x, jy=(y +
dy))
df = rbind(hscale, vticks)
return(geom_segment(data=df,
aes(x=ix, xend=jx, y=iy, yend=jy),
...))

}

hscale_text = function(breaks, ...)
{
dx = max(breaks$x) - min(breaks$x)
dy = 2/30 * dx
breaks$y = breaks$y + dy
return(geom_text(data=breaks,
aes(x=x, y=y, label=label),
hjust=0.5,
vjust=0,
...))

}

Then it's a simple matter of defining the break points for the
scalebar and adding layers with the two above functions to your plot.
Defining the breaks obviously depends on your coordinate system, the
below example is for a projected metric coordinate system. (ymin,
xmax) is the lower right hand corner in which the scalebar is being
placed.

scalebreaks = data.frame(x=seq(xmax - 11000, xmax - 1000, 5000),
y=(ymin + 1000),
label=c("0", "5", "10 km"))

plot = (ggplot(...)
+ ...
+ hscale_segment(scalebreaks, size=0.1)
+ hscale_text(scalebreaks, size=2)
+ ...
)

It will look about like this:

0 5 10 km
|--------|--------|

Adding a north arrow should be trivial, assuming you yourself know
which way north is.

Pet Chiang

unread,
Nov 14, 2014, 11:24:32 PM11/14/14
to ggp...@googlegroups.com
How can I find out my map xmax and ymin?
Reply all
Reply to author
Forward
0 new messages