[英]How can I generate data from a plotted line in R?
我没有只有两条标绘线的数据集,我想生成与平均值(标绘线)相差 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
是否与您显示的一致,而是根据您的评论指导我的回答)。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.