如何在重复测量设计中获取长格式数据的直方图得分 [英] How to get the scores in a long-format data for histogram in the repeated-measures design
问题描述
我使用melt()
将数据从宽格式转换为长格式,并显示了我的长格式数据(df),如下所示:
I transformed my data from wide-format to long-format using melt()
, and my long-format data (df) is shown, as follows:
Participant Tests Scores
1 A 8
1 B 7
1 C 1
1 D 6
2 A 9
2 B 5
2 C 2
2 D 5
3 A 6
3 B 2
3 C 3
3 D 8
4 A 5
4 B 3
4 C 1
4 D 9
5 A 8
5 B 4
5 C 5
5 D 8
6 A 7
6 B 5
6 C 6
6 D 7
我如何获得df$Tests[1]
的所有分数(即测试中的每个参与者= A)?
How can I get all scores for df$Tests[1]
(i.e., for each Participant in the Tests = A)?
此外,如果我希望绘制直方图,例如:
Furthermore, in case I wish to plot the Histogram such as:
hist.TestA <- ggplot(df, aes(???)) + theme(legend.position = "none") + geom_histogram(aes(y=..density..), colour="black", fill="white", binwidth = 1) + labs(x="Test A", y = "Density") + stat_function(fun = dnorm, args = list(mean = mean(???, na.rm = TRUE), sd = sd(???, na.rm = TRUE)), colour = "black", size = 1)
但是我不知道我在"???"字段中写了什么用上面的语法.
but I don't know what I write in the field "???" in the above syntax.
谢谢.
最佳.
推荐答案
您可以执行以下操作来获得分数或仅提供给定Test数据的子集
You can do the following to get scores or just subsets of data given Test
data <- data.frame(
Participant= c(1, 1, 1, 1, 2, 2, 2, 2),
Tests = c("A", "B", "C", "D", "A", "B", "C", "D"),
Scores = c(8, 7, 1, 6, 9, 5, 2, 5)
)
sub <- by(data = data, INDICES = data$Tests, FUN = c)
不清楚您要使用ggplot
做什么,但是如果您想为每个测试绘制直方图,可以这样做:
It's a bit unclear of what you try to do with ggplot
but if you want to plot histogram for each Test can do like this:
hist(x = sub$A$Scores, freq = FALSE, xlab = "Scores", main = "Test A")
或使用ggplot
ggplot(data, aes(x=Scores, color=Tests, fill=Tests)) +
geom_histogram(aes(y=..density..), position="identity", alpha=0.5, bins=10)
可以通过以下简单的方差分析图进行绘制:
Simple ANOVA plots can be done as following:
ggplot(data, aes(x = Tests, y = Scores)) +
geom_boxplot(fill = "grey80", colour = "blue") +
scale_x_discrete() + xlab("Treatment Group") +
ylab("Dried weight of plants")
有关方差分析和绘图的更多信息,请参见本教程
for more on ANOVA and plotting see this tutorial
这篇关于如何在重复测量设计中获取长格式数据的直方图得分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!