0

enter image description here

How can please using ggplot2 package or whatever else package remove that "blank space " between months caused by absence of certain months on my x axis ? In other words to make the x axis looks equidistant and not having those "blank gapes".By the code is very normal ,it is about plotting certain values vs other column containing dates (not all the months are present in that date column ).

 filtredplot1<-reactive({
    req(res_mod())
    dat<-res_mod() 
    dt<-dat[dat$M_Datum >= input$dateRange[1] & dat$M_Datum <= input$dateRange[2],]
    
    dt[,5]<-as.Date(format(as.Date(dt[,5]), "%Y-%m-01"))
    req(dt$M_Datum,dt$Yield)
    dr<-data.frame("M_Datum"=dt$M_Datum,"Yield"=dt$Yield)
  
    
    
    
    mydf=aggregate(Yield ~ M_Datum, dr, length) 
    
    req(mydf$M_Datum,mydf$Yield)
    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")+
      scale_x_date(labels="%b %Y")
      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("")
    
    
    
    
    
    
    
    
    
  })    
  
samir
  • 17
  • 5
  • One way would be to treat the months as factors rather than dates, eg, use something like `aes(x = factor(month))` - you will then have a discrete x scale, rather than a continuous one. You might also need to sort the factors in date order, depending on the format of your data. – Andrew Gustar Jul 07 '22 at 09:45
  • @AndrewGustar of course i need them sorted as in the picture Moth name and year but the problem with your solution is i am getting an error :Invalid input: date_trans works with objects of class Date only – samir Jul 07 '22 at 10:16
  • @AndrewGustar it worked but i lost the line linking the triangles in the plot sitting in the middle and i can not set a date format for x axis – samir Jul 07 '22 at 10:35
  • 1
    Try `scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")`. – Rui Barradas Jul 07 '22 at 11:56
  • @RuiBarradas this does not work because we have saied that you code will bring Months which are not in my data and so there no values (point) to assign to it and will remain empyt which i want to avoid – samir Jul 07 '22 at 12:01
  • @samir If you treat the dates as factors you cannot use scale_x_date, as that will try to put the points in the correct chronological position (i.e. with the gaps for missing months). Instead you will have to format the labels separately - perhaps set a new variable with `date_new = format(date, "%b %Y")` and use that for your x axis. I can't tell why the line is not showing as you have not shared any of your code or data. – Andrew Gustar Jul 07 '22 at 13:43
  • @AndrewGustar so the code is now added to my post – samir Jul 07 '22 at 13:47
  • You say that there are no dates in your data but then have `format` in `aes()`. Why don't you use `aes(x=M_Datum, y=Yiel,group = 1)` instead? – Rui Barradas Jul 07 '22 at 14:10
  • @RuiBarradas please take a look here https://stackoverflow.com/questions/72898903/order-or-sort-the-x-axis-in-ggplot-where-x-axis-is-a-factor-of-dates?noredirect=1#comment128758974_72898903 – samir Jul 07 '22 at 14:25
  • @AndrewGustar please take a look here https://stackoverflow.com/questions/72898903/order-or-sort-the-x-axis-in-ggplot-where-x-axis-is-a-factor-of-dates?noredirect=1#comment128758974_72898903 – samir Jul 07 '22 at 14:25

1 Answers1

1

The code below does not create the data.frames dr and mydf, your data preparation code is too complicated. The following is much simpler and works.
Also, you have the typo Yiel for Yield twice in your code. The first when creating koka and the second in aes().

suppressPackageStartupMessages({
  library(shiny)
  library(ggplot2)
})

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$M_Datum <- as.Date(dt$M_Datum)
req(dt$M_Datum, dt$Yield)
koka <- aggregate(Yield ~ M_Datum, dt, length) 

ggplot(koka, aes(x = M_Datum, y = Yield, group = 1)) + 
  geom_point(size = 7, colour = "#EF783D", shape = 17) +
  geom_line(color = "#EF783D") +
  scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
  xlab("") +
  ylab("Wafer Quantity") +
  theme(
    axis.text.x = element_text(angle = 60, vjust = 1, hjust = 1),
    axis.text.y.left = element_text(color = "#EF783D"),
    axis.title.y.left = element_text(color = "#EF783D")
  )
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66