[R-sig-Geo] Leader lines for inset map
Rohan Sadler
ropo.sadler at gmail.com
Mon Nov 3 12:45:53 CET 2014
Hi Vladimir,
Cheap and nasty says add lines, but first define where you want to put your
insets explicitly by specifying x,y for the subplots. modified code below
(you will want to move your xy values around slightly to suit).
library(TeachingDemos)
library(maptools)
data(wrld_simpl)
plot(wrld_simpl,border=NA,col='blue',axes=TRUE, xlim=c(100,130),
ylim=c(-40,30))
tmp <- subplot(
plot(wrld_simpl,border=NA,col='green',bg='white',axes=FALSE),
y=c(-40,-25),x=c(75,110),inset=c(0.01,0.01))
tmp2 <- par('usr')
tmp3 <- subplot(
plot(wrld_simpl, border='red', axes=TRUE, xlim=c(95,105),
ylim=c(-5,5), bg='lightgrey'), y=c(-5,30),x=c(120,160))
par(xpd=TRUE) # reset clipping region
rect( tmp3$usr[1], tmp3$usr[3], tmp3$usr[2], tmp3$usr[4],
border='orange' )
lines(c(tmp3$usr[1],120),c(tmp3$usr[4],30),lwd=2)
lines(c(tmp3$usr[2],120),c(tmp3$usr[3],-5),lwd=2)
op <- par(tmp[c('plt','usr')])
rect( tmp2[1], tmp2[3], tmp2[2], tmp2[4], border='red' )
par(op)
I've added in a ggmap equivalent, which gives close to publication quality
graphic, pulling a map from googlemaps (you may want imagery instead). All
based on Kahle & Wickham's excellent paper. Note that this example is
fudged in that the inset is a zoom out rather than a zoom in; and I could
waste a lot of time getting rid of the second 'accidental' legend. Go for
something other than the googlemaps API if you wish to publish something
similar with ggmap. Depending on one's level of abstraction, it still
requires you to add lines ... :)
(assuming I have understood your question right!!)
Regards
Rohan
library(ggmap)
# Source: http://journal.r-project.org/archive/2013-1/kahle-wickham.pdf
# violent crimes in houston, texas
violent_crimes <- subset(crime,
offense != "auto theft" & offense != "theft" & offense != "burglary")
# order violent crimes
violent_crimes$offense <- factor(violent_crimes$offense,
levels = c("robbery", "aggravated assault", "rape", "murder"))
# restrict to downtown
violent_crimes <- subset(violent_crimes,
-95.39681 <= lon & lon <= -95.34188 &
29.73631 <= lat & lat <= 29.78400)
# arbitrary area of interst
AOI<- data.frame(x=c(-95.38,-95.38,-95.385,-95.385,-95.38),
y=c(29.74,29.745,29.745,29.74,29.74))
AOIConnectors<- data.frame(x=c(-95.38,-95.35836,-95.38,-95.35836),
y=c(29.74,29.73631,29.745,29.75062),seg=rep(1:2,each=2))
# googlemaps api
houston <- get_map("houston", zoom = 14)
# generate your graphics layers
HoustonMap <- ggmap(houston, extent = "device", legend = "topleft")
overlay <- stat_density2d(
aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
bins = 4, geom = "polygon",
data = violent_crimes
)
# construct your image
HoustonMap + overlay + inset(
grob = ggplotGrob(ggplot() + overlay + theme_inset()),
xmin = -95.35836, xmax = Inf, ymin = -Inf, ymax = 29.75062)+
geom_line(aes(x,y,group=seg),data=AOIConnectors)+
geom_path(aes(x,y),data=AOI)
On 3 November 2014 18:22, Vladimir <sendmeoffer at gmail.com> wrote:
> Hello!
>
> The minimal example is
> ===== (source
>
> http://r-sig-geo.2731867.n2.nabble.com/making-an-inset-in-a-map-figure-tp7584585p7584618.html
> )
>
> |library(TeachingDemos)
> library(maptools)
> |
>
> ||
> |data(wrld_simpl)
>
> plot(wrld_simpl,border=NA,col='blue',axes=TRUE, xlim=c(100,130),
> ylim=c(-40,30))
>
> tmp <- subplot(
> plot(wrld_simpl,border=NA,col='green',bg='white',axes=FALSE),
> 'bottomleft', size=c(2,1), inset=c(0.01,0.01))
>
> tmp2 <- par('usr')
>
> tmp3 <- subplot(
> plot(wrld_simpl, border='red', axes=TRUE, xlim=c(95,105),
> ylim=c(-5,5), bg='lightgrey'), 'topright', size=c(2,2))
>
> par(xpd=TRUE) # reset clipping region
> rect( tmp3$usr[1], tmp3$usr[3], tmp3$usr[2], tmp3$usr[4],
> border='orange' )
>
> op <- par(tmp[c('plt','usr')])
> rect( tmp2[1], tmp2[3], tmp2[2], tmp2[4], border='red' )
> par(op)
> |
>
> =====
>
> I want to connect orange rectangle with topright inset map.
>
> All the best,
> Vladimir.
>
>
> On 03/11/14 08:53, Barry Rowlingson wrote:
> >
> > I don't think you'll get a complete solution from this mailing list
> > until you tell us how you are drawing your maps in the first place.
> >
> > Are you using base graphics plotting, or ggplot, or ggmap, or lattice?
> > Could you throw us a few lines of code that show how you make your
> > base and insert maps? Otherwise you're going to get a bunch of "just
> > draw lines" answers...
> >
> > On 1 Nov 2014 20:07, "Vladimir" <sendmeoffer at gmail.com
> > <mailto:sendmeoffer at gmail.com>> wrote:
> >
> > Hello list!
> >
> > I would like to add leader lines to my inset maps. These lines
> connect
> > the frame of the inset map with the corresponding area on the main
> > map.
> > Good examples are here [1], [2], or here [3].
> >
> > What is the most convenient R approach to do so?
> >
> > Kind regards,
> > Vladimir.
> >
> > ---
> > [1]
> >
> http://gce-lter.marsci.uga.edu/public/images/gce_study_area_blowup_nov07_rev.jpg
> > [2] http://pubs.usgs.gov/fs/fs08203/images/study_area.gif
> > [3]
> >
> http://www.innovativegis.com/basis/supplements/bm_dec_02/Ironing_Colorado_files/image004.jpg
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo at r-project.org <mailto:R-sig-Geo at r-project.org>
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Senior Scientist
Astron Environmental Services Pty. Ltd.
rohan.sadler at astron.com.au
Adjunct Senior Lecturer
School of Agricultural and Resource Economics, M089
The University of Western Australia
Office: +61 8 94219684
Mobile: 0433 192 600
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list