在R中的mtext()左侧添加一个点

时间:2017-02-03 17:57:40

标签: r plot

我想知道是否可以在points()的左侧插入mtext()换句话说,有没有办法可以获得mtext() x,y ,这样我才能确定points()的正确位置出现?

这是我的R代码:

curve(dnorm(x),-3,3)
mtext(bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))),line=3)

1 个答案:

答案 0 :(得分:3)

如果您使用text代替mtext,则变得非常简单,因为您可以对文本和点使用xy

#Plot the curve
curve(dnorm(x),-3,3)

#Enable drawing outside the plot region
par(xpd = TRUE)

#STEP 3. Add text at certain x and y.
text(x = 0, y = 0.45, 
    bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))))

#Determine the width of the text you added  
text_width = strwidth( bquote(paste("Medium: ",bold('CT'[12])," = ", .(round(1/3,3)))) )

#Find out x poistion just left of the text.
#Since the text is centre aligned by default,
#you can subtract half the text_width to the x value
#that you had used to add text in STEP 3
#You may also add 10% extra space
points_x = (0 - text_width/2) - (0.1*text_width)

#Add a point just to the left of the text
points(x = points_x, y = 0.45, pch = 20, cex = 3)

enter image description here