R 将线段叠加到圆上

R 将线段叠加到圆上,r,function,ggplot2,overlay,geometry,R,Function,Ggplot2,Overlay,Geometry,我希望创建一个圆,然后将一条线段从原点覆盖到圆上的给定点(在本次试运行中,我选择将线段的端点设置为(.25,.25))。对于我正在进行的研究,我需要能够以多个长度和端点生成这些线段,因此我希望有一个代码能够适用于此。以下是我到目前为止的情况。我最近才开始学习R,所以任何建议都将不胜感激 circle<- function(center=c(0,0),r,npoints=1000){ tt<-seq(0,2*pi,length.out=npoints) xx<-cente

我希望创建一个圆,然后将一条线段从原点覆盖到圆上的给定点(在本次试运行中,我选择将线段的端点设置为(.25,.25))。对于我正在进行的研究,我需要能够以多个长度和端点生成这些线段,因此我希望有一个代码能够适用于此。以下是我到目前为止的情况。我最近才开始学习R,所以任何建议都将不胜感激

circle<- function(center=c(0,0),r,npoints=1000){
  tt<-seq(0,2*pi,length.out=npoints)
  xx<-center[1]+r*cos(tt)
  yy<-center[2]+r*sin(tt)
  return(data.frame(x=xx,y=yy))
}

circle1<-circle(c(0,0),.48)
k<-ggplot(circle1,aes(x,y))+geom_line(aes(0,0,.25,.25))
k

circle讽刺的是,你问题的标题与答案很接近。。。使用
geom\u段

ggplot(circle1,aes(x,y)) + geom_path() + 
  geom_segment(x = 0, y=0, xend=0.25, yend=0.25)
由于“需要能够以多个长度和端点生成这些线段”,因此应将这些点放入
data.frame
中,而不是手动添加它们:

# dummy data
df <- data.frame(x.from = 0, y.from=0, x.to=0.25, y.to=c(0.25, 0))
# plot
ggplot(circle1,aes(x,y)) + geom_path() + 
  geom_segment(data=df, aes(x=x.from, y=y.from, xend=x.to, yend=y.to))
# depending on how you want to change this plot afterwards, 
# it may make more sense to have the df as the main data instead of the circle
ggplot(df) + 
  geom_path(data=circle1, aes(x,y)) + 
  geom_segment(aes(x=x.from, y=y.from, xend=x.to, yend=y.to))
#虚拟数据

df我想您需要
ggplot(circle1,aes(x,y))+geom_path()
根据该代码最初来源的答案开始。我已经能够生成一个圆了。我遇到的问题是线段的重叠。我可以用geom_路径来做这件事吗?你在问题中的示例代码并没有生成一个圆。我知道你想从原点开始创建线,但我认为你至少应该有一个圆作为起点。
annotate
可能就是你想要的。正如在
ggplot(圆圈1,aes(x,y))+geom_path()+注释(“段”,x=0,xend=0.25,y=0,yend=0.25)
中一样,我试图操纵我的工作循环代码来添加行。我可以将代码更改回原始代码,它使用
ggplot(圆圈1,aes(x,y))+geom+plot()