简体   繁体   English

使用data.frame作为输入制作填充轮廓图?

[英]Making a filled.contour plot with data.frame as input?

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. 我首先尝试使用“ plotly”进行此操作,但没有任何问题。由于必须保存图像,我遇到了问题。 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? 我不确定为什么会收到此错误,因为它在使用plotly时没有任何问题...所以我不确定为什么会这样?

`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

In my limited understanding, I think filled.contour() is specifically looking for x y and z components when the input is a list . 以我的有限理解,我认为filled.contour()在输入为list时专门在寻找x yz分量。 A dataframe internally is a list but has a matrix like structure and all vectors must of the same length. dataframe内部是一个list但具有类似矩阵的结构,并且所有向量的长度必须相同。

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 我的猜测是,当filled.contour看到一个列表(数据filled.contour也是一个列表)时,它开始专门查找文档中写的x, y, and z元素- ?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... 希望这可以帮助...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM