用R绘制对数

编程入门 行业动态 更新时间:2024-10-27 18:33:01
本文介绍了用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绘制对数

本文发布于:2023-07-18 08:29:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1142854.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!