0

我有一个关于使用重复测量数据集绘制误差线的问题。我有这样的数据集:

    Food_type Quantity Temperature Time Diameter
    Food1      High       High      6    0.00100
    Food1      Medium     High      6    0.00090
    Food1      Low        High      6    0.00085
    Food2      High       High      6    0.00100
    Food2      Medium     High      6    0.00090
    Food2      Low        High      6    0.00085
    ...

我用不同食物类型、不同食物浓度的动物幼虫喂养,并在不同温度下培养幼虫,每6天测量一次它们的大小。

我想绘制这个数据集的误差线,因为数据是重复测量的,我需要在绘制之前使用函数“SummarySEwithin()”来汇总数据。我也使用了“SummarySE()”和“normDatawithin()”的功能,按照方法(在此处输入链接描述

这是我的代码:

    summarySEwithin(data, measurevar = "Diameter", groupvars= c("Quantity","Temperature", "Time"),idvar="quality")

我还尝试在数据集中添加一列“主题”,并编写代码:

    summarySEwithin(Data, measurevar = "Diameter", groupvars= c("Quality","Quantity","Temperature", "Time"),idvar="Subject")

他们都有“未使用的参数”错误。

有人可以帮我处理我的案子吗?任何答案将不胜感激。谢谢你。

4

1 回答 1

0

一些测试数据:

data <- data.frame(expand.grid(Quality = c('Food1','Food2'),
                           Quantity = c('High', 'Medium', 'Low'), 
                           Temperature = c('High','Low'), 
                           Time = 1:6,
                           Sample = 1:3))
data$Diameter <- runif(NROW(data))

dplyr 解决方案:

library(dplyr)
# I'm using standard dev as my measurement of error, but you can use
# any other definition you want
plotdata <-
 data %>% 
  group_by(Quality, Quantity, Temperature, Time) %>%
  summarise(DiameterMean = mean(Diameter),
            DiameterErr = sd(Diameter)) %>%
  arrange(Time)

data.table 解决方案:

library(data.table)
data <- setDT(data)
plotdata <- data[,.(DiameterMean = mean(Diameter),
                    DiameterErr = sd(Diameter)), 
                 by = .(Quality, Quantity, Temperature, Time)][order(Time)]

现在用 plotly 或 ggplot2 绘制:

library(plotly)
plot_ly(data = plotdata,
        y = DiameterMean, 
        x = Time, 
        error_y = list(array = DiameterErr), 
        color = paste0(Quantity,'-', Quality,'-',Temperature))

ggplot2:

library(ggplot2)
ggplot(data = plotdata) +
  geom_line(aes(x = Time, 
                y = DiameterMean, 
                color = paste0(Quantity,'-', Quality,'-',Temperature))) +
  geom_errorbar(aes(x = Time,
                    ymin = DiameterMean - DiameterErr,
                    ymax = DiameterMean + DiameterErr,
                    color = paste0(Quantity,'-', Quality,'-',Temperature)))
于 2016-05-12T03:53:59.367 回答