我知道参数 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绘制对数
发布评论