第一个想到的就是玩binwidth
。但这也没有给出一个很好的解决方案:
ggplot(data, aes(x=dist)) +
geom_histogram(binwidth=10) +
scale_x_continuous(expand=c(0,0)) +
scale_y_continuous(expand=c(0.015,0)) +
theme_bw()
gives:
在这种情况下,最好使用密度图。但是,当您使用scale_x_log10
您将收到一条警告消息(Removed 524 rows containing non-finite values (stat_density)
)。这可以通过使用来解决对数加一转型。
下面的代码:
library(ggplot2)
library(scales)
ggplot(data, aes(x=dist)) +
stat_density(aes(y=..count..), color="black", fill="blue", alpha=0.3) +
scale_x_continuous(breaks=c(0,1,2,3,4,5,10,30,100,300,1000), trans="log1p", expand=c(0,0)) +
scale_y_continuous(breaks=c(0,125,250,375,500,625,750), expand=c(0,0)) +
theme_bw()
will give this result: