尝试绘制density
个对象时遇到问题。考虑例如
require(grDevices)
set.seed(43)
d0 = density(rexp(1e5,rate=1))
d1 = density(rexp(1e5,rate=1.8))
plot(d1, col="white", xlab = "x", ylab="Density", main = "")
polygon(d1, col=adjustcolor("blue", alpha.f=0.2))
lines(d0, col="white")
polygon(d0, col=adjustcolor("red", alpha.f=0.2))
目前,它看起来像我预期的那样。当放大Y轴的低值时出现问题。考虑例如
plot(d1, col="white", xlim=c(2.5,3), xlab = "x", ylab="Density", main = "",
ylim=c(0,0.02))
polygon(d1, col=adjustcolor("blue", alpha.f=0.2))
lines(d0, col="white", xlim=c(2.5,3), ylim=c(0,0.02))
polygon(d0, col=adjustcolor("red", alpha.f=0.2))
奇怪的是,多边形的下半部分未达到密度= 0.此外,一个多边形的末端低于另一个多边形。设置yaxs="i"
和xaxs="i"
时,问题仍然存在。
发生了什么以及如何解决此问题?
通过一些个人数据,我得到了像
这样的东西答案 0 :(得分:2)
另一个选择是将密度估计的x范围扩展到数据范围之外,以便密度估计在两端实际上实际上为零。这避免了人为地改变密度估计中的任何值的需要。例如:
const data = {id: "xx"....};
r.table('mytable')
.get(id)
.replace((row) => {
return r.expr(data).merge(row.pluck('createdAt'))
}
.run()
[1] 2.987316e-03 1.235864e-06
set.seed(43)
d0 = density(rexp(1e5,rate=1))
d1 = density(rexp(1e5,rate=1.8))
d1$y[c(1, length(d1$y))]
[1] 6.334144e-17 3.797333e-17
答案 1 :(得分:1)
你可以通过在两端将y值固定为零来作弊吗?
set.seed(43)
d0 = density(rexp(150,rate=1))
d1 = density(rexp(150,rate=1.8))
d0$y[c(1, length(d0$y))] <- 0
d1$y[c(1, length(d1$y))] <- 0
plot(d1, col="white", xlim=c(2.5,3), xlab = "x", ylab="Density", main = "",
ylim=c(0,0.02))
polygon(d1, col=adjustcolor("blue", alpha.f=0.2))
lines(d0, col="white", xlim=c(2.5,3), ylim=c(0,0.02))
polygon(d0, col=adjustcolor("red", alpha.f=0.2))
略微改进是降低整个多边形,使其中一端接触基线,然后将下一个设置为零:
set.seed(43)
d0 = density(rexp(150,rate=1))
d1 = density(rexp(150,rate=1.8))
ends0 <- d0$y[c(1, length(d0$y))]
ends1 <- d1$y[c(1, length(d1$y))]
d0$y <- d0$y - min(ends0)
d1$y <- d1$y - min(ends1)
d0$y[c(1, length(d0$y))] <- 0
d1$y[c(1, length(d1$y))] <- 0
plot(d1, col="white", xlim=c(2.5,3), xlab = "x", ylab="Density", main = "",
ylim=c(0,0.02))
polygon(d1, col=adjustcolor("blue", alpha.f=0.2))
lines(d0, col="white", xlim=c(2.5,3), ylim=c(0,0.02))
polygon(d0, col=adjustcolor("red", alpha.f=0.2))
为了比较,这里没有更正。