整理資料

dat <- read.table("coping.txt", head=T)
dta <- stack(dat)
## Warning in stack.data.frame(dat): non-vector columns will be ignored
dta$ID <- c(rep("S2",6), rep("S17",6), rep("S76",6),
            rep("S135",6), rep("S137",6), rep("S139",6),
            rep("S185",6), rep("S203",6), rep("S233",6),
            rep("S269",6), rep("S302",6), rep("S317",6),
            rep("S363",6), rep("S372",6))
dta$situation <-
  c(rep(c("Fail","NoPart","TeacNo","Bully","Work","MomNo"), 14))
str(dta)
## 'data.frame':    672 obs. of  4 variables:
##  $ values   : num  4 4 2 4 4 4 3 3 3 4 ...
##  $ ind      : Factor w/ 8 levels "annoy","sad",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ ID       : chr  "S2" "S2" "S2" "S2" ...
##  $ situation: chr  "Fail" "NoPart" "TeacNo" "Bully" ...
head(dta)
##   values   ind ID situation
## 1      4 annoy S2      Fail
## 2      4 annoy S2    NoPart
## 3      2 annoy S2    TeacNo
## 4      4 annoy S2     Bully
## 5      4 annoy S2      Work
## 6      4 annoy S2     MomNo

點狀圖

library(ggplot2)
p0 <- ggplot(data=dta, 
             aes(x=situation, 
                 y=values)) +
  geom_point(data=dta, aes(color=ind))
p0

了解不同情境的不同心情

ggplot(dta, aes(ind, values))+
  geom_boxplot(col='blue') +
  geom_point(aes(color=ind)) +
  facet_wrap(. ~ situation) +
  theme_bw()

Error bars

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
dta.1 <- dta %>% group_by(situation, ind) %>%
  summarize(mean=mean(values), se=sd(values)/sqrt(n()))
dta.1
## # A tibble: 48 x 4
## # Groups:   situation [6]
##    situation ind        mean    se
##    <chr>     <fct>     <dbl> <dbl>
##  1 Bully     annoy      3    0.277
##  2 Bully     sad        2.43 0.228
##  3 Bully     afraid     1.57 0.228
##  4 Bully     angry      2.14 0.294
##  5 Bully     approach   1.90 0.196
##  6 Bully     avoid      2.64 0.194
##  7 Bully     support    2.33 0.279
##  8 Bully     agressive  1.79 0.281
##  9 Fail      annoy      2.64 0.248
## 10 Fail      sad        1.64 0.169
## # ... with 38 more rows
p1 <- ggplot(data=dta.1) +
  aes(ind, mean, group=situation, shape=situation) +
  geom_errorbar(aes(ymin=mean - se,
                    ymax=mean + se),
                width=.4, size=.5, 
                position=position_dodge(.5))+
   geom_point(aes(color = situation), size = 3, 
                position=position_dodge(.5))+
  labs(x="", y="mean_score")+
  theme(legend.position= c(0.07,0.9),)+
  theme_bw()
p1