filled.contour绘制栅格图

今天帮一个同学做了几个图,用到filled.contour这个函数,在一个有点傻的地方耗费了我大量精力。最后保存作图的时候如果保存成pdf 格式,图像会有白色格子和等高线印记,试了半天也没去掉,后来直接保存成TIFF就没有,真是fuck 了。因为是别人未发表的数据,所以这里只放图的一角。

pdf保存
TIFF保存

另外,两个图还是有点区别的。pdf图中的颜色是用colorRampPalette(c("black","blue", "green","yellow","red"), bias=2)(500)生成的,TIFF中的颜色是把rainbow改了一下。完整代码贴一下:

m <- read.csv("10.csv",header = F)

m11 <- m[,c(1,2,intersect(which(m[1,]>=1580),which(m[1,]<=1650)))]

colnames(m11) <- m11[1,];m1 <- m11[-1,]

n <- matrix(NA,nrow(m1),2)

n1 <- cbind(m1,n)

x <- ncol(m1);y <- x+1;z <- x+2

for(i in 1:nrow(m1)){

  n1[i,y] <- max(n1[i,3:x])

  n1[i,z] <- mean(as.numeric(n1[i,3:x]))

}

n3 <- n1[,c(1,2,y,z)]

#n3 <- n3[-which(n3[,1]<=-41),];n3 <- n3[-which(n3[,1]>=40),]

range(n3[,1]);range(n3[,2]);range(n3[,4]);plot(n3[,4])

n3[which(n3[,4]>=500),4] <- 500

mx <- unique(n3[,1]);my <- unique(n3[,2])

volcano <- matrix(NA,length(mx),length(my))

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

  for(j in 1:length(my)){

    a <- which(n3[,1]==mx[i])

    b <- which(n3[,2]==my[j])

    c <- intersect(a,b)

    if(length(c)>0) volcano[i,j] <- n3[c,4]

  }

}

#require(grDevices) # for colours

#mycolors <- colorRampPalette(c("black","blue", "green","yellow","red"), bias=2)(500)

#barplot(rep(1,times=100),col=mycolors,border=mycolors,axes=FALSE); box()

#RB <- colorRampPalette(colors = mycolors)

#ccol=rainbow(n=6,start=0,end=5/6)

rabw <- function (n, s = 1, v = 1, start = 0, end = (17/24)*(max(1, n - 1)/n),

                  alpha = 1)

{

  if ((n <- as.integer(n[1L])) > 0) {

    if (start == end || any(c(start, end) < 0) || any(c(start,

                                                        end) > 1))

      stop("'start' and 'end' must be distinct and in [0, 1].")

    rev(hsv(h = seq.int(start, ifelse(start > end, 1, 0) + end,

                        length.out = n)%%1, s, v, alpha))

  }

  else character()

}

#pdf("0.03_2.pdf",10,10)

tiff("10_1.tiff",width = 1000, height = 1000)

#x <- seq(0, 1, length.out = nrow(volcano))

#y <- seq(0, 1, length.out =ncol(volcano))

x <- 10*1:nrow(volcano);y <- 10*1:ncol(volcano)

filled.contour(x, y, volcano, color = rabw,axes=F,nlevels = 100)

dev.off()


P.S. 理解rainbow颜色函数的参数说明

了解了rainbow()的基本属性,那如何使用呢?来看一个实例,在该例中会产生由赤、橙、黄、绿、青、蓝、紫7色依次合成6组颜色,每组包含6个渐变色。

barplot(rep(1,41),  

       ccol=c(rainbow(n=6,start=0,end=1/6),  

             "white",  

             rainbow(n=6,start=1/6,end=2/6),  

             "white",  

             rainbow(n=6,start=2/6,end=3/6),  

             "white",  

             rainbow(n=6,start=3/6,end=4/6),  

             "white",  

             rainbow(n=6,start=4/6,end=5/6),  

             "white",  

             rainbow(n=6,start=5/6,end=1)))  

x.text<-c("赤色","黄色","绿色","青色","蓝色","紫色")  

axis(side=1,at=seq(from=3,length.out=6,by=8.5),labels =x.text,cex=0.75) 

你可能感兴趣的:(filled.contour绘制栅格图)