Mohamed Rahouma
Mohamed Rahouma

Reputation: 1236

How to plot date in a proper format in x-axis in the completeness of the follow-up plot in r car package?

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

Upvotes: 1

Views: 75

Answers (2)

patula
patula

Reputation: 136

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

Upvotes: 1

Rfanatic
Rfanatic

Reputation: 2282

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

Upvotes: 1

Related Questions