nncross, geom_segment and arrows

17 views
Skip to first unread message

Michel Barbosa

unread,
Mar 23, 2009, 8:22:46 PM3/23/09
to ggplot2
Dear list,

I've successfully applied a nearest neighbour analysis on 2 datasets
using Spatstat's nncross method to find the nearest neighbour in Y of
each point in X, given two point datasets X and Y. For my research, X
is the restaurants dataset and Y is the ATM dataset.

For each restaurant, I can calculate the nearest ATM. However, I'd
like to graphically pinpoint this on the map using arrows. Although,
I've come across two other posts in which the use of arrows is
discussed. I couldn't tell whether it's possible with ggplot2 to draw
arrows from the location of a restaurant at (lng, lat) to the location
of the nearest ATM (long, lat). Is is possible to draw arrows from one
coordinate to another, if so, could you supply me with some directions
on how to do so?

I've been experimenting with a base plot with data = X. On top of
this, a geom_segment layer is used. I'm stuck at getting the arrow
connect to the coordinates from data frame X and dataframe Y.

http://img.skitch.com/20090324-qqfixastpsgbfefd7wscqnt8uk.jpg

From the picture above, I'd like to connect the pinpoints to the
nearest dollar sign.

Thanks for all your help,

Kind regards,

Michel

hadley wickham

unread,
Mar 24, 2009, 5:24:35 PM3/24/09
to Michel Barbosa, ggplot2
Hi Michel,

It's a little hard to suggest a solution without seeing the data, but
I suspect you could use merge to create a dataset that connects each
restaurant with the closest atm, and then use geom_segment to plot
that data.

Hadley
--
http://had.co.nz/

Michel Barbosa

unread,
Mar 24, 2009, 7:48:17 PM3/24/09
to ggplot2
Dear Hadley,

the datasets are as follows:

info_restaurants <- data.frame(name_restaurant, lng_restaurant,
lat_restaurant)

info_atms <- data.frame(name_atm, lng_atm, lat_atm)

Using the nncross method, I get the indices of the nearest ATMS as
well as the distance:

For example:

dist which
1 0.0028948962 4
2 0.0008424542 3

So I have the locations of the restaurants and the atms, as well as
which atms are nearest to each restaurant. How do you suggest I merge
these two?

Kind regards,

Michel

Michel Barbosa

unread,
Mar 24, 2009, 8:24:06 PM3/24/09
to ggplot2
Dear Hadley,

I've got it working.

pattern_restaurants <- ppp(info_restaurants$lng, info_restaurants$lat,
marks = info_restaurants$kitchen_type, window = owin(x_bounds,
y_bounds))
pattern_atms <- ppp(info_atms$lng, info_atms$atm_lat, marks = info_atms
$name, window = owin(x_bounds, y_bounds))

lng_atm <- c()
lat_atm <- c()

crossdistances <- nncross(pattern, pattern_atms)

for(i in 1:length(restaurants)) {
lng_atm <- append(lng_atm, atm_info$atm_lng[crossdistances$which
[i]])
lat_atm <- append(lat_atm, atm_info$atm_lat[crossdistances$which
[i]])
}

positions <- data.frame(info$lng, info$lat, lng_atm, lat_atm)

# Setup base plot

myplot <- ggplot(data = info, aes(x = lng, y = lat))

# Add layer

myplot <- myplot + geom_segment(aes(x = lng, y = lat, xend = lng_atm,
yend = lat_atm, colour = '#FF0000', size=1), arrow=arrow(length=unit
(0.35,"cm"))) +
coord_cartesian(xlim=x_bounds, ylim=y_bounds)

# Control margins

myplot <- myplot + no_margins


This gives me:

http://img.skitch.com/20090325-xgmsy49xqysghfxaih7s7e3uam.png

I guess I've misinterpreted the 'xend' and 'yend' parameters of
geom_segment.

Thanks for the eye opener :)

Cheers,

Michel

On 24 mrt, 22:24, hadley wickham <h.wick...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages