Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacke different plots in a facet manner

Tags:

r

ggplot2

To train with ggplot and to improve my skills in writing R functions I decided to build a series of functions that produces survival plots, with all kinds of extras. I managed to build a good working function for the basic survival plot, now I am getting to the extras. One thing I would like to do is an option that stacks an area plot of the number at risk at a given time point, on top of the survival plot. I would like it to look just like the facet_grid option of ggplot, but I did not manage to do it with this function. I do not want the two plots binded, like we can do with grid.arrange, but rather to have the same x-axis.

The following code produces the two (simplified) plots that I would like to stack on top of each other. I tried to do this with facet_grid, but I don't think the solution lies in this

library(survival)
library(ggplot2)
data(lung)
s <- survfit(Surv(time, status) ~ 1, data = lung)
dat <- data.frame(time = c(0, s$time),
                  surv = c(1, s$surv),
                  nr = c(s$n, s$n.risk))
pl1 <- ggplot(dat, aes(time, surv)) + geom_step()

enter image description here

pl2 <- ggplot(dat, aes(time, nr)) + geom_area()

enter image description here

like image 820
Edwin Avatar asked May 17 '13 17:05

Edwin


People also ask

What does it mean to facet a plot?

The facet approach partitions a plot into a matrix of panels. Each panel shows a different subset of the data. This R tutorial describes how to split a graph using ggplot2 package.

What is a facet wrap?

Facet wraps are a useful way to view individual categories in their own graph. For example, if you wanted to make a separate graph for each cut measuring the price (y axis) for each clarity (x axis), you could add facet_wrap(~cut) .

What is facet_ wrap in ggplot2?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner. You can control how the ribbon is wrapped into a grid with ncol , nrow , as.


1 Answers

First, melt your data to long format.

library(reshape2)
dat.long<-melt(dat,id.vars="time")
head(dat.long)
  time variable     value
1    0     surv 1.0000000
2    5     surv 0.9956140
3   11     surv 0.9824561
4   12     surv 0.9780702
5   13     surv 0.9692982
6   15     surv 0.9649123

Then use subset() to use only surv data in geom_step() and nr data in geom_area() and with facet_grid() you will get each plot in separate facet as variable is used to divide data for facetting and for subsetting. scales="free_y" will make pretty axis.

ggplot()+geom_step(data=subset(dat.long,variable=="surv"),aes(time,value))+
  geom_area(data=subset(dat.long,variable=="nr"),aes(time,value))+
  facet_grid(variable~.,scales="free_y")

enter image description here

like image 95
Didzis Elferts Avatar answered Oct 19 '22 18:10

Didzis Elferts