【问题标题】:Plot geographic coordinates alongside with multidimensional scaling coordinates in R在 R 中绘制地理坐标和多维缩放坐标
【发布时间】:2014-08-06 17:04:29
【问题描述】:

所描述的问题涉及将多维缩放 (MDS) 坐标与真实数据点(城市的经度和纬度)一起表示。

所以,我有一个距离矩阵,上传到here。矩阵中的条目表示九个美国城市之间的距离(以英里为单位)。该矩阵输入到 MDS。 MDS 生成一组我希望在地图上绘制的坐标。

让我们先画出美国的地图。 (下图贴。)

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()

现在是 MDS 部分:

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)

拟合的坐标存储在fit$points对象中:

> 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

我的问题:如何缩放这些点以将它们添加到我的地图中。任何指针将不胜感激。

【问题讨论】:

标签: r ggplot2 geolocation geographic-distance multi-dimensional-scaling


【解决方案1】:

我粘贴我自己的解决方案。我们需要通过 mean 和 st 重新调整计算机 MDS 坐标。实际经纬度值的偏差:

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)

(对于rescale() 函数加载psych 库。)结果如下。

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    • 2014-06-01
    • 2023-01-17
    • 1970-01-01
    相关资源
    最近更新 更多