Skip to content
Prev 21937 / 29559 Next

Leader lines for inset map

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: