【问题标题】:How does plot() decide where to plot data points on the x-axis?plot() 如何决定在 x 轴上绘制数据点的位置?
【发布时间】:2021-09-30 15:01:05
【问题描述】:

我正在尝试使用三组数据构建图表: 预测的“伞”和观察到的“伞”,其中比率转换为整数,然后根据值范围将子集转换为向量。

ylrt    <- c()
yhatlrt <- c()
x       <- 0:678
y       <- 0:640
#
i = 0
while (i <= 12000)
{
    ylrt[[i+1]] <-  sample(x,2)
    i = i + 1
}
#
i = 0
while (i <= 30000)
{
    yhatlrt[[i+1]] <-  sample(x,2)
    i = i + 1
}
#
ylrt    <- unlist(ylrt)
yhatlrt <- unlist(yhatlrt)
#
table(ylrt)
#
table(yhatlrt)
#

为了确定一个比率在给定范围内出现的频率,我获取了向量的长度,并使用 as.integer 将值分配给唯一变量,并用它们构建了一个向量。

ylrt  <- as_tibble(ylrt)
OLR1 <- as.integer(length(unlist(subset(ylrt, ylrt <=  25))))
OLR2 <- as.integer(length(unlist(subset(ylrt, ylrt >   25 & ylrt  <=  50))))
OLR3 <- as.integer(length(unlist(subset(ylrt, ylrt >   50 & ylrt  <=  75))))
OLR4 <- as.integer(length(unlist(subset(ylrt, ylrt >   75 & ylrt  <= 100))))
OLR5 <- as.integer(length(unlist(subset(ylrt, ylrt >  200 & ylrt  <= 400))))
OLR6 <- as.integer(length(unlist(subset(ylrt, ylrt >  400 & ylrt  <= 680))))
OLR7 <- as.integer(length(unlist(subset(ylrt, ylrt >  400 & ylrt  <= 680))))
OLR  <- c(OLR1, OLR2, OLR3, OLR4, OLR5, OLR6, OLR7)

# yhatlrt <- as_tibble(yhatlrt)
PLR1 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt <=  25))))
PLR2 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >   25 & yhatlrt  <=  50))))
PLR3 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >   50 & yhatlrt  <=  75))))
PLR4 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >   75 & yhatlrt  <= 100))))
PLR5 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >  100 & yhatlrt  <= 200))))
PLR6 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >  200 & yhatlrt  <= 400))))
PLR7 <- as.integer(length(unlist(subset(yhatlrt, yhatlrt >  400 & yhatlrt  <= 680))))
PLR      <- c(PLR1, PLR2, PLR3, PLR4, PLR5, PLR6, PLR7)
#

第三个数据集被用作 x 轴值的一种分组数据,包含 1 到 6 之间的整数。

IY  <- c()
#
z   <- 1:7
#
padvar <- length(unlist(ylrt)) - length(IY)
for (i in 1:padvar)
{
    new_value <- sample(z,1)
    IY          <- c(IY, new_value)
}
#

为了绘制向量,然后我用 NA 值填充每个向量,直到它们的长度匹配。

#
padvar <-length(IY) - length(OLR)
    for (i in 1:padvar)
        {
            new_value <- i*NA
                OLR = c(OLR,new_value)
        }
#
padvar1 <-length(IY) - length(PLR)
    for (i in 1:padvar1)
        {
            new_value <- i*NA
                PLR = c(PLR,new_value)
        }
#

现在已经设置了上下文,我注意到在绘制数据集时

plot(OLR~IY)
plot(PLR~IY)

几个点堆叠在相同的 x 值上,同时具有适当的 y 轴值,我无法确定原因。 R 究竟如何确定在 y 轴上绘制数据的位置,从而导致某些值以这种方式堆叠?

【问题讨论】:

  • 请确保您的代码可以在新的 R 会话中运行。目前,OLR6ylr 等变量尚未定义,因此在运行代码时出现错误并且看不到输出。这使您更难看到正在发生的事情对您有帮助。
  • 已修复。在复制到论坛之前,我应该在新会话中对此进行测试。

标签: r plot


【解决方案1】:

您需要提供绘制的数据。 R 根据 x 和 y 轴上显示的比例绘制数据。我第一次运行您的数据时,得到了以下结果:

ALL <- cbind(IY, PLR, OLR)
ALL[complete.cases(ALL), ]
#      IY   PLR  OLR
# [1,]  4  2296  927
# [2,]  4  2232  893
# [3,]  1  2240  892
# [4,]  3  2252  902
# [5,]  3  8771 7159
# [6,]  2 17624 9742
# [7,]  5 24587 9742

IY 有两个值 4,OLR 值分别为 893 和 927,考虑到您的绘图规模,它们非常接近。同样PLR值也差不多,2296、2232。IY上也有两个3的值,但是PLR和OLR上的匹配值差别很大。

第二次运行数据时,我得到的结果没有重叠:

#      IY   PLR  OLR
# [1,]  2  2255  878
# [2,]  4  2198  918
# [3,]  5  2272  861
# [4,]  7  2176  875
# [5,]  1  8845 7095
# [6,]  4 17630 9875
# [7,]  6 24626 9875

“重叠”第三次再次出现:

#      IY   PLR  OLR
# [1,]  1  2356  873
# [2,]  4  2296  915
# [3,]  1  2227  916
# [4,]  4  2146  839
# [5,]  6  8885 7097
# [6,]  5 17555 9856
# [7,]  6 24537 9856

【讨论】:

  • 知道了,所以问题主要出在数据的性质上,即使它完全按照设计运行,我也将重叠视为错误?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多