1

I'm trying to create this filled contour plot for a project. I want the x and y axis to go from 0 to 1, incrementing at every .2, and the color to go from yellow at -1 and red at 1.

I've read in the .dat file and set it to a matrix that I can use for z.

For some reason when I run this I'm getting the error "insufficient 'x' or 'y' values".

Here's my short code:

C <- read.table("C:/Users/ken/Desktop/stats.dat")
df = data.matrix(C)

x <- y <- seq(0,1, by=7)

filled.contour(x, y, df, colors.palette = colorRampPalette(c("yellow", "red")))

Edited to include data

Here is the data:

structure(c(0.8798, -0.9344, 0.4009, 0.8743, 0.2772, 0, 0.2772, 
0.8743, 0.4009, -0.9344, 0.8798, -0.9344, 0.9923, -0.4257, -0.9285, 
-0.2944, 0, -0.2944, -0.9285, -0.4257, 0.9923, -0.9344, 0.4009, 
-0.4257, 0.1827, 0.3983, 0.1263, 0, 0.1263, 0.3983, 0.1827, -0.4257, 
0.4009, 0.8743, -0.9285, 0.3983, 0.8687, 0.2754, 0, 0.2754, 0.8687, 
0.3983, -0.9285, 0.8743, 0.2772, -0.2944, 0.1263, 0.2754, 0.0873, 
0, 0.0873, 0.2754, 0.1263, -0.2944, 0.2772, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.2772, -0.2944, 0.1263, 0.2754, 0.0873, 0, 0.0873, 
0.2754, 0.1263, -0.2944, 0.2772, 0.8743, -0.9285, 0.3983, 0.8687, 
0.2754, 0, 0.2754, 0.8687, 0.3983, -0.9285, 0.8743, 0.4009, -0.4257, 
0.1827, 0.3983, 0.1263, 0, 0.1263, 0.3983, 0.1827, -0.4257, 0.4009, 
-0.9344, 0.9923, -0.4257, -0.9285, -0.2944, 0, -0.2944, -0.9285, 
-0.4257, 0.9923, -0.9344, 0.8798, -0.9344, 0.4009, 0.8743, 0.2772, 
0, 0.2772, 0.8743, 0.4009, -0.9344, 0.8798), .Dim = c(11L, 11L
), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6", 
"V7", "V8", "V9", "V10", "V11")))
Spacedman
  • 92,590
  • 12
  • 140
  • 224
r2333
  • 103
  • 1
  • 4
  • Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap May 04 '16 at 22:32
  • Thanks a lot, I'll do that now – r2333 May 04 '16 at 22:35

1 Answers1

1

For filled.contour() with vector arguments x and y and matrix argument z, x must have length equal to the number of rows of z and y must have length equal to the number of columns of z. Your z has 11 rows and 11 columns, so your x and y must both have length 11.

Your call to seq() asks for a sequence starting with 0, ending with 1, with a spacing of 7. The interval 0 to 1 is less than 7, so R just returns a zero.

Replace your call to seq() with the following

x = y = seq(from=0,to=1,length.out=11)

The argument length.out tells seq() the desired length of the output vector(s). The following should work.

C <- structure(c(0.8798, -0.9344, 0.4009, 0.8743, 0.2772, 0, 0.2772, 0.8743, 0.4009, -0.9344, 0.8798, -0.9344, 0.9923, -0.4257, -0.9285, -0.2944, 0, -0.2944, -0.9285, -0.4257, 0.9923, -0.9344, 0.4009, -0.4257, 0.1827, 0.3983, 0.1263, 0, 0.1263, 0.3983, 0.1827, -0.4257, 0.4009, 0.8743, -0.9285, 0.3983, 0.8687, 0.2754, 0, 0.2754, 0.8687, 0.3983, -0.9285, 0.8743, 0.2772, -0.2944, 0.1263, 0.2754, 0.0873, 0, 0.0873, 0.2754, 0.1263, -0.2944, 0.2772, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2772, -0.2944, 0.1263, 0.2754, 0.0873, 0, 0.0873, 0.2754, 0.1263, -0.2944, 0.2772, 0.8743, -0.9285, 0.3983, 0.8687, 0.2754, 0, 0.2754, 0.8687, 0.3983, -0.9285, 0.8743, 0.4009, -0.4257, 0.1827, 0.3983, 0.1263, 0, 0.1263, 0.3983, 0.1827, -0.4257, 0.4009, -0.9344, 0.9923, -0.4257, -0.9285, -0.2944, 0, -0.2944, -0.9285, -0.4257, 0.9923, -0.9344, 0.8798, -0.9344, 0.4009, 0.8743, 0.2772, 0, 0.2772, 0.8743, 0.4009, -0.9344, 0.8798), .Dim = c(11L, 11L), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11")))

df = data.matrix(C)
x = y = seq(from=0,to=1,length.out=11)
filled.contour(x, y, z=df)
  • This works perfectly, thank you! Could you tell me how to get the colorRamp to display from yellow to red? – r2333 May 05 '16 at 00:01
  • That is beyond the scope of the question as you've asked it here, so it's best asked as a new question. – Matt Bowers May 05 '16 at 00:15