如何从R中的绘制线生成数据?

如何从R中的绘制线生成数据?,r,scatter,standard-deviation,line-plot,data-generation,R,Scatter,Standard Deviation,Line Plot,Data Generation,我没有数据集,只有两条绘制线,我想生成离平均值(绘制线)2个标准偏差的散乱y轴数据。以下是我的代码: ggplot() + lims(x = c(0,20), y = c(0,1)) + annotate("segment",x = .1,xend = 5, yend = .25, y = .1) + annotate("segment",x = 5,xend = 20, yend = .35,y = .25) 对不起,如果这篇文章不清楚,但我

我没有数据集,只有两条绘制线,我想生成离平均值(绘制线)2个标准偏差的散乱y轴数据。以下是我的代码:

ggplot() +
 lims(x = c(0,20), y = c(0,1)) + 
 annotate("segment",x = .1,xend = 5, yend = .25, y = .1) +
 annotate("segment",x = 5,xend = 20, yend = .35,y = .25)


对不起,如果这篇文章不清楚,但我不知道最好的方式来解释它。如果您有任何问题,或者我想做的事情不可能,请告诉我。

以下是您的一行的示例(我没有仔细检查
y=0.09*x+0
是否与您显示的内容一致,并根据您的评论指导我的回答)

库(ggplot2)
图书馆(dplyr)
df%
#把它做成可折叠的
as_tible()%>%
#转向更长的格式
tidyr::pivot_更长(所有内容())%>%
#将列的名称分配给V1 V2。。。
#我们可以清理它,得到实际的x
#这很好地工作,因为你的x=1:20,否则会失败
变异(X=as.numeric(stringr::str_remove(name,“V”))))%>%
#策划这件事
ggplot(aes(X,值))+
几何点()+
#添加之前的“平均”值
几何点(数据=df,aes(x,y1),col=“红色”,大小=2)

您正在尝试的操作是可能的。1) 将直线拟合到线段(这样每个线段的y=ax+b)2)选择x值以获得称为“平均”的y值。3) 对你的每个x使用
rnorm(n,mean)
,这样你就可以得到你想要的分布了好吧,我可以理解你在说什么。1) 我找到了斜率(0.09和0.0067),我改变了线,所以截距为0,以简化事情。2) 我的x值是离散的1:20。我得到,我可以把它们插入方程中,得到y。3) 我不明白这一步是什么或为什么。第3步是根据您的要求生成每个y(x)值周围的分散数据。你应该插上你想要的任何东西
library(ggplot2)
library(dplyr)

df <- tibble(x=1:20,
       y1=0.09*x,
       y2=0.0067*x)

# generate dots for y1
# mean y1 and sd = 1

sapply(df$y1, function(tt) rnorm(10, tt)) %>% 
  # make it into tibble
  as_tibble() %>% 
  # pivot into longer format
  tidyr::pivot_longer(everything()) %>% 
  # names of the columns get assigned to V1 V2 ... 
  # we can clean that and get the actual x
  # this works nicely because your x=1:20, will fail otherwise
  mutate(X=as.numeric(stringr::str_remove(name, "V"))) %>%  
  # plot the thing
  ggplot(aes(X, value)) +
  geom_point() +
  # add the "mean" values from before
  geom_point(data=df, aes(x, y1), col="red", size=2)