绘制ggplot2中分布的日志密度

时间:2014-12-08 10:47:28

标签: r plot ggplot2

我想在ggplot2中绘制密度,但是使用y轴的对数刻度。当我以天真的方式这样做时,我最终会在图表的顶部和两端以零线运行。

有没有一种简单的方法可以解决这个问题?

示例:

library(ggplot2)
library(data.table)
N = 10000
DT = data.table(X = c(rnorm(N), rt(N, df = 2)), Y = rep(c("N", "T"), each = N))
ggplot(DT, aes(x=X, color = Y)) +geom_density() + scale_y_log10() + xlim(-4,4)

输出:

enter image description here

0 个答案:

没有答案