如何在对数-对数图(ggplot2)中添加趋势线? [英] How to add trend line in a log-log plot (ggplot2)?

查看:231
本文介绍了如何在对数-对数图(ggplot2)中添加趋势线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绘制一个数据矢量,该矢量遵循幂定律分布.因此,如果我在对数-对数轴上绘制它们,它们将是一条直线. 但是,如果我未明确提供"y"参数,则不知道如何绘制. 这是代码

I need plot a data vector, which follow power law distribution. so if I plot them on log-log axis, and they will be a straight line. However, if I do not explicitly provide "y" parameter, I do not know how to plot. this is code

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",然后像这样绘制线

I know, I can manually bin data, provide "y" explicitly and then plot the line, like this

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))绘制趋势线.它隐式地计算数据仓.

But I want to know, how can I plot trend line with this code (geom_point(stat = 'bin', binwidth = 0.1)). It implicitly calculates data bin.

PS: 好吧,谢谢克里斯的回答.我还是有问题如果要绘制不同的组,该如何绘制?数据为df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)).如何在对数-对数轴上绘制不同的色点组和色线组?像这样:

PS: Well, thanks for Chris's answer. I still have a problem. If I want to plot different group, how can I draw it? The data are df <- data.frame(con_rns =con_rns[con_rns<1000],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T)) . How can I plot different color point group and color line group in log-log axis? like this:

推荐答案

一种方法是使用ggplot_build()

首先,我在没有最佳拟合线的情况下绘制了情节:

first I made the plot without the line of best fit:

p <- ggplot(data = df, aes(x = con_rns))+
  geom_point(stat = 'bin', binwidth = 0.1)+
  scale_x_log10() + 
  scale_y_log10() 

然后,我添加了可以从ggplot_build(p)$data找到的绘图中的合并数据(并逆转了log10转换)

Then I added the binned data from the plot which can be found with ggplot_build(p)$data (and reversed the log10 transformation)

p + geom_smooth(data = ggplot_build(p)$data[[1]], 
              mapping = aes(x=10^x, y= 10^y),method = "lm",se=FALSE)

更新: 另一个问题是如何按不同的颜色组划分图.我以相同的方式进行处理,但是我必须创造一种群体"美感,以便将此数据保存在ggplot_build数据中.

UPDATE: The additional problem was how to split the plot by different colour groups. I approached this in the same way but it was necessary for me to create a 'group' aesthetic so this data could be kept in the ggplot_build data.

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],col=sample(1:3,size = length(con_rns[con_rns<1000]),replace = T))

p <- ggplot(data = df, aes(x = con_rns))+
  geom_point(stat = 'bin', binwidth = 0.1, aes(colour=factor(col), group=factor(col)))+
  scale_x_log10() + 
  scale_y_log10() 


p + geom_smooth(data = ggplot_build(p)$data[[1]], 
                mapping = aes(x=10^x, y= 10^y, colour=factor(group)),method = "lm",se=FALSE)

请注意,现在我们已经将数据分组了,其中一些分组在其bin中的计数为零.当log10转换应用于零时,这将返回警告,并给出一个无穷大的值.这些点已从图中删除,并在趋势线中被忽略.

Note that now we have grouped the data, some of the groups have a count of zero in their bin. This returns a warning when the log10 transformation is applied to zero, giving an infinite value. These points are removed from the plot and ignored in the trend lines.

这篇关于如何在对数-对数图(ggplot2)中添加趋势线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆