Plotting density lines of distributions in log-log

2019-07-08 10:11发布

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.

enter image description here

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.

1条回答
一夜七次
2楼-- · 2019-07-08 10:56

Use the pdf of the lognormal in base10

enter image description here

[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)")

enter image description here

Plotting without ggplot

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

enter image description here

查看更多
登录 后发表回答