1

使用以下数据,即总秒数:

$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."

在此处输入图像描述

我需要创建一天作为一个因素吗?有没有另一种方法来创造这些休息?

4

2 回答 2

2

xlim()您正在使用,然后使用向 x 轴添加比例scale_x_discrete()。相反,您应该使用 limits 参数 withing 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)
于 2013-07-01T03:26:46.513 回答
0

如果有人好奇,我最终使用了带有特定中断的 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")
于 2013-07-01T18:39:25.090 回答