R 向外移动绘制在曲线上的一系列数字

R 向外移动绘制在曲线上的一系列数字,r,plot,R,Plot,我想知道如何使当前绘制在下面曲线上的数字向外移动一点,这样,无论我的R代码中的a和b发生了变化,数字和曲线之间的距离保持不变(即恒定)? 请参见下图下方的我的R代码: a=0;b=1 曲线(dnorm(x,平均值=a,sd=b),-4,4,轴=F,ann=F) xx正如其他同事也提到的,这个距离的计算可以由text()本身来完成。为此,text()中最合适的参数之一是pos。根据R文档pos获取4个整数值,每个整数值沿4个主要方向之一移动文本:请参见?text。在这种情况下,3会产生所需的效果

我想知道如何使当前绘制在下面曲线上的数字向外移动一点,这样,无论我的R代码中的
a
b
发生了变化,数字和曲线之间的距离保持不变(即恒定)?

请参见下图下方的我的R代码:

a=0;b=1
曲线(dnorm(x,平均值=a,sd=b),-4,4,轴=F,ann=F)

xx正如其他同事也提到的,这个距离的计算可以由
text()
本身来完成。为此,
text()
中最合适的参数之一是
pos
。根据R文档
pos
获取4个整数值,每个整数值沿4个主要方向之一移动文本:请参见
?text
。在这种情况下,
3
会产生所需的效果

因此,以下方法可能会解决此问题:

  a = 0  ;  b = 1

 curve( dnorm(x, mean = a, sd = b ), -4, 4, axes = F, ann = F)

 xx <- -4:4

 yy <- dnorm(xx, mean = a, sd = b)

 text(xx, yy, paste(round(yy, 2) ), font = 2, pos = 3 )
a=0;b=1
曲线(dnorm(x,平均值=a,sd=b),-4,4,轴=F,ann=F)
xx
a=0
b=1
#绘制曲线
曲线(dnorm(x,平均值=a,sd=b),-4,4,轴=F,ann=F)
#将曲线指定为“cc”,并确定曲线上点的长度
cc=曲线(dnorm(x,平均值=a,标准差=b),-4,4,轴=F,ann=F)
l_cc=长度(cc$x)

xx您还应该查看函数
text
中的
offset
参数。这将指定文本和指定坐标之间的间距。
  a = 0  ;  b = 1

 curve( dnorm(x, mean = a, sd = b ), -4, 4, axes = F, ann = F)

 xx <- -4:4

 yy <- dnorm(xx, mean = a, sd = b)

 text(xx, yy, paste(round(yy, 2) ), font = 2, pos = 3 )
a = 0
b = 1

#Draw curve
curve(dnorm(x, mean = a, sd = b ), -4, 4, axes = F, ann = F)

#Assign curve to 'cc' and determine the length of points on the curve
cc = curve(dnorm(x, mean = a, sd = b ), -4, 4, axes = F, ann = F)
l_cc = length(cc$x)

xx <- -4:4
yy <- dnorm(xx, mean = a, sd = b)

#Find indices of values in cc$x closest ot xx
slope_inds = findInterval(xx, cc$x)

#Calculate approximate slope of cc for each xx
slope = numeric(0)
for (i in 1:length(slope_inds)){
    if (slope_inds[i] == 1){
        n = 1
    }else if (slope_inds[i] == l_cc){
        n = l_cc - 1
    }else{
        n = slope_inds[i]
    }
    slope[i] = round(diff(cc$y[n:(n+1)])/diff(cc$x[n:(n+1)]), 1)
}

#Assign pos value based on slope of cc. For ~zero slope, put text on top
# For other slopes assign values accordingly
positions = integer(0)
positions[slope == 0] = 3
positions[slope > 0] = 2
positions[slope < 0] = 4

#Write text
points(xx,yy)
text(xx, yy, paste(round(yy, 2) ), font = 2, pos = positions)