Reputation: 161
I'm working with Cox regression and I'like to represent the following data in ggplot.
id <- c(1, 2, 3, 4, 5, 6, 7,8)
age <- c(54, 60, 50, 45, 62, 60, 80, 85)
dm <- c(0,0,1,1,0,1, 0,1)
time <- c(180, 120, 200, 125, 1000, 100, 150, 300)
event <- c(1, 0, 1, 0, 0, 1, 1, 1)
df<-data.frame(id,age,dm,time,event)
df
vfit <- coxph(Surv(time, event) ~ age + dm, df)
vfit
zp <- cox.zph(vfit, transform= function(time) log(time))
zp
plot(zp[1])
R output with plot(zp1) is
The cox.zph has the information:
zp$time
zp$x # is the log of time
zp$y[,1]
> zp$time
[1] 100 150 180 200 300
> zp$x
[1] 4.605170 5.010635 5.192957 5.298317 5.703782
> zp$y[,1]
100 150 180 200 300
-0.02336403 0.09461281 -0.03200438 -0.09778832 0.03476997
When I try to plot the graphics I have two possibilities
plot(zp$x, zp$y[,1], ylim = c(-0.2,0.2))
plot(zp$time, zp$y[,1], ylim = c(-0.2,0.2))
that offers the following graphics A) Scale in "x" as the initial graphic but with different numbers
B) Scale in"x" with original (no transformed time)
The question is: How can I obtain the graphic A with the "x" numbers corresponding in B, to get the initial graph (R output with plot(zp1) - beta(t) for age)
The reason for that, is because I'd like to reproduce the initial graphic in ggplot
library (ggplot2)
scaled<- zp$y[,1]
x<- zp$x
time<- zp$time
df<- data.frame(scaled, x, time)
ggplot()+
geom_point(aes(zp$time, scaled), df)+
coord_cartesian(ylim = c(-0.2, 0.2))
ggplot()+
geom_point(aes(zp$x, scaled), df)+
coord_cartesian(ylim = c(-0.2, 0.2))
Upvotes: 1
Views: 128
Reputation: 17069
Use scale_x_log10()
:
library(ggplot2)
library(survival)
ggplot(df)+
geom_point(aes(time, scaled))+
coord_cartesian(ylim = c(-0.2, 0.2)) +
scale_x_log10(breaks = seq(90, 300, by = 30))
(nb, you could use scale_x_continuous(trans = "log")
to use the natural logarithm instead of log10, but this isn't necessary -- the results are identical with any base.)
Upvotes: 1