在绘图中添加文本

时间:2015-12-16 01:48:32

标签: r visualization

x <- seq(-3,3,0.01)
y1 <- dnorm(x,0,1)
y2 <- 0.5*dnorm(x,0,1)
plot(x,y1,type="l",bty="L",xlab="X",ylab="dnorm(X)")
points(x,y2,type="l",col="red")
polygon(c(x,rev(x)),c(y2,rev(y1)),col="skyblue")

enter image description here

我想标记两条曲线在-3,-2,-1,...,3之间的差异。我试过只使用text函数,我手动逐一调整坐标然后输入两条曲线之间的差异。是否有更有效的方法可以清楚地显示两条曲线之间的差异?

1 个答案:

答案 0 :(得分:0)

x <- seq(-3,3,0.01)
y1 <- dnorm(x,0,1)
y2 <- 0.5*dnorm(x,0,1)
plot(x,y1,type="l",bty="L",xlab="X",ylab="dnorm(X)")
points(x,y2,type="l",col="red")
polygon(c(x,rev(x)),c(y2,rev(y1)),col="skyblue")

idx <- which(x %in% -3:3)
text(x[idx], y1[idx], labels = round(y1[idx], 2))
text(x[idx], y2[idx], labels = round(y2[idx], 2), col="red")

enter image description here