2013-07-24 5 views
3

ggplot을 사용하여 x 축에 날짜가있는 가로 막 대형 차트를 만들고 싶습니다. 여기 내가 가지고있는 코드는 다음과 같습니다.ggplot의 날짜 서식 지정 가로 막대 그래프

df <- data.frame(patient= c('Harry','John'), pain_date = c(as.POSIXct('13-04-2000', format = '%d-%m-%Y'), as.POSIXct('12-02-2000', format = '%d-%m-%Y')), agony_date = c(as.POSIXct('25-05-2000', format = '%d-%m-%Y'),as.POSIXct('21-05-2000', format = '%d-%m-%Y')), death_date = c(as.POSIXct('30-06-2000', format = '%d-%m-%Y'), as.POSIXct('23-11-2000', format = '%d-%m-%Y'))) 
ggplot(df, aes(x=patient)) + 
geom_linerange(aes(ymin=pain_date, ymax=agony_date), color="red", size=5) + 
geom_linerange(aes(ymin=agony_date, ymax=death_date), color="black", size=5) + 
coord_flip() 

한계는 JAN에서 DEC으로 제한하고 매월 첫날에 진드기를 넣는 방법은 무엇입니까?

내가 좋아하는 몇 가지를 시도했다 :

scale_x_datetime(major="months") 
scale_x_datetime(lim = c(as.POSIXct("01-01-2000"), as.POSIXct("31-12-2000"))) 

그러나 그것은 나에게 다음과 같은 오류 메시지가 있습니다 : 당신의 도움이 위의 의견을 요약

+0

오류 메시지가 분명하지 않습니까? 클래스 POSIXct가 클래스 Date가 아닙니다. – Roland

+0

또 다른 문제는 scale_x_datetime()을 사용하지만 scale_y_date()를 사용해야한다는 것입니다. 날짜가 y 축에 있고 그 다음에 x로 뒤집 으면서 비늘이 뒤집기 전에 형식이 지정됩니다. –

답변

4

, 코드에 대한

Erreur : Invalid input: date_trans works with objects of class Date only 

감사합니다 다음과 같이 표시되어야합니다.

require(scales) 
df[, "pain_date"] <- as.Date(df[, "pain_date"]) 
df[, "agony_date"] <- as.Date(df[, "agony_date"]) 
df[, "death_date"] <- as.Date(df[, "death_date"]) 

ggplot(df, aes(x=patient)) + 
    geom_linerange(aes(ymin=pain_date, ymax=agony_date), color="red", size=5) + 
    geom_linerange(aes(ymin=agony_date, ymax=death_date), color="black", size=5) + 
    coord_flip() + scale_y_date(lim = c(as.Date("2000-01-01"), as.Date("2000-12-31")), 
           breaks=date_breaks(width = "1 month"))