2017-02-03 1 views
2

Ich frage mich, ob ich eine points() einfügen könnte, um auf der linken Seite von mtext() erscheinen? Mit anderen Worten, gibt es eine Möglichkeit, dass ich die x, y der mtext() bekomme, damit ich den richtigen Ort für diese points() erscheinen kann?Hinzufügen eines Punktes links von einem mtext() in R

Hier ist mein R-Code:

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

Antwort

3

Es wird ziemlich einfach, wenn Sie text anstelle von mtext wie Sie x und y für Text und Punkte verwenden können.

#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

+1

Clever zu wechseln, nur um 'Text' – Gregor

Verwandte Themen