2014-12-12 6 views
1

혈압 데이터를 계획하여 일 (시간에서 시간), 주간에서 매일 볼 수있는 음모를 꾸미려고합니다. 여기에 내가 기대하는 프로토 타입 플롯이 있습니다.플롯에서 일일 및 주간 변동 날짜 데이터

enter image description here

나는 다음과 같은 형식으로해야합니다 데이터를 생성하는 ploting 앱 자동화해야합니다

mydHeart <- data.frame (datetime = paste("12/", rep(1:10, each=4), "/14 ", 
    rep(c("14:14:00", "12:15:00", "18:15:14", "22:15:14"), 4), " PM", sep=""), 
systolic = round(rnorm(40, 120, 5),0), distolic= round(rnorm(40, 80, 3),0)) 

        datetime systolic distolic 
    1 12/1/14 14:14:00 PM  126  76 
    2 12/1/14 12:15:00 PM  126  79 
    3 12/1/14 18:15:14 PM  122  79 
    4 12/1/14 22:15:14 PM  130  84 
    5 12/2/14 14:14:00 PM  122  80 
    6 12/2/14 12:15:00 PM  120  79 
    7 12/2/14 18:15:14 PM  119  82 
    8 12/2/14 22:15:14 PM  126  80 
    9 12/3/14 14:14:00 PM  123  81 
    10 12/3/14 12:15:00 PM  122  84 
    11 12/3/14 18:15:14 PM  126  79 
    12 12/3/14 22:15:14 PM  122  78 
    13 12/4/14 14:14:00 PM  128  74 
    14 12/4/14 12:15:00 PM  114  81 
    15 12/4/14 18:15:14 PM  122  84 
    16 12/4/14 22:15:14 PM  114  81 
    17 12/5/14 14:14:00 PM  126  79 
    18 12/5/14 12:15:00 PM  121  77 
    19 12/5/14 18:15:14 PM  125  86 
    20 12/5/14 22:15:14 PM  128  79 
    21 12/6/14 14:14:00 PM  114  86 
    22 12/6/14 12:15:00 PM  129  81 
    23 12/6/14 18:15:14 PM  117  80 
    24 12/6/14 22:15:14 PM  126  82 
    25 12/7/14 14:14:00 PM  118  88 
    26 12/7/14 12:15:00 PM  114  81 
    27 12/7/14 18:15:14 PM  124  79 
    28 12/7/14 22:15:14 PM  120  76 
    29 12/8/14 14:14:00 PM  119  79 
    30 12/8/14 12:15:00 PM  114  79 
    31 12/8/14 18:15:14 PM  117  85 
    32 12/8/14 22:15:14 PM  117  78 
    33 12/9/14 14:14:00 PM  120  78 
    34 12/9/14 12:15:00 PM  126  83 
    35 12/9/14 18:15:14 PM  121  79 
    36 12/9/14 22:15:14 PM  119  78 
    37 12/10/14 14:14:00 PM  118  79 
    38 12/10/14 12:15:00 PM  128  78 
    39 12/10/14 18:15:14 PM  133  81 
    40 12/10/14 22:15:14 PM  116  81 

내가, 기본 날짜 변환 facilties을 시도하고를하지만, 다음 코드는 재미 플롯을 생산 .

plot(as.Date(mydHeart[,1]), mydHeart[,2], type = "l") 

나는 다음과 같은 시도했지만

내가 어떻게 일의 이름을 인식 어떻게 입력 R. 위의 날짜 스타일

plot(as.Date(mydHeart[,1], '%m-%d-%y'), mydHeart[,2], type = "l") 

Error in plot.window(...) : need finite 'xlim' values 
In addition: Warning messages: 
1: In min(x) : no non-missing arguments to min; returning Inf 
2: In max(x) : no non-missing arguments to max; returning -Inf 

나는 확실하지 않다 수있는 방법을 잘 모르겠습니다 및 오류를 제공 I 그리드 선을 어디에 둘 수 있습니까? 여기

답변

7

내가이 접근 할 방법은 다음과 같습니다

mydHeart$datetime <- as.character(mydHeart$datetime) 
library(lubridate) 
library(reshape2) 
library(ggplot2) 
library(scales) 
## 
mydHeart$datetime <- lubridate::mdy_hms(
    sub("(\\s[A-Z]+)","",mydHeart$datetime)) 
## 
> str(mydHeart) 
'data.frame': 40 obs. of 3 variables: 
$ datetime: POSIXct, format: "2014-12-01 14:14:00" ... 
$ systolic: num 120 121 114 122 113 114 120 117 116 126 ... 
$ distolic: num 80 76 83 81 84 79 80 81 83 77 ... 

위 당신의 datetime 열이 올바른 형식 얻을 것이다; 여기에서 나는 melt 데이터를 사용하고 R의 기본 그래픽을 사용하지 않고 ggplot2으로 플롯을 만들지 만 기본 플롯을 선호 할 수 있습니다.

mData <- melt(
    mydHeart, 
    id.vars="datetime", 
    variable.name="bp_type", 
    value.name="blood_pressure") 
## 
ggplot(
    mData, 
    aes(x=datetime,y=blood_pressure, 
     color=bp_type))+ 
    geom_line()+ 
    scale_x_datetime(
    labels=date_format("%a %m/%d")) 

enter image description here

편집 : 약간의 수정의 부부와 함께, eipi10의 제안 @ 촬영 :

enter image description here

ggplot(
    mData, 
    aes(x=datetime,y=blood_pressure, 
     color=bp_type))+ 
    theme_bw()+ 
    scale_x_datetime(
    breaks="1 day", 
    minor_breaks="6 hours") + 
    coord_cartesian(
    xlim=c(mdy_hms("11/30/14 12:00:00"), 
      mdy_hms("12/10/14 00:00:00")))+ 
    theme(
    panel.grid.minor.x=element_line(
     colour="grey30", linetype="11"), 
    panel.grid.major.x=element_line(
     colour="black"))+ 
    geom_line(size=1.1) 
내가 6 시간에 작은 격자 선 간격을 설정 , 그렇지 않으면 숫자가 꽤 ​​혼잡 해집니다. 그러나 당신은 당신의 취향대로 이것을 확실히 바꿀 수 있습니다.

+3

1 일 간격으로 주요 눈금 선을 얻기 위해'scale_x_datetime (breaks = "1 day")'를 추가하고'theme (panel.grid.major.x = element_line (color = "black"))'를 일 경계를 표시하는 검정색 수직선을 얻습니다. – eipi10

+0

@ eipi10 고맙습니다. 제 답변에 제안을 추가하겠습니다. – nrussell

+0

감사합니다. 시간 간격으로 작은 눈금 선은 어때요? – SHRram

관련 문제