2018年3月13日火曜日

draw the graph to record blood pressure vol.2


Please refer to 2018/2/20 for the detail


#
# read data as csv format and convert to xts to plot
#
Sys.setenv(TZ=Sys.timezone())
bp <- read.csv("~/Downloads/bp - シート1.csv")
system("rm \"$HOME/Downloads/bp - シート1.csv\"")
bp.xts <- xts(bp[,c(-1,-2)],as.POSIXct(paste(bp$Date,bp$Time,sep=" "),tz=Sys.timezone()),tz=Sys.timezone())
# bp.xts <- xts(bp[,c(-1,-2)],as.POSIXct(paste(bp$Date,bp$Time,sep=" ")))
# bp.xts <- xts(bp[,c(-1,-2)],as.POSIXct(paste(bp$Date,bp$Time,sep=" ")),tz="UTC")
# weekly average
apply.weekly(bp.xts[bp.xts$High > 95],mean)
# make UTC based weekly data
# apply.weekly(as.xts(bp[,c(-1,-2)],as.POSIXct(paste(bp$Date,bp$Time,sep=" "),tz="UTC")),mean)
#  draw the graph
plot(bp.xts[,-3][bp.xts$High > 95],col = c("red", "blue"),lwd=c(3,3,2,2),major.ticks='days',grid.ticks.on='days',type='p',ylim=c(60,160))
# draw a horizontal line at 130 as the benchmark
addSeries(xts(rep(130,length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=5,lwd=1)
# draw another line at 85.
addSeries(xts(rep(85,length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=5,lwd=1)
# add aux axis
axis(2,at=c(135,130,125,85))
axis(4,at=c(135,130,125,85))
# draw the horizontal line at the average
addSeries(xts(rep(mean(bp.xts[,1][bp.xts$High > 95]),length(index(bp.xts[bp.xts$High > 95]))),index(bp.xts[bp.xts$High > 95])),ylim=c(60,160),on=1,col=6,lwd=1)


plot(apply.weekly(apply.daily(merge(to.daily(bp.xts[bp.xts$High > 95][,1])[,2],to.daily(bp.xts[bp.xts$High > 95][,2])[,2]),mean),mean),type='p')


bp.tmp <- merge(to.daily(bp.xts[bp.xts$High > 95][,1])[,2],to.daily(bp.xts[bp.xts$High > 95][,2])[,2])
colnames(bp.tmp)[1] <- "high"
colnames(bp.tmp)[2] <- "low"
apply.weekly(bp.tmp,mean)
plot(bp.tmp ,type='p' ,ylim=c(50,170))
addSeries(xts(rep(130,length(index(bp.tmp))),index(bp.tmp)),ylim=c(50,170),on=1,col=5,lwd=1)
addSeries(xts(rep(85,length(index(bp.tmp))),index(bp.tmp)),ylim=c(50,170),on=1,col=5,lwd=1)

0 件のコメント: