我有一个关于用重复测量的数据集绘制误差条的问题。我有这样的数据集:
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()"one_answers"normDatawithin()"的函数,遵循的方法(在此处输入链接描述)
这是我的代码:
summarySEwithin(data, measurevar = "Diameter", groupvars= c("Quantity","Temperature", "Time"),idvar="quality")
我还试图在数据集中添加一列"主题",并编写代码:
summarySEwithin(Data, measurevar = "Diameter", groupvars= c("Quality","Quantity","Temperature", "Time"),idvar="Subject")
它们都有"未使用的参数"错误。
有人能帮我处理案子吗?任何答复都将不胜感激。非常感谢。
一些测试数据:
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)))