1

I'm new in R. I'd like to draw figures like this (lower left part): http://www.nature.com/ng/journal/v45/n8/images_article/ng.2699-F3.jpg,

to show the co-existing or exclusive of mutations (one gene per line and one sample per column). I don't think it's plotted by heatmap or pheatmap. And here is a sample data (1 for mutation, 0 for wildtype and NA for not available):

data=matrix(sample(c(rep(0,30),rep(1,30)),60),ncol=15)
is.na(data)=c(2,20)

Any suggestions on how to finish that? Thanks~

jbaums
  • 27,115
  • 5
  • 79
  • 119
CB Zhang
  • 21
  • 4

1 Answers1

3

This looks like raster/tiling graphics to me. There are several options. Personally I think the ggplot2 package does a good job.

I didn't use your provided example, however, because I think the data would best be organized in a long format. See code below for a simple example on how to do this:

require(ggplot2)

dat <- expand.grid(gene=1:10, subj=1:50)
dat$mut <- as.factor(sample(c(rep(0,300),rep(1,200)),500))
dat$mut[sample(500,300)] <- NA

ggplot(dat, aes(x=subj, y=gene, fill=mut)) + 
  geom_raster() +
  scale_fill_manual(values = c("#8D1E0B","#323D8D"), na.value="#FFFFFF")

#dev.off()

The visual appearance can be optimized using the manual scales and the theming functions provided by ggplot2. You can look that up in the documentation of the package: http://docs.ggplot2.org/current/

EDIT:

To elaborate a bit more on the options that are supplied by ggplot2 to customoize appearance. A cleaner variant of the plot, with customized appearance and scales, may look like this:

ggplot(dat, aes(x=subj, y=gene, fill=mut)) +
  geom_raster() +
  scale_fill_manual(values = c("#8D1E0B","#323D8D"), na.value="#FFFFFF") +
  scale_x_discrete("Subject") +
  scale_y_continuous(breaks=1:10,
    labels=c("D0","D1","D2","D3","D4","D5","D6","D7","D8","D9")) +
  guides(fill=FALSE) +
  theme(
    axis.ticks.x=element_blank(), axis.ticks.y=element_blank(),
    axis.text.x=element_blank(), axis.text.y=element_text(colour="#000000"), 
    axis.title.x=element_text(face="bold"), axis.title.y=element_blank(),
    panel.grid.major.x=element_blank(), panel.grid.major.y=element_blank(),
    panel.grid.minor.x=element_blank(), panel.grid.minor.y=element_blank(), 
    panel.background=element_rect(fill="#ffffff")
  )

#dev.off()

Nonetheless, a look into the documentation of ggplot2 is very useful in developing your graphics and adjusting them to your needs.

SimonG
  • 4,701
  • 3
  • 20
  • 31
  • That looks fine. But I need to learn more to optimize it. The published ones are much more better~~ [link](http://www.nature.com/nature/journal/v489/n7417/images/nature11404-f1.2.jpg) @SimonG – CB Zhang Aug 14 '14 at 10:34
  • I added some details to my answer to start you off on customizing graphics in `ggplot2`. There are far more options, of course, but these are the ones that I use most frequently. – SimonG Aug 14 '14 at 11:49
  • Yes, there is a long way to go. Thanks for enlightening~@SimonG – CB Zhang Aug 15 '14 at 06:17