Search code examples
rdateggplot2axis-labels

How to add custom date vector into plot?


I have a date vector called prod.dat.ordered.distinct1 which looks like this:

prod.dat.ordered.distinct1 <- c("2012-07", "2012-08", "2012-09", "2012-10", "2012-11", "2012-12", 
"2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06", 
"2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12"
)

Now in my plot I receive on the y-axis the numbers 1 to 18 instead of my prod.dat.ordered.distinct1 vector... What changes do I have to make in order to get my date values on the y-axis? Here is my code:

mydatal <- testdatensatz[testdatensatzSbaureihe == "F10",] 
prod.dat1 <- as.Date(mydata1$produktionsdatumf2, format = "%Y-%m-%d")
prod.dat1 <- format(prod.dat1, format = "%Y-%m") 
befund1 <- mydata1$befundnummer 
prod.dat.befund1 <- data.frame(p = prod.dat1, b = befund1) 

prod.dat.befund.ordered1 <- prod.dat.befund1[order(prod.dat.befund1$p), ]

prod.dat.ordered.distinct1 <- prod.dat.befund.ordered1$p[!duplicated(prod.dat.befund.ordered1$p)]

t1 <- as.data.frame(table(prod.dat.befund1))
matt = matrix(nrow = length(prod.dat.ordered.distinctl), ncol = 2) 
Produktionsdatum <- c() 
relativeHaufigkeit <- c() 
for (i in 1:length(prod.dat.ordered.distinct1)) 
{ 
  tl.date <- tl[t1$p == prod.dat.ordered.distinct1[i], ] 
  number.ref1 <- tl.date[tl.date$b == 0, 3] 
  number.faults1 <- sum(t1.date[as.numeric(t1.date$b) > 1, 3]) 
  Produktionsdatum[i] <- prod.dat.ordered.distinctl[i]
  relativeHaufigkeit[i]<- round((number.faults1 / number.refl)  * 100, digits = 0) 
}

df1 <- data.frame(Produktionsdatum, relativeHaufigkeit) 
df1$gr <- df1$Produktionsdatum %in% r 
ggplot(df1, aes(x=Produktionsdatum,y=relativeHaufigkeit, color=gr)) +
  geom_vline(xintercept=r1, alpha=0.5) + 
  geom_point()

Solution

  • Ther problem in the line with

    Produktionsdatum[i] <- prod.dat.ordered.distinct1[i]

    It converts factors into integer. If you wrap it into as.character function, then dates will be indicated at the x-axis. Moreover if you would like to use geom_vline you need to use separate aesthetics.

    Please see the code below:

    prod.dat.ordered.distinct1 <- c("2012-07", "2012-08", "2012-09", "2012-10", "2012-11", "2012-12", 
                                    "2013-01", "2013-02", "2013-03", "2013-04", "2013-05", "2013-06", 
                                    "2013-07", "2013-08", "2013-09", "2013-10", "2013-11", "2013-12"
    )
    
    testdatensatz <- data.frame(baureihe = "F10",
                              produktionsdatumf2 = c("2012-08-02", "2012-08-03", "2012-09-01", "2012-10-05"),
                              befundnummer = c(1, 0, 2, 1))
    
    relativeHaufigkeit <- c(1, 2, 3, 1)
    t1 <- data.frame(p = factor("2012-07", "2012-08", "2012-09", "2012-10"), b = 0)
    
    
    
    mydata1 <- testdatensatz[testdatensatz$baureihe == "F10",] 
    
    prod.dat1 <- as.Date(mydata1$produktionsdatumf2, format = "%Y-%m-%d")
    prod.dat1 <- format(prod.dat1, format = "%Y-%m") 
    befund1 <- mydata1$befundnummer 
    prod.dat.befund1 <- data.frame(p = prod.dat1, b = befund1) 
    
    prod.dat.befund.ordered1 <- prod.dat.befund1[order(prod.dat.befund1$p), ]
    
    prod.dat.ordered.distinct1 <- prod.dat.befund.ordered1$p[!duplicated(prod.dat.befund.ordered1$p)]
    
    t1 <- as.data.frame(table(prod.dat.befund1))
    matt = matrix(nrow = length(prod.dat.ordered.distinct1), ncol = 2) 
    Produktionsdatum <- c() 
    relativeHaufigkeit <- c() 
    
    for (i in 1:length(prod.dat.ordered.distinct1)) { 
      t1.date <- t1[t1$p == prod.dat.ordered.distinct1[i], , drop = FALSE] 
      # number.ref1 <- t1.date[t1.date$b == 0, 3] 
      number.ref1 <- t1.date[t1.date$b == 0, 3] 
      number.faults1 <- sum(t1.date[as.numeric(t1.date$b) > 1, 3]) 
      Produktionsdatum[i] <- as.character(prod.dat.ordered.distinct1[i]) #
      relativeHaufigkeit[i]<- round((number.faults1 / number.ref1)  * 100, digits = 0) 
    }
    
    
    df1 <- data.frame(Produktionsdatum, relativeHaufigkeit, gr = sample(1:2, length(Produktionsdatum), replace = TRUE)) 
    
    ggplot(df1, aes(x = Produktionsdatum, y = relativeHaufigkeit, color = gr)) +
      geom_line() +
      geom_vline(aes(xintercept = 1, alpha=0.5)) + 
      geom_point()
    

    Output:

    plotted