如何使用R中的两个数据集绘制CDF

时间:2015-09-22 06:21:06

标签: r plot power-law

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

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

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)

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

enter image description here

1 个答案:

答案 0 :(得分:0)

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

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_

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

首先分别为m_bs和'm_eq':

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

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

enter image description here

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

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

enter image description here

这些图的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")
> 

enter image description here