使用poweRlaw软件包绘制多个幂律图

4

我尝试使用poweRlaw包来绘制一些幂律拟合曲线。对于单条曲线似乎是起作用的。但我没法在同一图表上绘制多个曲线。 参考:这个包内有解决方法吗? [注:我是个新手]

set.seed(1)
x1 <- ceiling(rlnorm(1000, 4))

x2 <- ceiling(rlnorm(1000, 2))

library(poweRlaw)

pl_d = pl_data$new(x1)
plot(pl_d)

#Now fit the powerlaw
m = displ$new(pl_d)
#Estimate the cut-off
#estimate_xmin(m)
aaa <- estimate_xmin(m)
aaa <- as.data.frame(aaa)
aaa <- aaa[2,1]
x_min <- min(table(x))
m$setXmin(aaa); m$mle()
#Plot the data and the PL line
#plot(m)
lines(m, col=2)

# next POWER LAW graph

#Plot the data
pl_d = pl_data$new(x2)
points(pl_d)

#Now fit the powerlaw
m = displ$new(pl_d)
#Estimate the cut-off
#estimate_xmin(m)
aaa <- estimate_xmin(m)
aaa <- as.data.frame(aaa)
aaa <- aaa[2,1]
x_min <- min(table(x))
m$setXmin(aaa); m$mle()
#Plot the data and the PL line
#points(m)
lines(m, col=3)
1个回答

3
您可以使用lines命令添加多个最佳拟合幂律。因此,使用您的数据:
set.seed(1)
x1 = ceiling(rlnorm(1000, 4))

我们加载包并创建一个离散的幂律对象:
m = displ$new(x1)

然后绘制数据。
plot(m)

接下来,我们为每个幂律设置xminalpha,并使用lines将其添加到图形中:

m$setXmin(100)
p_100 = estimate_pars(m)
m$setPars(p_100)
lines(m, col=2, lwd=2)

##Line 2    
m$setXmin(202)
p_200 = estimate_pars(m)
m$setPars(p_200)
lines(m, col=3, lwd=2)

enter image description here


如果您想在同一张图上拥有多个数据集,最简单的方法是将一个数据集绘制出来,并将数据保存为数据框。例如,生成一些数据集:

set.seed(1)
x1 = ceiling(rlnorm(1000, 4))
x2 = ceiling(rlnorm(1000, 5))

符合幂律分布

m1 = displ$new(x1)
m1$setXmin(estimate_xmin(m1))

m2 = displ$new(x2)
m2$setXmin(estimate_xmin(m2))

绘制数据集2,并将数据保存为pts2

pts2 = plot(m2)

按照正常的方式绘制和添加线条:

plot(m1)
points(pts2$x, pts2$y, col=3)
lines(m1, col=2)
lines(m2, col=4)

获得:

enter image description here


谢谢。我会等待“xmins”版本的发布。 - Attu nonUser De
好的,我实际想要绘制的是类似于以下内容的东西:![图表]<img src="http://i50.tinypic.com/33x8tg7.png" width="100" height="100"> - Attu nonUser De
@AttuD 请查看更新后的答案。在未来的版本中,我可能会添加一个参数到绘图函数中,以使这更容易。 - csgillespie
谢谢,我明白了。而且我做的方式几乎和你一样。 - Attu nonUser De

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接