问题描述
假设我有两个数据集:(1)一个数据框:位置坐标,每个都有ID; (2)语言距离矩阵,该矩阵反映了这些地区之间的语言距离。
# My data are similar to this structure
# dataframe
id <- c("A","B","C","D","E")
x_coor <- c(0.5,1,1.5,2)
y_coor <- c(5.5,3,7,6.5,5)
my.data <- data.frame(id = id,x_coor = x_coor,y_coor = y_coor)
# linguistic distance matrix
A B C D
B 308.298557
C 592.555483 284.256926
D 141.421356 449.719913 733.976839
E 591.141269 282.842712 1.414214 732.562625
现在,我想通过连接R中相邻位置的线的粗细或颜色来可视化地图上每两个站点之间的语言距离。
就像这样: enter image description here
我的想法是通过R中的deldir或tripack包生成delaunay三角剖分。
# generate delaunay triangulation
library(deldir)
de=deldir(my.data$x_coor,my.data$y_coor)
plot.deldir(de,wlines="triang",col='blue',wpoints = "real",cex = 0.1)
text(my.data$x_coor,my.data$y_coor,my.data$id)
这是情节: enter image description here
我的问题是如何通过三角形边缘的粗细或颜色反映语言距离?还有其他更好的方法吗?
非常感谢您!
解决方法
您想要做的关于线宽的操作可以“很公平地完成 轻松”通过deldir程序包。您只需使用以下命令调用plot.deldir() 适当的“ lw”(线宽)值。
此答案的底部是一个演示脚本“ demo.txt”,该脚本演示在您的示例情况下如何执行此操作。特别是该脚本显示 如何从“语言距离”获得适当的lw值 矩阵”。我不得不对该矩阵的处理方式进行一些调整 提出了。即我不得不将其转换为适当的矩阵。
我将距离重新调整为介于0和10之间,以获得 线宽的相应值。您可能希望以其他方式重新缩放。
关于颜色,有两个问题:
(1)完全不清楚您要如何映射“语言” 距离”。
(2)不幸的是,plot.deldir()的代码是用非常 笨拙的方式,segments()的“ col”参数不能为 以与“ lw”参数相同的方式适当地传递。 (我很早以前就编写了plot.deldir()代码,当时我对它的了解还很少。 R编程比我现在知道的要多! :-))
我将调整此代码,并将deldir的新版本提交给CRAN 很快。
#
# Demo script
#
# Present the linguistic distances in a useable way.
vldm <- c(308.298557,592.555483,284.256926,141.421356,449.719913,733.976839,591.141269,282.842712,1.414214,732.562625)
ldm <- matrix(nrow=5,ncol=5)
ldm[row(ldm) > col(ldm)] <- vldm
ldm[row(ldm) <= col(ldm)] <- 0
ldm <- (ldm + t(ldm))/2
rownames(ldm) <- LETTERS[1:5]
colnames(ldm) <- LETTERS[1:5]
# Set up the example data. It makes life much simpler if
# you denote the "x" and "y" coordinates by "x" and "y"!!!
id <- c("A","B","C","D","E")
x_coor <- c(0.5,1,1.5,2)
y_coor <- c(5.5,3,7,6.5,5)
# Eschew nomenclature like "my.data". Such nomenclature
# is Micro$oft-ese and is an abomination!!!
demoDat <- data.frame(id = id,x = x_coor,y = y_coor)
# Form the triangulation/tessellation.
library(deldir)
dxy <- deldir(demoDat)
# Plot the triangulation with line widths proportional
# to "linguistic distances". Note that plot.deldir() is
# a *method* for plot,so you do not have to (and shouldn't)
# type the ".deldir" in the plotting command.
plot(dxy,col=0) # This,and plotting with "add=TRUE" below,is
# a kludge to dodge around spurious warnings.
ind <- as.matrix(dxy$delsgs[,c("ind1","ind2")])
lwv <- ldm[ind]
lwv <- 10*lwv/max(lwv)
plot(dxy,wlines="triang",col='grey',wpoints="none",lw=10*lwv/max(lwv),add=TRUE)
with(demoDat,text(x,y,id,col="red",cex=1.5))