1

I am trying to plot the completeness of the follow-up plot in r but I am experiencing a problem with x-axis in plotting date in a proper format although I put it in the correct format in the dataset using POSIXct

My code and sample dataset:

Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)

str(Date.Interventon )
library(car)
scatterplot( OS.years ~ Date.Interventon | Status, data=data.fu.lorenzoo, 
             xlab = "Date of procedure",
             ylab = "Follow-up (years)", 
             smooth = F,  # Removes smooth estimate
             regLine = T) # Removes linear estimate


The resulting plot is shown below. I did many trials but I am still struggling. Any advice will be greatly appreciated. enter image description here

Mohamed Rahouma
  • 1,084
  • 9
  • 20

2 Answers2

1

Probably not the best solution, you need separately format the axis.

Date.Interventon = as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60
    Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
    OS.years <-c(11,13,14,13,13,10,13,14,10,11)
    
    data.fu.lorenzoo= data.frame(Date.Interventon, Status, OS.years)
    
    
    
    
    library(car)
    scatterplot( OS.years ~ Date.Interventon| Status, data=data.fu.lorenzoo, 
                 xlab = "Date of procedure",
                 ylab = "Follow-up (years)", 
                 smooth = F,  # Removes smooth estimate
                 regLine = T,
                 axes=F) # Removes linear estimate
    axis(2)
    axis(1, Date.Interventon, format( as.POSIXct("1/27/2017", format="%m/%d/%y") + 1:40*60, cex.axis = .7))

enter image description here

Rfanatic
  • 2,224
  • 1
  • 5
  • 21
  • Thx for your precious output. Upvoted. Would it be possible to put only years and months without days, hours and minutes and to align those dates vertically so we can put many dates. Appreciate your precious input. Please, if possible, try to upvote my question. – Mohamed Rahouma Dec 02 '21 at 12:51
1

I am not sure that it is what you need but, from your dataset, I assume that you want to see the minutes in x-axis, because days and hours are similar to each other.

with using minute from lubridate package, you can change your x-axis in a proper way.

car::scatterplot( OS.years ~ minute( Date.Interventon) | Status, data=data.fu.lorenzoo, 
        xlab = "Date of procedure",
        ylab = "Follow-up (years)", 
        smooth = F,  # Removes smooth estimate
        regLine = T) # Removes linear estimate

By using different packages, like ggplot2 we can produce similar plot.

Before going forward I updated Data.Inverventon to represent different years. Then created yearmonth variable using as.yearmon function from zoo package.

Date.Interventon = as.POSIXct('1/27/2017', format='%m/%d/%Y') + 1:40*60000000
Status<- c ("Alive","Dead","Dead","Dead","Alive","Alive","Dead","Dead","Alive","Dead")
OS.years <-c(11,13,14,13,13,10,13,14,10,11)
data.fu.lorenzoo= data.frame(Date.Interventon,Status, OS.years)
data.fu.lorenzoo$yearmonth = zoo::as.yearmon(data.fu.lorenzoo$Date.Interventon)

then you can plot a similar graph like

ggplot(data.fu.lorenzoo, aes(x = yearmonth, y = OS.years, color = Status)) + 
  geom_point() + geom_smooth(method = lm, se = FALSE) +
  labs( x = "Date of procedure", y = "Follow-up (years)") +
  theme_minimal()

Output is like that,

enter image description here

patula
  • 136
  • 5
  • Thx for your precious input. Upvoted. Would it be possible to plot both months and years instead of minutes. Regards. Please, if possible, try to upvote my question. – Mohamed Rahouma Dec 02 '21 at 12:53
  • 1
    Hi Mohamed, there is another function which is `as.yearmon` from `zoo` package. you can change format of your date like Jan 2020, however, when you put in your x axis, we cannot see the month. I cannot figure out why but it only shows year. may be it will help you. If I find solution, I will update my answer. – patula Dec 02 '21 at 14:06
  • Thx a lot. Upvoted. I used it and it makes sense now. ```car::scatterplot( OS.years ~ zoo::as.yearmon ( Date.Interventon) | Status, data=data.fu.lorenzoo, xlab = "Date of procedure", ylab = "Follow-up (years)", smooth = F, regLine = T)``` – Mohamed Rahouma Dec 02 '21 at 14:24
  • 1
    I also reproduce the graph using ggplot, maybe it can be better for you – patula Dec 02 '21 at 14:28
  • Thx a lot. upvoted your comments. – Mohamed Rahouma Dec 02 '21 at 18:07