用R绘制对数-对数刻度(以10为底)的分布密度线 [英] Plotting density lines of distributions in log-log scale (base 10) with R

查看:345
本文介绍了用R绘制对数-对数刻度(以10为底)的分布密度线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道参数 log = xy ,但是我不知道您是否可以控制对数刻度的底数(我猜是10是默认值(?)),而我对下面的特定问题并不满意...



如何重现以下情节(



撇开幂律红线,我在玩

  x = rlnorm(1e4,0,10)
h = hist(x,prob = T,plot = F)
plot(h $ count,log = xy,type = l,lend = 2 )

没有成功。

解决方案

我们e base10中对数正态的pdf





[将其推广到其他对数基是很简单的。]



然后我们可以在log10-log10比例尺上绘制pdf。



(gg)绘图



 #lognormal base log10 pdf,w在log10中
lognorm_base10< -函数(w,mu,sigma){
log10(exp(1))/(sqrt(2 * pi * sigma ^ 2)* 10 ^ w)* exp(-(w-mu)^ 2 / (2 * sigma ^ 2));
}

#为mu = 0,sigma = 10
x≤-seq(0,10,length.out = 100)生成数据;
y<-lognorm_base10(x,0,10);

#绘图
require(ggplot2);
gg<-ggplot(data.frame(x = x,y = y),aes(x,y));
gg<-gg + geom_line()+ scale_y_log10();
gg<-gg + labs(x = log10(x),y = log10(p))



不进行ggplot绘制



 图(x,log10(y),type = l)


I know about the parameter log="xy", but I don't know whether you can control the base of the logarithmic scale (my guess is that 10 may be the default (?)), and I'm not getting lucky on the specific issue below...

How can I reproduce the following plot (from this source) with R. In particular, I am having problems with the log base 10 x and y axes.

Leaving aside the power law red line, I was playing with

x = rlnorm(1e4,0,10)
h = hist(x, prob=T, plot=F)
plot(h$count, log="xy", type="l", lend=2)

without success.

解决方案

Use the pdf of the lognormal in base10

[Generalising it to other log-bases is straightforward.]

We can then plot the pdf on a log10-log10 scale.

(gg)plotting

# lognormal base log10 pdf, w is in log10
lognorm_base10 <- function(w, mu, sigma) {
    log10(exp(1)) / (sqrt(2*pi*sigma^2) * 10^w) * exp(- (w - mu)^2 / (2 * sigma^2));
}

# Generate data for mu = 0, sigma = 10 
x <- seq(0, 10, length.out = 100);
y <- lognorm_base10(x, 0, 10);

# Plot
require(ggplot2);
gg <- ggplot(data.frame(x = x, y = y), aes(x, y));
gg <- gg + geom_line() + scale_y_log10();
gg <- gg + labs(x = "log10(x)", y = "log10(p)")

Plotting without ggplot

plot(x, log10(y), type = "l")

这篇关于用R绘制对数-对数刻度(以10为底)的分布密度线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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