Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用ggplot反转ecdf图中的x轴_R_Graph_Plot_Ggplot2_Ecdf - Fatal编程技术网

R 使用ggplot反转ecdf图中的x轴

R 使用ggplot反转ecdf图中的x轴,r,graph,plot,ggplot2,ecdf,R,Graph,Plot,Ggplot2,Ecdf,如何使用ggplot()函数(不是qplot()函数)为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

如何使用
ggplot()
函数(不是
qplot()
函数)为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))

test1颠倒x轴的顺序将使ecdf函数重新计算新范围内的累积概率(如tonytonov所述)

这是一个解决方法:

#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()

在那里。现在,它是可切割的。你如何定义“反向ecdf”??比如,
1-edcf()
?@MrFlick,我指的不是ecdf的反转功能,而是反转x轴。这就是我使用scale_x_reverse(lim=c(12,0))的原因。现在我明白问题的标题是错误的。我编辑了它。你认为现在清楚了吗?“有一个类似的问题:
scale
stat
之前应用。与
coord_trans
相反,谢谢!我还试着反转两个轴,它看起来像上面的图(scale_y_reverse(breaks=seq(1,0,-0.2))+scale_x_reverse(breaks=seq(15,1,-1)))。你认为它在数学上也有意义吗?我必须做一些测试,但如果情节是一样的,我想它是可行的@用户30314