1

I trying to make a filled contour plot, but seem to have some issues doing so.

I firstly tried to do it with ´plotly´which worked without any problems,I ran into problems, as i had to save the images. Now i am trying some other way.

This is function that outputs the data matrix

knn_tester <-function(max_smooth, maxK){
  output =  data.frame(k=numeric((maxK*max_smooth/2)-1), error =numeric((maxK*max_smooth/2)-1), kernel_size=numeric((maxK*max_smooth/2)-1), stringsAsFactors = FALSE)
  #output = as.data.frame(matrix(ncol= 3))
  #output = output[-1,]
  number = 0
  for(smooth in 1:max_smooth){
     if(smooth%%2 != 0){
      message("Smoothing level: " , smooth)
      fullDataList[[1]] = loadSinglePersonsData(DPI,2,1,smooth); gc(); 
      fullDataList[[2]] = loadSinglePersonsData(DPI,2,2,smooth); gc(); 
      data<- dataset_extracter(fullDataList)
      # data[1] = training
      # data[2] = trainClass
      # data[3] = testing
      # data[4] = testClass
      for(i in 1:maxK){
        #print(data)
        message("iteration: " , i)
        output$kernel_size[number]= smooth
            #z = smooth
            output$k[number]=i
        #x = i
        predicted = knn(data$training,data[3]$testing,data[2]$trainClass,k = 1)
            #message("done")
            Error = mean(predicted == data[4]$testClass)
        output$error[number] = Error
        #y = Error
        #print(z)
        #print(x)
        #print(y)
        #rbind(output,data.frame(z,x,y))
        #print(output)
        number = number + 1
      }
    }
  }
  return(output)
}

output =  knn_tester(12,10)

When i then tries to plot it:

filled.contour3(output)

I get this error

error in plot.window(xlim = c(0,1), ylim = range(levels), xaxs ="i", :
need finite 'ylim' values:
in addition: warning messages
1: in min(x,na.rm = na.rm):  no non-missing arguments to min; returning Inf
2: in max(x,na.rm = na.rm):  no non-missing argument to max;  returning -Inf
3: in min(x): no non-missing arguments to min; returning Inf
4: in max(x): no non-missing arguments to max; returning -Inf

I am not sure why i get this error, as it without any problems with plotly... So I am not sure why this is happening?

`str(ouput)` 

'data.frame':   59 obs. of  3 variables:
  $ k          : num  2 3 4 5 6 7 8 9 10 1 ...
$ error      : num  0.226 0.226 0.226 0.226 0.226 ...
$ kernel_size: num  1 1 1 1 1 1 1 1 1 3 ...

data as matrix

     k  error kernel_size
[1,] 2 0.25500           3
[2,] 3 0.25375           3
[3,] 1 0.24825           5
[4,] 2 0.23050           5
[5,] 3 0.24275           5
[6,] 0 0.00000           0
Masda
  • 11
  • 3
  • Your code is missing the `loadSinglePersonsData` function. I noticed that `output` is a data frame, and the examples in the doc for `filled.contour` do not appear to take a data frame, but they do take a matrix; is this the same for `filled.contour3`? I tested the example in the R docs for `filled.contour` but using `filled.contour3` and it worked when `volcano` was a matrix but gave similar error to yours when converting it to a data frame. – steveb Feb 14 '16 at 16:57
  • I didn't add the function, as i know it works, and it is code wise too big to add, Would just add unessesary complexity – Masda Feb 14 '16 at 17:27
  • What about the rest of the comment, have you looked into the way you are calling `filled.contour3` ? – steveb Feb 14 '16 at 17:30
  • Same error.. as before – Masda Feb 14 '16 at 18:02
  • What does `output` look like (e.g. `head(output)`, `str(output)` ? – steveb Feb 14 '16 at 18:04
  • You are trying to plot a data frame, have you tried the following: `output_mat <- as.matrix(output)` and `filled.contour3(output_mat)` ? That worked for me with toy data (based your your `str` result). – steveb Feb 14 '16 at 18:24
  • I get this error Error in .Internal(filledcontour(as.double(x),as.double(y),z,as.double(levels), : there is no .Internal function filledcontour' – Masda Feb 14 '16 at 18:35
  • http://stackoverflow.com/questions/16812528/filled-contour-in-r-3-0-x-throws-error fixed the error :) Problem solved – Masda Feb 14 '16 at 18:38
  • I saw that too but that was a different error from the one with `Inf`. Either way, I am glad your problem is solved. – steveb Feb 14 '16 at 18:51

1 Answers1

0

In my limited understanding, I think filled.contour() is specifically looking for x y and z components when the input is a list. A dataframe internally is a list but has a matrix like structure and all vectors must of the same length.

In contrast, when using a list, each element of a list can of any type and have any dimension. See below.

# works since volcano is a matrix
filled.contour(volcano)

# Create a data frame volcano
volcano.list <- list(x = 1:nrow(volcano),
                     y = 1:ncol(volcano),
                     z = volcano)
# EDIT:
# This is more appropriate
volcano.list <- list(x = seq(0, 1, length.out = nrow(volcano)),
                     y = seq(0, 1, length.out = ncol(volcano)),
                     z = volcano)

# This should work
filled.contour(volcano.list)

# This doesn't work
volcano.list <- list(a = 1:nrow(volcano),
                     b = 1:ncol(volcano),
                     c = volcano)
filled.contour(volcano.list)

My guess is that when filled.contour sees a list (dataframe is also a list) it starts to specifically look for x, y, and z elements as written in the documentation - ?filled.contour

For example:

volcano.df = as.data.frame(volcano)

volcano.list <- list(x = 1:nrow(volcano),
                     y = 1:ncol(volcano),
                     z = volcano)

is.list(volcano.df)  # TRUE
is.list(volcano.list)  #TRUE !!

Hope this helps...

royr2
  • 2,239
  • 15
  • 20
  • My data set looks like this k error kernel_size [1,] 2 0.25500 3 [2,] 3 0.25375 3 [3,] 1 0.24825 5 [4,] 2 0.23050 5 [5,] 3 0.24275 5 [6,] 0 0.00000 0 When store it as a matrix, but how do i know which axes the data is plotted onto.. It seems to me that all the data are plotted on all the axis... – Masda Feb 15 '16 at 12:18
  • Not sure, I understand. If I take the `volcano` example and pass it to `filled.contour()` as a matrix, then the **row numbers** (scaled to [0,1]) are charted on the **x axis**, and column numbers (scaled to [0.1]) are charted on the **Y-axis**. The matrix itself is used to create the filled contour. If you do `range(volcano)` you'll notice that the range is same as the color legend shown on the chart itself. Not sure if this answers question though. – royr2 Feb 15 '16 at 13:11