【问题标题】:Making a filled.contour plot with data.frame as input?使用 data.frame 作为输入制作填充轮廓图?
【发布时间】:2016-02-14 16:07:33
【问题描述】:

我试图制作一个填充的等高线图,但这样做似乎有一些问题。

我首先尝试使用“plotly”来完成它,它没有任何问题,但我遇到了问题,因为我必须保存图像。现在我正在尝试其他方式。

这是输出数据矩阵的函数

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)

当我尝试绘制它时:

filled.contour3(output)

我收到此错误

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

我不知道为什么我会收到这个错误,因为它在情节上没有任何问题......所以我不确定为什么会这样?

`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 ...

数据作为矩阵

     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

【问题讨论】:

  • 您的代码缺少loadSinglePersonsData 函数。我注意到output 是一个数据框,filled.contour 文档中的示例似乎没有采用数据框,但它们确实采用了矩阵; filled.contour3 也一样吗?我在 R 文档中测试了 filled.contour 的示例,但使用了 filled.contour3,当 volcano 是矩阵时它可以工作,但在将其转换为数据框时会出现类似的错误。
  • 我没有添加该功能,因为我知道它可以工作,而且它的代码太大而无法添加,只会增加不必要的复杂性
  • 评论的其余部分怎么样,你有没有研究过你打电话给filled.contour3的方式?
  • 同样的错误.. 和以前一样
  • output 看起来像什么(例如head(output)str(output)

标签: r plot contour plotly


【解决方案1】:

在我有限的理解中,当输入为list 时,我认为filled.contour() 专门寻找x yz 组件。 dataframe 内部是 list 但具有类似矩阵的结构,并且所有向量必须具有相同的长度。

相比之下,当使用列表时,列表的每个元素都可以是任何类型并具有任何维度。见下文。

# 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)

我的猜测是,当filled.contour 看到一个列表(数据框也是一个列表)时,它开始专门查找 文档 中所写的x, y, and z 元素 - ?filled.contour

例如:

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 !!

希望这会有所帮助...

【讨论】:

  • 我的数据集看起来像这样 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 将其存储为矩阵时,但我怎么知道数据绘制在哪些轴上。在我看来,所有数据都绘制在所有轴上...
  • 不确定,我明白。如果我采用volcano 示例并将其作为矩阵传递给filled.contour(),则行号(缩放为[0,1])绘制在x 轴 和列号(缩放为 [0.1])在 Y 轴 上绘制。矩阵本身用于创建填充轮廓。如果您执行range(volcano),您会注意到范围与图表本身显示的颜色图例相同。不确定这是否能回答问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 2021-03-17
相关资源
最近更新 更多