Friday, July 19, 2013

R ggbio - tracks

  • container for a plot you want to align
  • constructor tracks()
  • first we produce two plots:
require(ggbio)
require(gridExtra)
df1 <- data.frame(time = 1:100, score = sin((1:100)/20)*10)
p1 <- ggplot(data = df1, aes(x = time, y = score)) +
      geom_line()
df2 <- data.frame(time = 30:120, score = sin((30:120)/20)*10, value = rnorm(120-30 + 1))
p2 <- ggplot(data = df2, aes(x = time, y = score)) +
    geom_line() +
    geom_point(size = 2, aes(color = value))
grid.arrange(p1,p2)

  • the two plots have different scale on the x-axis
  • we want to align them on exactly the same x-axis to make it more easy to compare each other
tracks(p1,p2)

  • now add a theme (included in the ggbio package)
tracks(time1=p1,time2=p2) +
    xlim(1,40) +
    theme_tracks_sunset()

  • add a title
tracks(time1=p1,time2=p2,title="My title")

  • set background colors (for each of the single plots)
tracks(time1=p1,time2=p2,track.plot.color=c("darkred","darkgreen"))

  • set background color (for the whole tracks)
tracks(time1=p1,time2=p2,track.bg.color="darkred")

  • set color of the label borders and the filling
tracks(time1=p1,time2=p2,label.bg.color="darkred",label.bg.fill="lightblue")

  • set color and size of the label text and the width of the labels
tracks(time1=p1,time2=p2,label.text.color="midnightblue",label.text.cex=2,label.width=unit(2.5,"cm"))

  • other zoom methods:
    • scale along with the limits of a GRange

require(GenomicRanges)
gr <- GRanges("chr", IRanges(1, 40))
tracks(time1 = p1, time2 = p2) + xlim(gr)

  • scale along with the limits of a IRange
require(GenomicRanges)
gr <- GRanges("chr", IRanges(1, 40))
tracks(time1 = p1, time2 = p2) + xlim(ranges(gr))

  • change limits afterwards
require(GenomicRanges)
gr <- GRanges("chr", IRanges(1, 40))
trks <- tracks(time1 = p1, time2 = p2) + xlim(ranges(gr))
xlim(trks)
[1]  1 40
xlim(trks) <- c(1,30)
trks

No comments :

Post a Comment