Question

Using the following data, which is a total number of seconds:

$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

I pull the data into R:

df<-read.delim("date-subtraction.csv",sep=",",header=F)
df<-data.frame(seconds=df$V1,days=df$V1/86400)

I create a 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)

enter image description here

When I try to make the x-axis labels break at specific points, I receive a an odd message about adding another scale, the graph changes and the lables seem to stack on top of each other:

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

enter image description here

Do I need to create a days as a factor? Is there another way to create these breaks?

Était-ce utile?

La solution

You are adding a scale to the x axis with xlim() and then with scale_x_discrete(). Instead, you should use the limits argument 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)

Autres conseils

If anyone is curious, I ended up using scale_x_continuous() with specific breaks, which is similar to the @alexwhan solution. Thanks for the help! You all can see the code below:

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")
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top