yi.tang.uni
yi.tang.uni

Reputation: 103

In R and ggplot2 package, How to Add Lines?

I am doing a survival analysis and have produced the survival graph using

  1. plot function to plot the Kaplan-Meier(KA variable) estimate as y value against time.

  2. lines function to plot the step lines between estimate i and i+1, for each i=1,2,....

The code is as follows:

plot(KA)

for( i in 1:(length(KA)-1)){

lines(c(i,i+1),c(KA[i],KA[i]))       # The horizontal step lines

lines(c(i+1,i+1),c(KA[i],KA[i+1]))   # The vertical step lines

}

Now I want to make a more beautiful survival graph using ggplot2 package.

The question is: how to add the step lines into the graph?

I'm sorry, I can not put graphs as my reputation is less than 10.

Upvotes: 0

Views: 229

Answers (1)

wligtenberg
wligtenberg

Reputation: 8025

Have a look at either geom_step, geom_path or geom_segment.
They might be helpful in what you are trying to achieve.

http://had.co.nz/ggplot2/geom_step.html
http://had.co.nz/ggplot2/geom_path.html
http://had.co.nz/ggplot2/geom_segment.html

Upvotes: 1

Related Questions