Plotting density lines of distributions in log-log scale (base 10) with R

寵の児 提交于 2019-12-24 01:26:08

问题


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



来源:https://stackoverflow.com/questions/47044939/plotting-density-lines-of-distributions-in-log-log-scale-base-10-with-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!