-1

in this question scale_x_date and how to make it equidistant? i tried to find a solution for the problem of the equidistance in the x-axis caused by the non existence of some months in ym data .a guy there suggested to me use factor() .if fact that wa s ok except that i can not anymore order my x-axis chronologically as i was enjoying in my first plot which i uploaded in my first post. iused scale_x-date in for that plot but this does not work for the factor() trick.

enter image description here

 dt<-data.frame(M_Datum=c("2018-02-05","2018-02-15","2018-02-10","2018-02-13","2017-02-05","2017-02-15","2017-02-10","2017-02-23","2020-02-25","2012-02-15","2020-02-10","2020-02-13"),Yield=c(4,47,18,10,22,50,70,120,150,400,60,78))
        
        dt[,1]<-as.Date(format(as.Date(dt[,1]), "%Y-%m-01"))
       
        dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
      
        
        
        
        mydf=aggregate(Yield ~ M_Datum, dr, length) 
        
        
        koka<-data.frame("M_Datum"=mydf$M_Datum,"Yiel"=mydf$Yield)
        
        
        
        ggplot(koka, aes(x=factor(format(M_Datum, "%b %Y")), y=Yiel,group = 1)) + 
          geom_point(size=7,colour="#EF783D",shape=17) +
          geom_line(color="#EF783D")+
          
          theme(axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=1))+
          theme(axis.text.y.left = element_text(color = "#EF783D"),
                axis.title.y.left = element_text(color = "#EF783D"))+
          ylab("Wafer Quantity")+
          xlab("")
        
        
        
        
        
        
        
        
        
      
      
MrFlick
  • 195,160
  • 17
  • 277
  • 295
samir
  • 17
  • 5
  • It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Maybe this question already has what you need: https://stackoverflow.com/questions/21070033/transform-a-date-to-factor-in-r-keeping-order-in-factor-levels-considering-origi – MrFlick Jul 07 '22 at 13:46
  • @MrFlick i added the code used to get the plot in the middle .it is almost the same used to reproduce the first in the top of the image. – samir Jul 07 '22 at 13:54
  • 1
    But the code isn't runnable with out any data so we can't really test it. Plus it seems to be surrounded by shiny stuff which doesn't seem relevant to the specific plotting question you are asking. Please make sure you provide a minimal [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it easier to help you. – MrFlick Jul 07 '22 at 14:01
  • @MrFlick now it is runnable. if you run it you will the plot in the middle but the problem it is orderd chronologically in the x-axis – samir Jul 07 '22 at 14:17
  • @MrFlick sorry i wanted to write it is not ordered chronologically and i want it orderd – samir Jul 07 '22 at 14:24

1 Answers1

0

You will need to put the levels of the factor in the correct order. You can do that with

ggplot(koka, aes(
  x=factor(format(M_Datum, "%b %Y"), levels=format(sort(unique(M_Datum)),"%b %Y")), 
  y=Yiel,
  group = 1)) + ...

Which is a bit messy so you could write a helper function to clean up up

date_factor <- function(dates, format_string="%b %Y") {
  factor(format(dates, format_string), levels=format(sort(unique(dates)), format_string))
}
ggplot(koka, aes(x=date_factor(M_Datum), y=Yiel,group = 1)) + ...

That results in the following plot

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295