R 将平均值和sem叠加到每个面的GG图上

R 将平均值和sem叠加到每个面的GG图上,r,ggplot2,overlay,R,Ggplot2,Overlay,我有一个可怕的线条图,我使用刻面来划分组(训练和未训练)和试验(E1,E2)。 我试着看看趋势会是什么样子,所以我使用了stat\u smooth(),但这不是我希望得到的结果。请注意红色平滑线表示数据的效果有多差(特别是在由垂直线分隔的区域中) 我现在想,我更愿意用同样的方式绘制平均值和标准误差。但我想不出怎么做。我看过其他帖子,但我只找到了一个变量的简单点图的信息,没有任何重复。这幅图来自两组各20次重复,采样率为0.2秒,因此更复杂 ?我是否应该找到一种方法来计算每组的平均和标准误差,并将

我有一个可怕的线条图,我使用刻面来划分组(训练和未训练)和试验(E1,E2)。 我试着看看趋势会是什么样子,所以我使用了
stat\u smooth()
,但这不是我希望得到的结果。请注意红色平滑线表示数据的效果有多差(特别是在由垂直线分隔的区域中)

我现在想,我更愿意用同样的方式绘制平均值和标准误差。但我想不出怎么做。我看过其他帖子,但我只找到了一个变量的简单点图的信息,没有任何重复。这幅图来自两组各20次重复,采样率为0.2秒,因此更复杂

?我是否应该找到一种方法来计算每组的平均和标准误差,并将其传递给
stat\u smooth()
函数

不幸的是,我的数据大于字符限制,因此我将提供一个玩具示例

    dput(toy)
    structure(list(tiempo = c(618.2, 618.4, 618.6, 618.8, 619, 619.2, 
    619.4, 619.6, 619.8, 620, 620.2, 620.4, 620.6, 620.8, 621, 621.2, 
    621.4, 621.6, 621.8, 622, 622.2, 622.4, 622.6, 622.8, 623, 623.2, 
    623.4, 623.6, 623.8, 624, 624.2, 624.4, 624.6, 624.8, 625, 625.2, 
    625.4, 625.6, 625.8, 626, 626.2, 626.4, 626.6, 626.8, 627, 3138.2, 
    3138.4, 3138.6, 3138.8, 3139, 3139.2, 3139.4, 3139.6, 3139.8, 
    3140, 3140.2, 3140.4, 3140.6, 3140.8, 3141, 3141.2, 3141.4, 3141.6, 
    3141.8, 3142, 3142.2, 3142.4, 3142.6, 3142.8, 3143, 3143.2, 3143.4, 
    3143.6, 3143.8, 3144, 3144.2, 3144.4, 3144.6, 3144.8, 3145, 3145.2, 
    3145.4, 3145.6, 3145.8, 3146, 3146.2, 3146.4, 3146.6, 3146.8, 
    3147), UT2x45.19 = c(8325L, 8324L, 8323L, 8321L, 8320L, 8317L, 
    8315L, 8313L, 8311L, 8307L, 8305L, 8302L, 8301L, 8298L, 8295L, 
    8294L, 8293L, 8292L, 8290L, 8287L, 8285L, 8283L, 8282L, 8281L, 
    8279L, 8275L, 8271L, 8270L, 8266L, 8263L, 8259L, 8257L, 8255L, 
    8254L, 8253L, 8252L, 8250L, 8246L, 8239L, 8235L, 8235L, 8234L, 
    8232L, 8229L, 8226L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 
    1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 
    1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 
    1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 
    1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 1579L, 
    1579L, 1579L, 1579L), UT2x45.20 = c(17444L, 17445L, 17447L, 17448L, 
    17449L, 17449L, 17451L, 17451L, 17452L, 17451L, 17451L, 17451L, 
    17451L, 17452L, 17453L, 17459L, 17461L, 17463L, 17462L, 17462L, 
    17463L, 17466L, 17467L, 17467L, 17467L, 17466L, 17467L, 17467L, 
    17468L, 17469L, 17469L, 17470L, 17471L, 17473L, 17475L, 17475L, 
    17475L, 17475L, 17476L, 17477L, 17479L, 17481L, 17482L, 17482L, 
    17483L, 19867L, 19868L, 19869L, 19869L, 19869L, 19869L, 19869L, 
    19869L, 19869L, 19869L, 19869L, 19869L, 19869L, 19869L, 19870L, 
    19870L, 19870L, 19870L, 19870L, 19870L, 19871L, 19871L, 19872L, 
    19873L, 19875L, 19877L, 19878L, 19878L, 19878L, 19878L, 19879L, 
    19879L, 19879L, 19881L, 19882L, 19883L, 19884L, 19884L, 19887L, 
    19887L, 19888L, 19890L, 19892L, 19892L, 19892L), TR2x45.19 = c(15870L, 
    15870L, 15866L, 15860L, 15861L, 15863L, 15863L, 15872L, 15877L, 
    15857L, 15845L, 15842L, 15840L, 15830L, 15826L, 15824L, 15818L, 
    15815L, 15813L, 15810L, 15808L, 15804L, 15799L, 15798L, 15776L, 
    15746L, 15734L, 15727L, 15723L, 15723L, 15709L, 15671L, 15634L, 
    15617L, 15598L, 15582L, 15568L, 15557L, 15547L, 15537L, 15526L, 
    15517L, 15506L, 15501L, 15496L, 14338L, 14338L, 14338L, 14338L, 
    14338L, 14338L, 14338L, 14338L, 14337L, 14329L, 14324L, 14323L, 
    14322L, 14322L, 14322L, 14321L, 14321L, 14321L, 14321L, 14321L, 
    14321L, 14321L, 14321L, 14321L, 14321L, 14313L, 14309L, 14307L, 
    14306L, 14307L, 14304L, 14280L, 14253L, 14239L, 14234L, 14225L, 
    14218L, 14204L, 14193L, 14183L, 14174L, 14163L, 14153L, 14143L, 
    14138L), TR2x45.20 = c(15760L, 15760L, 15761L, 15768L, 15780L, 
    15781L, 15787L, 15795L, 15795L, 15791L, 15791L, 15791L, 15789L, 
    15790L, 15792L, 15795L, 15794L, 15794L, 15793L, 15793L, 15791L, 
    15791L, 15791L, 15793L, 15795L, 15798L, 15806L, 15809L, 15812L, 
    15818L, 15822L, 15825L, 15829L, 15833L, 15837L, 15838L, 15839L, 
    15840L, 15841L, 15843L, 15845L, 15847L, 15849L, 15851L, 15854L, 
    14945L, 14945L, 14945L, 14944L, 14942L, 14943L, 14943L, 14943L, 
    14943L, 14942L, 14942L, 14942L, 14942L, 14942L, 14942L, 14942L, 
    14942L, 14942L, 14942L, 14942L, 14942L, 14942L, 14942L, 14942L, 
    14942L, 14941L, 14939L, 14939L, 14939L, 14939L, 14939L, 14939L, 
    14938L, 14938L, 14937L, 14936L, 14936L, 14936L, 14936L, 14936L, 
    14936L, 14936L, 14935L, 14935L, 14935L), ensayo = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("E1", "E2"), class = "factor")), .Names = c("tiempo", 
    "UT2x45.19", "UT2x45.20", "TR2x45.19", "TR2x45.20", "ensayo"), row.names = c("3088", 
    "3089", "3090", "3091", "3092", "3093", "3094", "3095", "3096", 
    "3097", "3098", "3099", "3100", "3101", "3102", "3103", "3104", 
    "3105", "3106", "3107", "3108", "3109", "3110", "3111", "3112", 
    "3113", "3114", "3115", "3116", "3117", "3118", "3119", "3120", 
    "3121", "3122", "3123", "3124", "3125", "3126", "3127", "3128", 
    "3129", "3130", "3131", "3132", "15688", "15689", "15690", "15691", 
    "15692", "15693", "15694", "15695", "15696", "15697", "15698", 
    "15699", "15700", "15701", "15702", "15703", "15704", "15705", 
    "15706", "15707", "15708", "15709", "15710", "15711", "15712", 
    "15713", "15714", "15715", "15716", "15717", "15718", "15719", 
    "15720", "15721", "15722", "15723", "15724", "15725", "15726", 
    "15727", "15728", "15729", "15730", "15731", "15732"), class = "data.frame")
以及功能

deriv_abs<- function(tiempo,dataframe){
  matriz = as.matrix(dataframe)
  matriz_dif = abs(diff(matriz ))# derivar
  matriz_dif1= rbind(c(rep(0,dim(matriz_dif)[2])),matriz_dif) #agrega linea de ceros para compensar diff
  return(data.frame(cbind(tiempo, matriz_dif1)))
}

vel_us<-by( toy[,(2:length(toy))-1], 
         toy$ensayo, 
         FUN= function(x) deriv_abs(x$tiempo, x[,-1] ))

names(vel_us$E1)<-names(toy[2:length(toy)-1])
names(vel_us$E2)<-names(toy[2:length(toy)-1])

df4<-data.frame(rbind(vel_us$E1,vel_us$E2),ensayo=us$ensayo)

m_absvel_us<-melt(df4,id=c("ensayo","tiempo"))

m_absvel_us$trat<-rep(NA,length(m_absvel_us))
m_absvel_us$trat[grep("TR",m_absvel_us$variable)]<-c("TR")
m_absvel_us$trat[grep("UT",m_absvel_us$variable)]<-c("UT")

    us_absvel_plot<-ggplot(m_absvel_us,aes(x=tiempo,y=value,fill=trat,color=trat))+theme_minimal()
us_absvel_plot+geom_line(aes(fill=variable))+facet_wrap(trat~ensayo ,scales="free_x")+labs(title=c("Velocidad por ensayo entrenamiento")) + geom_vline(aes(xintercept = tiempo),lty=2,  vline.data) +stat_smooth(aes(fill=trat))

deriv_abs添加平均线和平均值的标准误差:

us_absvel_plot + 
    stat_summary(fun.y=mean, geom="line", colour="red") +
    stat_summary(fun.data=mean_cl_normal, geom="errorbar", confint=0.683, colour="red") +
    stat_summary(fun.y=mean, geom="point", colour="blue")
conf.int
设置为0.683,会得到一个等于+/-1标准误差的误差条

要获得更平滑、更接近数据的效果,有许多选项。例如,如果使用“黄土”平滑器(每组观测值少于1000个的数据的默认值),则可以执行以下操作:

geom_smooth(method="loess", span=1, se=FALSE, colour="red", size=2) 
您必须使用
span
的值。跨度越小,平滑功能越灵活

您也可以尝试自然样条线。在这种情况下,您可以设置更高的自由度,
df
,以获得更灵活的功能:

library(splines)

geom_smooth(method="lm", formula=y ~ ns(x,df=10), colour="red", size=2)

你的代码是不可复制的,但是如果你想添加一条平均线,你可以这样做,例如:
us\u absvel\u plot+stat\u summary(fun.y=mean,geom=“line”,color=“red”)
。很抱歉,我更改了数据框,而不是子集值,我会尝试修复它。您的代码可以工作,那么SEM或其他更平滑的方法呢?