如何解决如何从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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。