How to get the scores in a long-format data for histogram in the repeated-measures design

时间秒杀一切 提交于 2019-12-24 19:00:24

问题


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  

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.

Thank you in advance.

Best.


回答1:


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)

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")

or with 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



来源:https://stackoverflow.com/questions/48972955/how-to-get-the-scores-in-a-long-format-data-for-histogram-in-the-repeated-measur

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!