1

我是 R 代码的新手。现在我在使用 R 代码绘制等高线图时遇到了一些麻烦。

我检查了 help(filled.contour) ,它告诉如果你想绘制轮廓,x,y 应该都是升序的。实际上,我随机收到数据,例如:

latitude, longitude, value
37.651098 140.725082 9519
37.650765 140.725248 9519
37.692738 140.749118 23600
37.692737 140.749118 9911
37.692695 140.749107 16591
37.692462 140.74902 6350
37.692442 140.749052 5507
37.692413 140.749148 5476
37.692383 140.74929 7069
37.692357 140.749398 6152
37.692377 140.749445 6170
37.692355 140.749587 7163
37.692298 140.749672 6831
37.692292 140.749787 6194
37.692283 140.749903 6696
37.692342 140.750007 8204
37.692585 140.750037 2872
37.692648 140.749948 3907
37.692655 140.749827 4891
37.692667 140.749687 4899

我怎样才能绘制轮廓图!?这是我的代码:

args <- commandArgs(trailingOnly = TRUE) 
data1 <- args[1]
outputDir <- args[2]
outputFig = paste(outputDir, "Cs13x.jpeg",sep="");
jpeg(file = outputFig, width = 800,height=600, pointsize=20)
pinkcol <- rgb(1,0.7,0.7)

gpsdata <- read.table(file=data1,sep=" ");
lat <- as.vector(gpsdata[,1]);
lon <- as.vector(gpsdata[,2]);
datas <- as.vector(gpsdata[,3]);
datas <- abs(datas)
#---Convert gpsdata into x,y coordinate---#
# Convert degree into value
lat_pi <- lat*pi/180;
lon_pi <- lon*pi/180;
# calculate the value into corresponding x,y coordinate
x = cos(lat_pi) * cos(lon_pi);
y = cos(lat_pi) * sin(lon_pi);
#----------#
dataMatrix = matrix(datas, nrow = length(datas), ncol=length(datas));
plot.new()
filled.contour(sort(x),sort(y, decreasing = TRUE),dataMatrix, col = rainbow(100),     main="Contour Figure of Cs13x"); (**WRONG HERE!!!**)
dev.off()

<--------------- 终点线------------>

4

1 回答 1

4

'akima' 包会做到这一点。它旨在处理不规则间隔的 z 值。前两点与其他点相距甚远,这使得整个数据集的结果看起来相当粗略,所以我省略了它们。

 require(akima)
 gps.interp <- with( gpsdata[-(1:2), ], interp(x=latitude, y=longitude, z=value))
 contour(gps.interp)

在此处输入图像描述

于 2013-01-09T07:30:15.247 回答