-3

Can you explain this code?

What is the use of x1 and x2

I want to create contour from this code.

please explain the use of interp().

how can this code be helpful in plotting the contour?

we have text file in which we have three separate columns with lat long. thow to use interp()?

f=read.table("data2_input.txt",sep="\t",header=T)
attach(f)
library(akima)
library(reshape2)
c1=length(f[1,])
r1=length(f[,1])
lat=0,long=0,conc=0,x1=0 y1=0 midx=0 x2=0 y2=0 midy=0 
conc=0 r=r1                            
c=c1
lat=f[,1]
long=f[,2]
conc=f[,3]
ak=interp(long,lat,conc, xo=seq(min(long),max(long),length=200),yo=seq(min(lat),max(lat),length=75))
mak=melt(ak$z)
names(mak)=c('x','y','value')
mak$lon=ak$x[mak$x]
mak$lat=ak$y[mak$y]
lon_diff=round(ak$x[2]-ak$x[1],5)
lat_diff=round(ak$y[2]-ak$y[1],5)
mak1=mak[,3:5]
mak1=subset(mak1,mak1$value!='NA')
names(mak1)=c('Concentration','Longitude','Latitude')

/* why use x1 and x2*/
x1=round(mak1$Latitude-lat_diff/2,4)
y1=round(mak1$Longitude-lon_diff/2,4)
x2=round(mak1$Latitude+lat_diff/2,4)
y2=round(mak1$Longitude+lon_diff/2,4)
/*why use round function*/          
z1=as.data.frame(cbind(x1,y1,x2,y2,round(mak1$Latitude,4),round(mak1$Longitude,4),round(mak1$Concentration,4)))
names(z1)=c('latitude_x1','longitude_y1','latitude_x2','longitude_y2','midx','midy','dvalue')
write.csv(z1,'data2_output.csv',row.names=F,quote=F)
James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    I'm quite sure this question won't live long in here... SO is not "dump code here and ask someone to do my work" kind of place. You need to ask specific questions, and also show that you have actually done something. Repeating same thing multiple times or just "please suggest" isn't helping you. – James Z Aug 28 '18 at 14:55
  • Also, "Sir". Are you saying women can't answer to your questions? – James Z Aug 28 '18 at 14:56
  • 1
    @JamesZ flag the post – Bear Aug 28 '18 at 15:30

1 Answers1

0

Welcome to Stack Overflow and thanks for your question (however the comments posted are still relevant :)

  • x1,y1 and x2,y2 are the vectors of coordinates of the bottom-right and top-left vertices of rectangles you are going to make;
  • your spatial data on concentration are distributed irregularly on the surface so if you were able to draw it you will see a lot of blank spaces, disruptions and irregularities on the map. Your contour and underlying image would be unreadable and distorted.
  • to avoid it you should interpolate the data (or put the data on the regular grid) so here is why you need use interp function of akima package.
  • the extreme example (however for ggplot2 package) of not doing interpolation/smoothing you can see at Contour Plot is not Completly Filled
  • base countour plot is not possible to call per se for irregular data as the matrix of regularly distributed z-values should be provided as an argument.
Artem
  • 3,304
  • 3
  • 18
  • 41