【问题标题】:Incorrect x and y-axis values of contour2D plot等高线图的 x 和 y 轴值不正确
【发布时间】:2017-09-08 06:57:52
【问题描述】:

我想查看我的数据的密度,所以我使用cut 函数预处理的数据绘制了一个等高线图,这是我数据的一个小样本:

> z[1:2,]
           pc2_cut
pc1_cut         (-1.61,-1.45] (-1.45,-1.3] (-1.3,-1.15] (-1.15,-1] (-1,-0.851]
(-1.58,-1.38]             0            1            1          0           0
(-1.38,-1.18]             5            1            4          1           0

我使用了plot3D 库,并且,

> contour2D(z,border="black",xlab="PC1",ylab="PC2")

这是我得到的:

您可以看到 x 轴和 y 轴的值不正确,甚至不在区间的中点附近。有谁知道如何纠正这个?

【问题讨论】:

    标签: r plot intervals contour axes


    【解决方案1】:

    contour2D() 函数默认在 0 和 1 之间缩放坐标轴。要获得不同的轴,您可以从原始 contour2D() 调用中省略轴,并使用 axis() 添加它们,并指定 atlabels。如果您使用来自cut()factors,则必须在绘图之前将它们转换为数值。我在下面提供了一个示例,我在其中生成数据、绘制数据,然后在将因子转换为数值后调整轴标签。如果没有您的确切数据格式,我不知道从您的数据中提取刻度线标签的最佳方法。

    # library for plot
    library(plot3D)
    
    # setting seed and generating some data
    set.seed(10)
    
    #### storing data in matrix ####
    datamatrix <- matrix(c(rnorm(500,-1,.4),rnorm(500,2,0.2),runif(500,-3,0),runif(500,0,3)),nrow=1000,ncol=2,byrow=F)
    
    # locations of cuts
    xcuts <- seq(min(datamatrix[,1]),max(datamatrix[,1]),length.out = 6)
    ycuts <- seq(min(datamatrix[,2]),max(datamatrix[,2]),length.out = 6)
    
    # calculating values for cutting
    xvals <- cut(datamatrix[,1], xcuts)
    yvals <- cut(datamatrix[,2], ycuts)
    
    # initializing matrix to store count in each bin
    z <- matrix(0,length(levels(yvals)),length(levels(xvals)))
    
    for(i in 1:length(levels(xvals))){
      for(j in 1:length(levels(yvals))){
        z[j,i] <- length(intersect(which(xvals == levels(xvals)[i]),which(yvals == levels(yvals)[j])))
      }
    }
    
    #### finding labels from factors cut ####
    factsx <- levels(xvals) # factsx <- levels_pc2_cut # or something like that
    xlabsFacts <- rep(NA,length(factsx))
    
    for(i in 1:(length(factsx))){
    
      comma_sep <- unlist(gregexpr(pattern =',',factsx[i])) # location of the comma in the factor
    
      #taking section of text and converting to numbers
      xlabsFacts[i] <- as.numeric(substr(factsx[i],2,comma_sep-1))
      xlabsFacts[i+1] <- as.numeric(substr(factsx[i],comma_sep+1,nchar(factsx[i])-1))
    
    }
    
    factsy <- levels(yvals) # factsy <- levels_pc1_cut # or something like that
    ylabsFacts <- rep(NA,length(factsy))
    
    for(i in 1:(length(factsy))){
    
      comma_sep <- unlist(gregexpr(pattern =',',factsy[i])) # location of the comma in the factor
    
      #taking section of text and converting to numbers
      ylabsFacts[i] <- as.numeric(substr(factsy[i],2,comma_sep-1)) 
      ylabsFacts[i+1] <- as.numeric(substr(factsy[i],comma_sep+1,nchar(factsy[i])-1))
    
    }
    
    
    #### formatting plot ####
    # contour plot without axes
    contour2D(z
              ,yaxt='n' # no y axis ticks
              ,xaxt='n' # no x axis ticks
              ,ylab='y values' # y axis label
              ,xlab='x values' # x axis label
    )
    
    # adding x axis with tick marks
    axis(side=1 # bottom
         ,at=seq(0,1,length.out = length(xlabsFacts)) # change 6 to number of tick marks you want
         ,labels=round(xlabsFacts,2) # change to labels for tick marks from your data
    )
    
    # adding x axis with tick marks
    axis(side=2 # bottom
         ,at=seq(0,1,length.out = length(ylabsFacts)) # change 6 to number of tick marks you want
         ,labels=round(ylabsFacts,2) # change to labels for tick marks from your data
    
    )
    

    【讨论】:

    • 我设法从 contour2D 中省略了轴,但是当我通过 > axis(side=1,at=seq(0,1,length.out=9),labels=round( pc2_cut,2)),我收到此错误:Math.factor 错误(c(10L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, : 'round' 对因子没有意义。你知道为什么吗?谢谢!
    • @HYY,检查class(pc_cut)。如果是"character",则R 将其视为字符串。解决此问题的两个选项是指定cut() 的位置,然后您可以在labels 参数中使用这些位置。如果您不想这样做,您可能必须使用substr() 获取数字部分,然后使用as.numeric() 使其成为可以四舍五入的值。如果您不想四舍五入,也可以只使用substr(),因为标签可以是字符串。
    • 我查过了,是因素。
    • 好的。您可以使用labels=pc2_cut,它只会在刻度线处绘制因子值(间隔)。如果您想要不同的东西,您可能必须将因子值重新格式化为数字格式。您可以随时尝试指定cut() 的位置,这样您就可以获得可在标签中使用的数值。
    • 答案中包含了一段新的代码,它应该处理因子水平并将它们转换为标签。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 2021-06-11
    • 2019-06-02
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多