【发布时间】:2018-11-18 12:15:00
【问题描述】:
我需要绘制一个遵循幂律分布的数据向量。因此,如果我将它们绘制在对数轴上,它们将是一条直线。 但是,如果我没有明确提供“y”参数,我不知道如何绘制。 这是代码
library("poweRlaw")
library("ggplot2")
xmin = 1; alpha = 1.5
con_rns = rplcon(1000, xmin, alpha)
#convert to data.frame format for ggplot2
df <- data.frame(con_rns =con_rns[con_rns<1000])
#make plot with both axes log scale
ggplot(data = df, aes(x = con_rns))+
geom_point(stat = 'bin', binwidth = 0.1)+
geom_smooth(stat = 'bin',mapping = aes(x=con_rns),method = "lm",se=FALSE)+
scale_x_log10() +
scale_y_log10()
结果是这样的:
但我想要这个
我知道,我可以手动分箱数据,明确提供“y”,然后绘制线,像这样
ggplot(data = data.frame(a = rnorm(50,0,1),b=5+rnorm(50,2,1)),mapping = aes(x = a,y=b))+
geom_point()+
geom_smooth(method = "lm",se=FALSE)
结果:
但我想知道,如何使用此代码 (geom_point(stat = 'bin', binwidth = 0.1)) 绘制趋势线。它隐式计算数据仓。
PS:
好吧,谢谢克里斯的回答。我还是有问题。如果我想绘制不同的组,我该如何绘制它?数据为 df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) 。如何在对数轴上绘制不同的色点组和色线组?像这样:
【问题讨论】: