R語言可視化ggplot2繪制24小時動態血糖圖

數據格式

sample=read.csv("sample.csv",header = T,stringsAsFactors = F)head(sample)

創建等距時間序列的方法(分鐘)

time=strptime("00:00:00","%H:%M:%S")+300*0:287 #沒有填日期的時候,默認日期為今天

畫圖代碼

#time首先要轉換成 POSIXct 格式, 下面的breaks一樣,geom_datetime接受的隻有POSIXct格式。
sample$time=as.POSIXct(sample$time)
#breaks都是自定義的,選取哪個時間點都可以
ggplot(sample,aes(x=time,y=Glucose,group=1))+geom_line()+scale_x_datetime(
  limits = c(as.POSIXct("2020-03-24 00:00:00 CST"),as.POSIXct("2020-03-24 23:59:59 CST")),
  breaks = c(as.POSIXct("2020-03-24 00:00:00 CST"),as.POSIXct("2020-03-24 04:00:00 CST"),as.POSIXct("2020-03-24 07:00:00 CST"),as.POSIXct("2020-03-24 12:00:00 CST"),as.POSIXct("2020-03-24 18:00:00 CST"),as.POSIXct("2020-03-24 22:00:00 CST")),
  labels = c("00:00","04:00","07:00","12:00","18:00","22:00")
             )+scale_y_continuous(
               breaks = c(6.0,6.1,6.5,7.0,7.5,8.0,8.5),labels = c("6.0","6.1","6.5","7.0","7.5","8.0","8.5")
             )+theme_bw() +
             theme(axis.text = element_text(size=16),axis.title = element_text(size=24)) #軸標簽的大小
             + geom_hline(yintercept = c(6.1)) #添加一水平線,6.1是正常血糖高值

以上就是R語言可視化ggplot2繪制24小時動態血糖的詳細內容,更多關於ggplot2繪制動態血糖的資料請關註LevelAH其它相關文章!

推薦閱讀: