rggplot2geolocationgeographic-distancemulti-dimensional-scaling

Plot geographic coordinates alongside with multidimensional scaling coordinates in R


The problem described deals with representing multidimensional scaling (MDS) coordinates alongside with real data points (longitude and latitute of cities).

So, I have a distance matrix, which is uploaded here. The entries in the matrix represent distances (in miles) between nine US cities. This matrix is input to MDS. MDS produce a set of coordinates which I wish to plot on the map.

Lets first draw the map of US. (The figure is pasted below.)

library(ggplot2)
library(maps)
library(ggmap)

# Build dataframe for plotting map of USA
states <- map_data("state")
geo.city <-  c("Atlanta", "Boston", "Dallas", "Indianapolis", "Los Angeles", "Memphis", "St. Louis", "Spokane", "Tempa")
geo.codes <- geocode(geo.city)
geo.data <- data.frame(name = geo.city, long = geo.codes[, 1], lat = geo.codes[, 2])
# Plot map
ggplot(mapstates, aes(long, lat, group = group)) +
    geom_polygon(fill = I("grey85")) +
    geom_path(color = "gray") +
    coord_map(project="globular") +
    xlab("Longitude") + ylab("Latitude") +
    annotate("point", x = geo.data$long, y = geo.data$lat) +
    #annotate("point", x = fit$points[,1], y = fit$points[, 2]) +
    annotate("text", x = geo.data$long, y = geo.data$lat + 0.7, label = geo.data$name, size = 3) +
    theme_bw()

Figure-1

And now the MDS part:

my.data <- read.table(file = "https://dl.dropboxusercontent.com/u/540963/airline_distances.csv", header = TRUE, sep = ",", row.names = 1)
my.mat <- as.matrix(my.data)
D <- dist(my.mat)
fit <- cmdscale(d = D, k = 2)

The fitted coordinates are stored in fit$points object:

> fit$points
                   [,1]        [,2]
Atlanta      -1563.1298   -89.67381
Boston        -780.5404  2208.74325
Dallas        -202.2759 -1053.70443
Indianapolis -1198.6748  -181.96331
Los Angeles   3445.3295  -412.50061
Memphis      -1171.9280  -793.69762
St. Louis    -1018.1581  -622.13715
Spokane       3712.7007   438.84657
Tampa        -1223.3233   506.08712

My question: how to scale this points to add them to my map. Any pointers would be greatly appreciated.


Solution

  • I paste my own solution. We need to rescale computer MDS coordinates by mean and st. deviation of real longitude and latitude values:

    fit <- cmdscale(D, eig = TRUE, k = 2)
    my.mean <- apply(geo.codes, 2, mean)
    my.sd <- apply(geo.codes, 2, sd)
    city.loc <- fit$points
    city.loc[, 1] <- -city.loc[, 1]
    city.loc <- rescale(city.loc, my.mean, my.sd)
    

    (For rescale() function load psych library.) The result is then as follows. enter image description here