2014-12-07 20 views
3

qplot() 함수가 아닌 ggplot() 함수를 사용하여 ecdf에 대한 역 x 축을 플로팅하려면 어떻게해야합니까?ggplot을 사용하여 ecdf 플롯에서 x 축 반전

다음 코드는 작동하지 않습니다

test1 <- 
structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L), ProductType = c("productA", 
"productA", "productA", "productA", "productA", "productA", "productA", 
"productA", "productA", "productA", "productB", "productB", "productB", 
"productB", "productB", "productB", "productB", "productB", "productB", 
"productB"), ConsumptionDays = structure(c(29, 98, 164, 96, 233, 
14, 12, 264, 97, 47, 27, 133, 28, 63, 420, 105, 67, 160, 22, 
41), class = "difftime", units = "days")), .Names = c("ID", "ProductType", 
"ConsumptionDays"), row.names = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L 
), class = "data.frame") 

ggplot(data=test1, aes(as.numeric(ConsumptionDays)/30 , color=ProductType)) + 
    geom_line(stat='ecdf', size = 0.9) + 
    scale_x_reverse(lim=c(15,0)) 

enter image description here

scale_x_reverse(lim=c(15,0))없이 음모처럼 될 것이다 다음

enter image description here

+0

:

는 해결 방법입니다. 이제는 잘라 붙일 수 있습니다. –

+2

"reverse ecdf"는 어떻게 정의합니까? '1-edcf()'처럼? – MrFlick

+1

@ MrFlick, 나는 ecdf의 반대 기능이 아니라 x 축의 반대 기능을 의미했습니다. 그래서 scale_x_reverse (lim = c (12,0))를 사용했습니다. 이제 질문 제목이 잘못되었음을 이해합니다. 나는 그것을 편집했다. 지금은 분명하다고 생각하니? – user30314

답변

2

x 축의 순서를 반대로는 것이다 새로운 범위에 걸쳐 누적 확률을 재 계산하도록 ecdf 함수를 만듭니다 (sta tonytonov에 의해 ted). 이

#Consumption Months 
df = data.frame(test1, C_months = as.numeric(test1$ConsumptionDays/30)) 
#Order from smallest to greatest based on Comsumption Months 
df = df[with(df, order(ProductType,C_months)), ] 

#Get cumulative observations in a scale from 0 to 1 
subA = subset(df,df$ProductType == "productA") 
seqA = c(seq(1/nrow(subA),1,1/nrow(subA))) 
subB = subset(df,df$ProductType == "productB") 
seqB = c(seq(1/nrow(subB),1,1/nrow(subB))) 
cumulated = c(seqA,seqB) 
#Add cumulative observations to data.frame 
df = data.frame(df,cum=cumulated) 

ggplot(data=df, aes(C_months,cum, group=ProductType, color=ProductType)) + 
    geom_line(size=1.5) + 
    scale_x_reverse(limits=c(15,0),"Comsumption Months") + 
    ylab("Cumulative of observations") + 
    theme_bw() 

enter image description here

+0

감사! 나는 또한 위의 플롯 (scale_y_reverse (breaks = seq (1,0, -0.2)) + scale_x_reverse (breaks = seq (15,1, -1)))처럼 보인다. 수학적으로도 의미가 있다고 생각합니까? – user30314

+1

몇 가지 테스트를 해봐야 겠지만, 플롯이 동일하다면 제대로 작동하는 것 같습니다. @ user30314 –