1

I am a green-hand on R code. Now I meet some trouble in plotting contour figure by using R code.

I have checked help(filled.contour) which tells that if you want to plot the contour, x,y should be both in ascending order. Actually, I receive the data randomly, like:

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

How can I plot the contour figure!? Here is my code:

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

<-------------- FINISH LINE ----------->

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
DavidBear
  • 35
  • 7
  • Are your data on a regular grid? – mnel Jan 09 '13 at 05:28
  • To mnel: nope. But I think the problem is the filled.contour function since it requires x,y in ascending order... – DavidBear Jan 09 '13 at 05:46
  • You problem is more that the filled.contour function isn't appropriate for your data. Can you post a reasonable reproducible example? – mnel Jan 09 '13 at 05:50
  • To mnel: I have upload my code here, please help me, thx~~ – DavidBear Jan 09 '13 at 06:10
  • That is not a reproducible example. I would look at the automap package. You need to interpolate somehow, and automap will make some sensible assumptions and automatically map it. – mnel Jan 09 '13 at 06:21
  • To mnel: what is the reproducible example? Anyway, thx very much, I will check the automap package first... – DavidBear Jan 09 '13 at 06:42
  • @DavidBear: A reproducible example would be enough data with which we could do something useful. In this case it might be the output of `dput(head(gpsdata, 30))` – IRTFM Jan 09 '13 at 06:47
  • @DWin I have added some data to the problem, is it enough? – DavidBear Jan 09 '13 at 07:10
  • @DavidBear evidently it is enough, but next time take his advice and use `dput()` so that someone else can copy/paste your data into R. – Gregor Thomas Jan 09 '13 at 08:05
  • 2
    Tips on how to make a reproducible example: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Roman Luštrik Jan 09 '13 at 09:04
  • @shujaa ok, learn from now, ^^ ,thx for the advice – DavidBear Jan 10 '13 at 01:26
  • @DavidBear I never would have learned it either except for Roman's reproducible example link. It's amazing how much quicker and better you can get help on SO by making it *really easy* for people to help you! – Gregor Thomas Jan 10 '13 at 03:46

1 Answers1

4

The 'akima' package will do it. It is designed to handle irregularly spaced z values. The first two points were widely separated from the rest and that made the results from the whole dataset look rather sketchy, so I omitted them.

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

enter image description here

IRTFM
  • 258,963
  • 21
  • 364
  • 487