传奇问题

时间:2014-01-08 20:07:27

标签: r

我需要在不同的日子里创建许多不同船舶位置的地图。我想为不同的船只使用不同的颜色,但由于我每天都有不同的船只,我遇到了一些麻烦。

以下是使用标准绘图设施的R代码:

# Create vector with all the ship names
shipname <- df

# Read every daily files

myfiles <- list.files(pattern="*.dat")
myfilesContent <- lapply(myfiles, read.table, quote="\"")

# Create the color vector index

z <- as.numeric((unique(df.knownSH$ship))
range01 <- function(x)(x-min(x))/diff(range(x))
rainbow(7)
cRamp <- function(x){
  cols <- colorRamp(rainbow(7))(range01(x))
  apply(cols, 1, function(xt)rgb(xt[1], xt[2], xt[3], maxColorValue=255))
}  

for (i in 1:length(myfiles)){

  # Create pdf daily file
  colnames(myfilesContent[[i]]) <- c("YR","MO","DY","LON","LAT","SHIPNAME") 
  ncol(myfilesContent[[i]])
  date <- as.chron(as.Date(ISOdate(myfilesContent[[i]]$YR, myfilesContent[[i]]$MO,                                       
  myfilesContent[[i]]$DY)),format = "day mon year")
  epoch <-with(month.day.year(date), julian(month, day, year, origin = c(1, 0, yrmin)))
  pdf(paste0(epoch,".pdf"),width=20,height=14)

  # Plot
  plot(myfilesContent[[i]]$LON,myfilesContent[[i]]$LAT,main = unique(date), sub = NULL, xlab = "LON", ylab = "LAT", ylim=c(-90,90),xlim=c(-180,180),cex=1.5,cex.main=2,cex.axis=2,cex.lab=2.5, col =  cRamp(z), pch= 17)
  map(add=T,col="saddlebrown",interior = FALSE)

  # Legend
  legend("topleft", legend=c(unique(myfilesContent[[i]]$SHIPNAME)), col=cRamp(z), cex=1.5,pch=17,border = FALSE)
  dev.off()
 }

非常感谢!

0 个答案:

没有答案
相关问题