Adding text to a plot

拟墨画扇 提交于 2019-12-20 07:05:50

问题


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

I want to label the difference between the two curves at -3, -2, -1, ..., 3. I have tried using just the text function where I manually adjust the coordinates one by one and then type in the difference between the two curves. Is there an more efficient way of doing this so that the difference between the two curves is displayed clearly?


回答1:


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



来源:https://stackoverflow.com/questions/34302680/adding-text-to-a-plot

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