library(readxl)
vital_0509 <- read.csv("vital_0509.csv")
View(vital_0509)
colnames(vital_0509)<-c("group","sbp_0.0","sbp_0.5","sbp_2.0","sbp_4.0","sbp_6.0","sbp_8.0","dbp_0.0",
                        "dbp_0.5","dbp_2.0","dbp_4.0","dbp_6.0","dbp_8.0","mbp_0.0","mbp_0.5","mbp_2.0",
                        "mbp_4.0","mbp_6.0","mbp_8.0","hr_0.0","hr_0.5","hr_2.0","hr_4.0","hr_6.0","hr_8.0")

vital_0509$group<-factor(vital_0509$group)
str(vital_0509)
## 'data.frame':    120 obs. of  25 variables:
##  $ group  : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
##  $ sbp_0.0: int  128 137 96 119 98 112 156 103 134 107 ...
##  $ sbp_0.5: int  113 103 92 105 114 108 72 97 128 107 ...
##  $ sbp_2.0: int  118 113 91 91 106 101 107 99 112 107 ...
##  $ sbp_4.0: int  100 115 90 91 111 105 108 94 100 113 ...
##  $ sbp_6.0: int  96 104 92 94 130 108 107 100 117 113 ...
##  $ sbp_8.0: int  95 111 97 86 111 110 109 101 131 115 ...
##  $ dbp_0.0: int  71 83 59 77 75 67 80 64 69 70 ...
##  $ dbp_0.5: int  64 65 57 60 73 64 56 60 70 59 ...
##  $ dbp_2.0: int  62 75 55 58 77 58 58 57 56 55 ...
##  $ dbp_4.0: int  57 65 57 56 77 57 57 62 60 45 ...
##  $ dbp_6.0: int  56 66 56 55 86 67 82 59 79 60 ...
##  $ dbp_8.0: int  56 74 69 52 78 68 76 60 107 47 ...
##  $ mbp_0.0: int  84 102 68 85 80 66 98 72 86 78 ...
##  $ mbp_0.5: int  76 83 64 66 82 57 59 68 81 70 ...
##  $ mbp_2.0: int  76 90 63 66 83 59 69 68 70 67 ...
##  $ mbp_4.0: int  68 88 65 64 85 59 69 70 69 62 ...
##  $ mbp_6.0: int  65 81 64 69 97 60 87 69 89 72 ...
##  $ mbp_8.0: int  65 87 75 60 100 61 79 69 113 63 ...
##  $ hr_0.0 : int  64 68 59 66 54 66 65 62 64 64 ...
##  $ hr_0.5 : int  56 67 64 72 62 57 68 65 79 69 ...
##  $ hr_2.0 : int  54 70 60 62 63 59 58 62 70 63 ...
##  $ hr_4.0 : int  54 68 63 61 57 59 59 63 67 67 ...
##  $ hr_6.0 : int  53 66 63 62 62 60 57 61 63 68 ...
##  $ hr_8.0 : int  53 56 57 59 60 61 63 63 63 63 ...
data_bp<-vital_0509[,1:19]
library(tidyr)
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
bp_long<-data_bp%>%gather(time,bp,2:19)
bp_long1<-bp_long%>%separate(time,into=c("measurement","time"),sep="_")
sbp_long<-filter(bp_long1,measurement=="sbp")
bp_long1$time<-as.numeric(bp_long1$time)    

library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
cdata <- ddply(sbp_long, c("group", "time"), summarise,
               N    = length(bp),
               mean = mean(bp,na.rm=T),
               sd   = sd(bp,na.rm=T),
               se   = sd / sqrt(N) )

cdata
##    group time  N     mean       sd       se
## 1      1  0.0 40 125.6250 20.44215 3.232187
## 2      1  0.5 40 114.7000 18.09689 2.861370
## 3      1  2.0 40 106.4750 16.98112 2.684950
## 4      1  4.0 40 109.3333 15.83855 2.504295
## 5      1  6.0 40 111.5882 16.16790 2.556370
## 6      1  8.0 40 106.4583 14.74413 2.331252
## 7      2  0.0 40 126.2250 18.87542 2.984466
## 8      2  0.5 40 118.6667 19.02538 3.008176
## 9      2  2.0 40 120.6410 17.88926 2.828540
## 10     2  4.0 40 122.5128 17.85301 2.822809
## 11     2  6.0 40 119.5758 17.64942 2.790618
## 12     2  8.0 40 123.1250 19.01885 3.007144
## 13     3  0.0 40 123.5000 17.05647 2.696864
## 14     3  0.5 40 114.5500 14.61813 2.311329
## 15     3  2.0 40 121.9500 15.86796 2.508946
## 16     3  4.0 40 128.2250 15.96228 2.523857
## 17     3  6.0 40 130.7222 17.99780 2.845701
## 18     3  8.0 40 134.0526 17.17063 2.714914
library(Rmisc)
## Loading required package: lattice
sbp_long2  <- summarySE(sbp_long, measurevar="bp", groupvars=c("group", "time"),conf.interval=0.95,na.rm=TRUE)
sbp_long2
##    group time  N       bp       sd       se        ci
## 1      1  0.0 40 125.6250 20.44215 3.232187  6.537716
## 2      1  0.5 40 114.7000 18.09689 2.861370  5.787666
## 3      1  2.0 40 106.4750 16.98112 2.684950  5.430825
## 4      1  4.0 39 109.3333 15.83855 2.536198  5.134264
## 5      1  6.0 34 111.5882 16.16790 2.772772  5.641248
## 6      1  8.0 24 106.4583 14.74413 3.009634  6.225902
## 7      2  0.0 40 126.2250 18.87542 2.984466  6.036652
## 8      2  0.5 39 118.6667 19.02538 3.046498  6.167313
## 9      2  2.0 39 120.6410 17.88926 2.864574  5.799027
## 10     2  4.0 39 122.5128 17.85301 2.858770  5.787278
## 11     2  6.0 33 119.5758 17.64942 3.072369  6.258211
## 12     2  8.0 16 123.1250 19.01885 4.754713 10.134430
## 13     3  0.0 40 123.5000 17.05647 2.696864  5.454923
## 14     3  0.5 40 114.5500 14.61813 2.311329  4.675105
## 15     3  2.0 40 121.9500 15.86796 2.508946  5.074821
## 16     3  4.0 40 128.2250 15.96228 2.523857  5.104983
## 17     3  6.0 36 130.7222 17.99780 2.999633  6.089578
## 18     3  8.0 19 134.0526 17.17063 3.939212  8.275977
str(sbp_long2)
## 'data.frame':    18 obs. of  7 variables:
##  $ group: Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 2 2 2 2 ...
##  $ time : chr  "0.0" "0.5" "2.0" "4.0" ...
##  $ N    : num  40 40 40 39 34 24 40 39 39 39 ...
##  $ bp   : num  126 115 106 109 112 ...
##  $ sd   : num  20.4 18.1 17 15.8 16.2 ...
##  $ se   : num  3.23 2.86 2.68 2.54 2.77 ...
##  $ ci   : num  6.54 5.79 5.43 5.13 5.64 ...
sbp_long2$time<-as.numeric(sbp_long2$time)
sbp_long2$group<-as.character(sbp_long2$group)
sbp_long2[1:6,1]<-"P"
sbp_long2[7:12,1]<-"K1"
sbp_long2[13:18,1]<-"K2"
str(sbp_long2)
## 'data.frame':    18 obs. of  7 variables:
##  $ group: chr  "P" "P" "P" "P" ...
##  $ time : num  0 0.5 2 4 6 8 0 0.5 2 4 ...
##  $ N    : num  40 40 40 39 34 24 40 39 39 39 ...
##  $ bp   : num  126 115 106 109 112 ...
##  $ sd   : num  20.4 18.1 17 15.8 16.2 ...
##  $ se   : num  3.23 2.86 2.68 2.54 2.77 ...
##  $ ci   : num  6.54 5.79 5.43 5.13 5.64 ...
library(ggplot2)
pd <- position_dodge(0.2)
ggplot(sbp_long2,aes(x=time,y=bp,col=group,group=group))+
  scale_x_continuous("Time (min)",breaks=seq(0,9,1),expand=c(0,0))+
  scale_y_continuous("Systolic Blood Pressure (mmHg)",breaks=seq(30,190,20),expand=c(0,0))+
  geom_point(aes(shape=group),size =3, position=pd,na.rm=T,alpha=0.5)+
  geom_line(position=pd,size=1)+
  geom_errorbar(aes(ymin=bp-ci,ymax=bp+ci),width=.5, position=pd) +
  coord_cartesian(xlim = c(-1,8.5), ylim = c(70,160))+
  theme_classic() + 
  theme(text=element_text(size=12,family="serif"))+
  theme(legend.position = c(0.8,0.9)) +
  theme(legend.title = element_blank())