如何用R中数据帧的平均值和标准偏差绘制单个数据点

如何用R中数据帧的平均值和标准偏差绘制单个数据点,r,plot,dataframe,R,Plot,Dataframe,我有一个R格式的大数据帧: "SubjID" "HR" "IBI" "Stimulus" "Status" "S1" 75.98 790 1 1 "S1" 75.95 791 1 2 "S1" 65.7 918 1 3 "S1" 59.63 100 1 4 "S1" 59.44 101 1 5 "S1" 59.62 101 2 1 "S1" 63.85 943 2 2 "S1"

我有一个R格式的大数据帧:

"SubjID"    "HR"    "IBI"   "Stimulus"  "Status"
"S1"    75.98   790 1   1
"S1"    75.95   791 1   2
"S1"    65.7    918 1   3
"S1"    59.63   100 1   4
"S1"    59.44   101 1   5
"S1"    59.62   101 2   1
"S1"    63.85   943 2   2
"S1"    60.75   992 2   3
"S1"    59.62   101 2   4
"S1"    61.68   974 2   5
"S2"    65.21   921 1   1
"S2"    59.23   101 1   2
"S2"    61.23   979 1   3
"S2"    70.8    849 1   4
"S2"    74.21   809 1   4
我想为status列的每个值绘制“HR”列的平均值

我编写了以下R代码,其中创建了数据的子集(通过“状态”的不同值)并绘制它:

numberOfSeconds <- 8;

    for(stimNumber in 1:40) {

    stimulus2plot <- subset(resampledDataFile, Stimulus == stimNumber & Status <= numberOfSeconds, select=c(SubjID, HR, IBI, Stimulus, Status))

    plot(stimulus2plot$HR~stimulus2plot$Status, xlab="",ylab="")
    lines(stimulus2plot$HR~stimulus2plot$Status, xlab="",ylab="")

    }

numberOfSeconds最简单的方法是先预计算值,然后绘制它们。我将使用
ddply
进行此类分析:

library(plyr)
res = ddply(df, .(Status), summarise, mn = mean(HR))
并使用ggplot2进行打印:

ggplot(res, aes(x = Status, y = mn)) + geom_line() + geom_point()

最简单的方法是先预计算值,然后绘制它们。我将使用
ddply
进行此类分析:

library(plyr)
res = ddply(df, .(Status), summarise, mn = mean(HR))
并使用ggplot2进行打印:

ggplot(res, aes(x = Status, y = mn)) + geom_line() + geom_point()

最简单的方法是
tapply()
。如果您的
data.frame
data

means <- with(data, tapply(HR, Status, mean))
plot(means, type="l")

意味着最简单的方法是
tapply()
。如果您的
data.frame
data

means <- with(data, tapply(HR, Status, mean))
plot(means, type="l")

意味着使其最接近您想要的:

library(ggplot2)
library(plyr)
df.summary <- ddply(df, .(Stimulus, Status), summarise,
                    HR.mean = mean(HR),
                    HR.sd = sd(HR))
ggplot(df.summary, aes(Status, HR.mean)) + geom_path() + geom_point() + 
  geom_errorbar(aes(ymin=HR.mean-HR.sd, ymax=HR.mean+HR.sd), width=0.25) +facet_wrap(~Stimulus) 
库(ggplot2)
图书馆(plyr)

df.summary要使其最接近您想要的内容:

library(ggplot2)
library(plyr)
df.summary <- ddply(df, .(Stimulus, Status), summarise,
                    HR.mean = mean(HR),
                    HR.sd = sd(HR))
ggplot(df.summary, aes(Status, HR.mean)) + geom_path() + geom_point() + 
  geom_errorbar(aes(ymin=HR.mean-HR.sd, ymax=HR.mean+HR.sd), width=0.25) +facet_wrap(~Stimulus) 
库(ggplot2)
图书馆(plyr)

df.summary您可以在ggplot2中完全这样做,如下所示,使用以下伪数据示例作为指导:

DF <- data.frame(stimulus = factor(rep(paste("Stimulus", seq(4)), each = 40)),
                 subject = factor(rep(seq(20), each = 8)),
                 time = rep(seq(8), 20),
                 resp = rnorm(160, 50, 10))
# spaghetti plots
ggplot(DF, aes(x = time, y = resp, group = subject)) +
   geom_line() +
   facet_wrap(~ stimulus, ncol = 1)
# plot of time averages by stimulus
ggplot(DF, aes(x = time, y = resp)) +
   stat_summary(fun.y = mean, geom = "line", group = 1) +
   stat_summary(fun.y = mean, geom = "point", group = 1, shape = 1) +
   facet_wrap(~ stimulus, ncol = 1)

DF您可以使用以下伪数据示例作为指导,在ggplot2中完全执行此操作,如下所示:

DF <- data.frame(stimulus = factor(rep(paste("Stimulus", seq(4)), each = 40)),
                 subject = factor(rep(seq(20), each = 8)),
                 time = rep(seq(8), 20),
                 resp = rnorm(160, 50, 10))
# spaghetti plots
ggplot(DF, aes(x = time, y = resp, group = subject)) +
   geom_line() +
   facet_wrap(~ stimulus, ncol = 1)
# plot of time averages by stimulus
ggplot(DF, aes(x = time, y = resp)) +
   stat_summary(fun.y = mean, geom = "line", group = 1) +
   stat_summary(fun.y = mean, geom = "point", group = 1, shape = 1) +
   facet_wrap(~ stimulus, ncol = 1)

DF谢谢第一部分工作得很好。但是,如果我将代码的第二部分用于错误条,则绘图会变得更加“平坦”(线条几乎是水平的),而仅使用平均值,您就可以看到它们在不同方向上的变化steepness@Albz您只需调整
ylim
参数,该参数指定y轴的下限和上限。我将其设置为
c(50,80)
,这样错误条就可以整齐地放在绘图中,但您可以根据自己的喜好进行调整。请参阅
?绘图。默认值
感谢第一部分工作正常。但是,如果我将代码的第二部分用于错误条,则绘图会变得更加“平坦”(线条几乎是水平的),而仅使用平均值,您就可以看到它们在不同方向上的变化steepness@Albz您只需调整
ylim
参数,该参数指定y轴的下限和上限。我将其设置为
c(50,80)
,这样错误条就可以整齐地放在绘图中,但您可以根据自己的喜好进行调整。请参阅
?绘图。默认值
编辑我的答案以反映此错误。感谢您的提醒,我将在查看分钟内删除我的评论,如果您执行相同操作,则除了我的编辑之外,将不存在此错误的任何记录。编辑我的答案以反映此错误。感谢您的提醒,我将在查看分钟内删除我的评论,如果你做了相同的事情,那么除了我的编辑之外,这个错误的记录就不存在了。