如何通过连接R中相邻位置的线的粗细或颜色在地图上可视化距离矩阵?

如何解决如何通过连接R中相邻位置的线的粗细或颜色在地图上可视化距离矩阵?

假设我有两个数据集:(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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?