3

How can I plot the reverse xaxis for ecdf using ggplot() function (not the qplot() function)?

The following code does not work:

 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

Without scale_x_reverse( lim=c(15,0)) the plot would be like the following:

enter image description here

Andre Silva
  • 4,435
  • 9
  • 44
  • 62
user30314
  • 143
  • 1
  • 10
  • There. Now it's cut-and-pastable. – IRTFM Dec 07 '14 at 23:58
  • 2
    How are you defining "reverse ecdf"?? Like, `1-edcf()`? – MrFlick Dec 08 '14 at 02:46
  • 1
    @MrFlick, I did not mean the reverse function of ecdf but the reverse x axis. This is why I used scale_x_reverse( lim=c(12,0)). Now I understand that the question title was wrong. I edited it. Do you think it is clear now? – user30314 Dec 08 '14 at 10:23
  • [Here](http://stackoverflow.com/questions/3387824/r-ggplot2-using-stat-summary-mean-and-logaritmic-scale)'s a similar problem: `scale` applies before `stat`. With `coord_trans` it is the opposite. – tonytonov Dec 08 '14 at 14:58

1 Answers1

3

Reversing the order of the x axis will make the ecdf function to recalculate the cumulative probabilities over the new range (as stated by tonytonov).

This is a workaround:

#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

Andre Silva
  • 4,435
  • 9
  • 44
  • 62
  • Thanks! I also tried reversing both axis's and it looks like your above plot (scale_y_reverse(breaks = seq(1,0, -0.2))+ scale_x_reverse(breaks = seq(15,1, -1))). Do you think it is mathematically also meaningful? – user30314 Dec 08 '14 at 23:59
  • 1
    I'd have to do some tests, but If the plot is the same I guess it works. @user30314 – Andre Silva Dec 09 '14 at 00:29