【问题标题】:How to plot CDF using two dataset in R如何在 R 中使用两个数据集绘制 CDF
【发布时间】:2015-09-22 06:21:05
【问题描述】:

我正在尝试使用绘图功能绘制以下数据集。我无法在同一个图中绘制这两个图。

我尝试使用数据集绘制图表。

m_bs = conpl$new(sample_data1$V1)
m_eq = conpl$new(sample_data2$V1)

est = estimate_xmin(m_bs, xmax=5e+5)
est_eq = estimate_xmin(m_eq, xmax=Inf)

m_bs$setXmin(est_bs)
m_eq$setXmin(est_eq)

plot(m_bs)
lines(m_bs)
d = plot(m_eq, draw =FALSE)
points(d$x, d$y, col=2)
lines(m_eq,col=2,lwd=2)

我得到了下图,它只显示了一张图。很抱歉再次发布问题,我之前的帖子没有得到正确的答案。

【问题讨论】:

  • 在我看来你得到了两个图,但是因为你首先绘制了 x 轴范围较小的图,所以来自另一个数据框的数据位于绘图区域之外。您可以从第二个图中看到一个红点。尝试在您的第一个图上设置 xlim,使其与两个数据集的规模相匹配
  • 您也应该发布一个可重现的示例。您调用了不在基本 R 中的函数。您使用了哪些包(即将library 调用添加到您的帖子中)。
  • 我只导入库('powerlaw')

标签: r plot power-law


【解决方案1】:

我查阅了poweRlaw使用的plot函数的源代码并进行了修改:

lines_ <- function (x, y, ...) 
{
  .local <- function (x, cut = FALSE, draw = TRUE, ...) 
  {
    xmin = x$getXmin()
    cut_off = cut * xmin
    x_values = x$dat
    if (!cut) 
      x$setXmin(min(x_values))
    y = dist_data_cdf(x, lower_tail = FALSE, xmax = max(x_values) + 1)
    cut_off_seq = (x_values >= cut_off)
    x_axs = x_values[cut_off_seq]
    if (is(x, "discrete_distribution")) 
      x_axs = unique(x_axs)
    x$setXmin(xmin)
    x = x_axs
    if (draw) 
      lines(x, y, ...)
    invisible(data.frame(x = x, y = y))
  }
  .local(x, ...)
}

#----------------------------------------------------------

points_ <- function (x, y, ...) 
{
  .local <- function (x, cut = FALSE, draw = TRUE, ...) 
  {
    xmin = x$getXmin()
    cut_off = cut * xmin
    x_values = x$dat
    if (!cut) 
      x$setXmin(min(x_values))
    y = dist_data_cdf(x, lower_tail = FALSE, xmax = max(x_values) + 1)
    cut_off_seq = (x_values >= cut_off)
    x_axs = x_values[cut_off_seq]
    if (is(x, "discrete_distribution")) 
      x_axs = unique(x_axs)
    x$setXmin(xmin)
    x = x_axs
    if (draw) 
      points(x, y, ...)
    invisible(data.frame(x = x, y = y))
  }
  .local(x, ...)
}

函数lines_points_

  • 绘制与poweRlaw 包的plot 函数相同的图形,但是
  • 表现得像标准的 linespoints 函数,因为它们不会破坏当前图形。

首先 m_bs 和 'm_eq' 分开:

> plot(m_bs, lwd=9, col="black")

> lines_(m_bs, lwd=5, col="green")

> plot(m_eq, lwd=9, col="black")

> lines_(m_eq, lwd=5, col="blue")

这些图表的 x 范围不重叠。因此, xlim必须适当地选择同一图片中的两个图。

> plot( m_eq, lwd=8, col="black",
+       xlim=c(min(m_bs$dat,m_eq$dat),max(m_bs$dat,m_eq$dat)))

> lines_(m_eq, lwd=5, col="blue")

> points_(m_bs,lwd=8,col="black")

> lines_(m_bs, lwd=5, col="green")
> 

【讨论】:

    猜你喜欢
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多