[英]How to create x-axis breaks in a CDF in R?
使用以下數據,這是總秒數:
$head date-subtraction_total_seconds.csv
15806856.0
15806970.0
190922.0
860863.0
33441.0
15806835.0
84041.0
17197453.0
17195029.0
-48.0
我將數據放入R:
df<-read.delim("date-subtraction.csv",sep=",",header=F)
df<-data.frame(seconds=df$V1,days=df$V1/86400)
我創建一個CDF:
ggplot(df, aes(x=df$days, y=ecdf(df$days)(df$days)))+
geom_step(size=2.2,color="red")+geom_step(color="cyan",size=1.5)+
scale_y_continuous(labels = percent_format(), limits=c(0,1))+
labs(x="Time (days)", y="% Total")+
ggtitle("Cumulative Distritubtion Function")+
xlim(-1,8)
當我嘗試使x軸標簽在特定點處斷裂時,我收到一條奇怪的消息,關於添加另一個比例,圖形發生了變化,標簽似乎彼此重疊:
ggplot(df, aes(x=df$days, y=ecdf(df$days)(df$days)))+
geom_step(size=2.2,color="red")+geom_step(color="cyan",size=1.5)+
scale_y_continuous(labels = percent_format(), limits=c(0,1))+
labs(x="Time (days)", y="% Total")+
ggtitle("Cumulative Distritubtion Function")+
xlim(-1,8)+
scale_x_discrete(breaks = c(0,1,2,3,4,6,7))
"Scale for 'x' is already present. Adding another scale for 'x', which will replace the existing scale."
我需要創建天數作為因素嗎? 有沒有其他方法可以創建這些休息時間?
您正在使用xlim()
然后使用scale_x_discrete()
將縮放比例添加到x軸。 相反,您應該將limit參數與scale_x_discrete()
:
ggplot(df, aes(x=df$days, y=ecdf(df$days)(ComplianceDateDiff$days)))+
geom_step(size=2.2,color="red")+geom_step(color="cyan",size=1.5)+
scale_y_continuous(labels = percent_format(), limits=c(0,1))+
labs(x="Time (days)", y="% Total")+
ggtitle("Cumulative Distritubtion Function")+
scale_x_discrete(breaks = c(0,1,2,3,4,6,7), limits = c(-1, 8)
如果有人好奇,我最終使用帶有特定中斷的scale_x_continuous(),類似於@alexwhan解決方案。 謝謝您的幫助! 大家都可以看到下面的代碼:
ggplot(df, aes(x=df$days, y=ecdf(df$days)(df$days)))+
geom_step(size=1.2,color="red",alpha=0.8)+geom_step(color="cyan")+
scale_x_continuous(limits=c(-1,8),breaks=c(0,1,2,3,4,5,6,7))+
scale_y_continuous(labels = percent_format(), limits=c(0,1),breaks=c(.0,.33,.5,.75,1))+
labs(x="Time (days)", y="% Compliance")+
ggtitle("Cumulative Distritubtion Function")
#scale_x_discrete(breaks = c(0,1,2,3,4,6,7), limits = c(-1, 7))
#geom_hline(yintercept = .3333, color="orange",size=1,linetype = "dashed")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.