Plotting density lines of distributions in log-log

2019-07-08 09:52发布

问题:

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.

回答1:

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