R 从具有多个y值的数据集为x值绘制平均值和标准偏差

R 从具有多个y值的数据集为x值绘制平均值和标准偏差,r,R,我的数据组织如下: Distance r^2 0 1 0 0.9 0 0 0 0.8 0 1 1 0.5 1 0.45 1 0.56 1 1 2 0 2 0.9 3 0 3 0.1 3 0.2 3 0.3 ... 300 1 300 0.8 我想绘制r^2

我的数据组织如下:

Distance r^2
0        1
0        0.9
0        0
0        0.8
0        1 
1        0.5 
1        0.45
1        0.56
1        1
2        0  
2        0.9
3        0
3        0.1
3        0.2
3        0.3
...
300      1 
300      0.8 
我想绘制r^2衰减与距离的关系图,这意味着我想为每个唯一的距离值绘制一个平均值+st dev。所以我应该在x=0处有一个点,在x=1处有一个点。。。但是我有多个x=0的值

考虑到数据的组织方式,实现这一目标的最佳方式是什么?如果可能的话,我想用R做

谢谢,, 阿德里安

编辑: 我试过:

> dd <-structure(list(Distance = dist18, r.2 = a18[,13]), Names = c("Distance",   "r^2"), class = "data.frame", row.names = c(NA, -15L))
> ggplot(dd, aes(x=Distance, y=r.2)) + stat_summary(fun.data="mean_sdl")
Error in data.frame(x = c(42L, 209L, 105L, 168L, 63L, 212L, 148L, 175L,  : arguments imply differing number of rows: 126877, 15
> head(dist18)
[1]  42 209 105 168  63 212
> head(dd)
Distance  r.2
1       42 0.89
2      209 0.92
3      105 0.91
4      168 0.81
5       63 0.88
6      212 0.88
>dd-ggplot(dd,aes(x=距离,y=r.2))+stat\u摘要(fun.data=“mean\u sdl”)
data.frame(x=c)中出错(42L、209L、105L、168L、63L、212L、148L、175L):参数表示行数不同:126877、15
>主管(18区)
[1]  42 209 105 168  63 212
>总目(dd)
距离r.2
1       42 0.89
2      209 0.92
3      105 0.91
4      168 0.81
5       63 0.88
6      212 0.88

这是因为我的数据没有排序吗?

使用
dplyr
它会是这样的:

df = data.frame(distance = rep(1:300, each = 10), r2 = runif(3000))

library(dplyr)
df_group = group_by(df, distance)
summarise(df_group, mn = mean(r2), s = sd(r2))Source: local data frame [300 x 3]
   distance        mn         s
1       300 0.4977758 0.3565554
2       299 0.4295891 0.3281598
3       297 0.5346428 0.3424429
4       296 0.4623368 0.3163320
5       291 0.3224376 0.2103655
6       290 0.3916658 0.2115264
7       288 0.6147680 0.2953960
8       287 0.3405524 0.2032616
9       286 0.5690844 0.2458538
10      283 0.2901744 0.2835524
..      ...       ...       ...

其中,
df
是包含数据的data.frame,而
distance
r2
是两个列名。

如果要绘制每个点的平均值和+/-1 sd,则使用
ggplot函数
可以轻松完成。使用测试数据

dd<-structure(list(Distance = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 
2L, 2L, 3L, 3L, 3L, 3L), r.2 = c(1, 0.9, 0, 0.8, 1, 0.5, 0.45, 
0.56, 1, 0, 0.9, 0, 0.1, 0.2, 0.3)), .Names = c("Distance", "r.2"
), class = "data.frame", row.names = c(NA, -15L))
产生

我用你的真实数据试过了

real <- read.table("http://pelinfamily.ca/bio/GDR-18_conc.ld", header=F)
dd <- data.frame(Distance=real[,2]-real[,1], r.2=real[,13])

ggplot(dd, aes(x=Distance, y=r.2)) +
   stat_summary(fun.data="mean_sdl", mult=1, geom="ribbon", alpha=.4) + 
   stat_summary(fun.data="mean_sdl", mult=1, geom="line")

real您还可以将SD绘制为均值周围的区域,类似于CI绘制(假设
temp
是您的数据集)

库(data.table)
图书馆(GG2)
温度这应该有效

# Create a data frame like yours

df=data.frame(sample(50,size=300,replace=TRUE),runif(300))
colnames(df)=c('Distance','r^2')

#initialize empty data frame with columns x, mean and stdev

results=data.frame(x=numeric(0),mean=numeric(0),stdev=numeric(0))
count=1 
for (i in 0:max(df$Distance)){
    results[count,'x']=i
    temp_mean=mean(df[which(df$Distance==i),'r^2'])
    results[count,'mean']=temp_mean
    temp_sd=sd(df[which(df$Distance==i),'r^2'])
    results[count,'stdev']=temp_sd
    count=count+1
}
# Plot your results
plot(results$x,results$mean,xlab='distance',ylab='r^2')

epsilon=0.02 #to add the little horizontal bar to the error bars
for (i in 1:nrow(results)){
     up = results$mean[i] + results$stdev[i]
    low = results$mean[i] - results$stdev[i]
    segments(results$x[i],low , results$x[i], up)
    segments(results$x[i]-epsilon, up , results$x[i]+epsilon, up)
    segments(results$x[i]-epsilon, low , results$x[i]+epsilon, low)
}

这是结果

谢谢你的回答,我得到了这个:>ggplot(dd,aes(x=Distance,y=r.2))+stat_summary(fun.data=“mean_sdl”)data.frame中的错误(x=c(42L,209L,105L,168L,63L,212L,148L,175L,:参数意味着不同的行数:126877,15>head(dist18)[1]42 209 105 168 63 212这就是你运行上面的代码得到的结果?你有什么版本的软件包?我用的是Hmisc_3.14-4和ggplot2_1.0.0。这两个版本都和我的相同。一定是关于我没有正确显示的数据。你介意在我的真实数据上试试吗?;距离是“column2-column1”(减)r^2是第13列。@AdrianP。我用真实数据更新了我的解决方案。我不确定第13列是否是
r^2
,因为如果是这样的话,在小范围内低值的情况下,这是一种奇怪的模式。我一定是做错了什么。谢谢,这非常有效!
library(data.table)
library(ggplot2)
temp <- setDT(temp)[, list(Mean = mean(r.2), SD = sd(r.2)), by = Distance]
ggplot(temp) + geom_point(aes(Distance, Mean)) + geom_ribbon(aes(x = Distance, y = Mean, ymin = (Mean - SD), ymax = (Mean + SD)), fill = "skyblue", alpha = 0.4)
# Create a data frame like yours

df=data.frame(sample(50,size=300,replace=TRUE),runif(300))
colnames(df)=c('Distance','r^2')

#initialize empty data frame with columns x, mean and stdev

results=data.frame(x=numeric(0),mean=numeric(0),stdev=numeric(0))
count=1 
for (i in 0:max(df$Distance)){
    results[count,'x']=i
    temp_mean=mean(df[which(df$Distance==i),'r^2'])
    results[count,'mean']=temp_mean
    temp_sd=sd(df[which(df$Distance==i),'r^2'])
    results[count,'stdev']=temp_sd
    count=count+1
}
# Plot your results
plot(results$x,results$mean,xlab='distance',ylab='r^2')

epsilon=0.02 #to add the little horizontal bar to the error bars
for (i in 1:nrow(results)){
     up = results$mean[i] + results$stdev[i]
    low = results$mean[i] - results$stdev[i]
    segments(results$x[i],low , results$x[i], up)
    segments(results$x[i]-epsilon, up , results$x[i]+epsilon, up)
    segments(results$x[i]-epsilon, low , results$x[i]+epsilon, low)
}