将文本添加到绘图 [英] Adding text to a plot

查看:42
本文介绍了将文本添加到绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在 -3、-2、-1、...、3 处标记两条曲线之间的差异.我尝试仅使用 text 函数手动调整坐标一一输入两条曲线之间的差异.有没有更有效的方法来做到这一点,以便清楚地显示两条曲线之间的差异?

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?

推荐答案

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

这篇关于将文本添加到绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆