> convertUnit(unit(c(181e3, 181e3, 181.5e3,
+ 181.5e3), "native"), "npc")
[1] 278.033794162826npc 278.033794162826npc 278.801843317972npc
[4] 278.801843317972npc
>
> convertUnit(unit(c(330e3, 331e3, 331e3, 330e3),
+ "native"), "npc")
[1] 506.912442396313npc 508.448540706605npc 508.448540706605npc
[4] 506.912442396313npc
>
Should you be looking for something in the interval 0 to 1?
Your best bet is to use an existing geom to draw the scale bar -
unfortunately ggplot2 doesn't use native scales because it's not
possible to support arbitrary coordinate systems with them.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
If the scalebar is black and white, you should only need two calls to
geom_rect (or rather geom_path i guess). Something like this perhaps,
library(ggplot2)
p = qplot(1:10,1:10)
## p
makeScaleBar <- function(x=8,y=2,width=2,N=4, ...){
dlong = data.frame(x=x - width/2,
xend = x + width/2,
y=y, yend=y)
dshort = data.frame(x=x - width/2 + seq(0, N-1,by=2)*width/N,
xend = x - width/2 + (1 + seq(0, N-1,by=2))*width/N,
y=y, yend=y)
list(geom_segment(data=dlong, aes(x=x,y=y,xend=xend,yend=yend),
colour="black", ...),
geom_segment(data=dshort, aes(x=x,y=y,xend=xend,yend=yend),
colour="white", ...))
}
p + makeScaleBar(N=5, size=5)
HTH,
baptiste
> --
> 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
>
I'm sorry but I don't understand what you mean. Would you have an
example of what you're trying to achieve?
On Sun, Nov 28, 2010 at 9:04 PM, Paul Hiemstra <p.h.hi...@gmail.com> wrote:
> Dear Baptiste,
>
> Thanks for the input. There is one problem however with your example,
> sticking geom's in a list and trying to add them to an existing ggplot does
> not work.
Works for me, admittedly on a dummy example.
> The function is probably going to look more like (in pseudo code):
>
>
> addScalebar = function(ggplot_obj, data_ggplot_was_based on) {
> extract coordinates for scale bar from data_ggplot_was_based on
> create list of geom's (path and text)
> for(geom in geoms) ggplot_obj = ggplot_obj + geom
> return(ggplot_obj)
> }
>
> bla = ggplot(aes(), bla_data)
> bla_withscalebar = addScalebar(bla, bla_data)
>
> When I'm done with the function, I'll report back to the list. Is there some
> kind
> of contributed code section for ggplot?
>
Either the wiki or the ggExtra package could be good places for
contributed code, I think.
I think the idea is to create a segmented scalebar that is in proportion
to the scaled real-world coordinates of the map and in arbitrary
scalebar units (ideally). Think in terms of a map of Italy with a scale
showing the measure of a kilometer or 100km.
The scalebar and the map should not interfere with each other (the
scalebar colors should not show up on a legend, for example). The
scalebar should also rescale if the data are zoomed (changed
presentation or data limits) and ideally change position and number of
scalebar segments.
The geom_polygon seems like it is a likely existing geom to start with.
It would have to be constructed according to the coordinates of the data
and the limits of the presentation. The polygon segments would be
adjusted according to the units of measure. The scalebar segments would
alternate color. The scalebar segments would not show up in the
legend. The scalebar would be anchored in space according to a
positioning specification, with a possible default given the extents of
the data on the map.
It seems that multiple viewports might separate the scalebar from the
map and get rid of conflicts. This has two problems: 1. not everyone
wants to deal with a plot with multiple viewports. 2. It may not be
possible to scale the scalebar viewport so that it has the scalebar at
the right proportions.
There seems to be a significant impedance mismatch with the notion of a
scalebar mixed with a gplot map. Maybe someone has some ideas, though.
It would be a worthwhile thing to accomplish.
I wonder if a graticule might be a more feasible option?
Mark
Could you explain in more detail how the scale bar is created? It's
my understanding that the length of (e.g.) 1km is not constant across
most maps.
> The scalebar and the map should not interfere with each other (the scalebar
> colors should not show up on a legend, for example). The scalebar should
> also rescale if the data are zoomed (changed presentation or data limits)
That should be trivial with geom_rect or geom_polygon.
> and ideally change position and number of scalebar segments.
That's a bit harder.