Ilik
Ilik

Reputation: 165

Custom legend in fille.contour3

I am trying to use a costume legend with filled.contour3, like in this example:

colorRampPalette(c('white','blue','green','yellow','red','darkmagenta','darkviolet'))(15)

When I use filled.contour3 with the predefined legend (e.g., terrain.colors) it works fine. But if I try to add a costume legend (e.g., terrain.colors(10)) I get an error message:

Error in .filled.contour(as.double(x), as.double(y), z, as.double(levels),  : 
could not find function "color.palette"

Here is a sample code:

#Generate some fake data
x = rep(c(10,11,12),length = 9)
y = rep(c(1,2,3),each = 3)
z = runif(n=9,min = 0,max = 1)
xcoords = unique(x)
ycoords = unique(y)
surface.matrix = matrix(z,nrow=length(xcoords),ncol=length(ycoords),byrow=T)

#this works:
filled.contour3(xcoords,ycoords,surface.matrix,color=terrain.colors,xlab = "",ylab = "",xlim = c(min(xcoords),max(xcoords)),ylim = c(min(ycoords),max(ycoords)),zlim = c(min(surface.matrix),max(surface.matrix)))

#this doesn't work:
filled.contour3(xcoords,ycoords,surface.matrix,color=terrain.colors(10),xlab = "",ylab = "",xlim = c(min(xcoords),max(xcoords)),ylim = c(min(ycoords),max(ycoords)),zlim = c(min(surface.matrix),max(surface.matrix)))

I use R 3.0.3 on Win7 64 Thanks ,Ilik

Upvotes: 1

Views: 584

Answers (1)

koekenbakker
koekenbakker

Reputation: 3604

?filled.contour has two options to set color:

color.palette: a color palette function to be used to assign colors in the plot.

col: an explicit set of colors to be used in the plot. This argument overrides any palette function specification. There should be one less color than levels

You're supplying a vector of colors to color, which is interpreted as color.palette. If you want to supply a predefined vector of colors, use col instead:

cols = colorRampPalette(c('white','blue','green','yellow','red','darkmagenta','darkviolet'))(15)
filled.contour(xcoords,ycoords,surface.matrix,col=cols,xlab = "",ylab = "",xlim = c(min(xcoords),max(xcoords)),ylim = c(min(ycoords),max(ycoords)),zlim = c(min(surface.matrix),max(surface.matrix)))

Upvotes: 1

Related Questions