I can help with something like this, but I don't know how to prettify the Y axis labels - all chromosomes in my example are of length 20, so labels are getting overcrowded and I disabled them.
-- edit: fixed labels
require(ggplot2)
require(reshape)
data <- data.frame(matrix(rnorm(2*20*4)-0.5, nrow=2*20, byrow=F))
names(data) <- paste("T",1:4,sep="")
data$chromosome=factor(1:(2*20),1:(2*20),
labels=paste(rep(paste("chr",1:2,sep=""),each=20),1:20,sep="."))
data$labels=matrix(rbind(paste("chr",1:2,sep=""),
matrix(rep("",19*2),nrow=19)),byrow=T,nrow=20*2)
dm <- melt(data,id.var=c("chromosome","labels"))
dm$fill <- rep("white",2*20*4)
dm$fill[dm$value < -0.5] <- "red"
dm$fill[dm$value > 0.5] <- "blue"
p <- ggplot(dm, aes(variable, chromosome, fill=fill)) + geom_tile()
p + scale_fill_identity(expand=c(0,0),guide = "legend",
labels=c("deletion","amplification","none")) +
scale_y_discrete(breaks=dm$chromosome,labels=dm$labels) +
ggtitle("Example for 2 chromosomes")
-- edit2, with your data, just changed two values to negatives:
require(ggplot2)
require(reshape)
# four samples, here genome is linear, 2M long, each bin is 1000bp
# matrix rows = bins, matrix columns = samples
data <- matrix(rep(rep(0,2000000/1000),4),ncol=4)
# the intervals from your sample
intervals=data.frame(chromosome=c("chr1","chr1","chr1","chr2","chr2","chr2"),
start=c(23432,34564,78463,1375364,1463723,1678573),
end=c(25925,44572,85634,1378364,1469367,1683642),
t1=c(4,5,3,-1,4,2),
t2=c(3,5,4,2,-6,5))
# transfer the values from data frame into the matrix, probably can code a single function
mark_t1 <- function(a,b,c){ data[ (a/1000) : (b/1000), 1] <<- c}
apply(intervals[,c('start','end','t1')], 1 , function(x) mark_t1(x[1],x[2],x[3]))
mark_t2 <- function(a,b,c){ data[ (a/1000) : (b/1000), 2] <<- c}
apply(intervals[,c('start','end','t2')], 1 , function(x) mark_t2(x[1],x[2],x[3]))
# converting the matrix into a data frame
data <- as.data.frame(data)
names(data) <- paste("T",1:4,sep="")
# add bins column
data$bin=seq(1,2000000,by=1000)
dm <- melt(data,id.var=c("bin"))
# color the bins according to their values
dm$fill <- "white"
dm$fill[dm$value < -0.5] <- "red"
dm$fill[dm$value > 0.5] <- "blue"
# plot as red-white-blue rectangles, by using bins and continuous scale, it's easy to
# place Chromosome or even Gene markers
#
p <- ggplot(dm, aes(variable, bin, fill=fill)) + geom_tile()
p + scale_fill_identity(expand=c(0,0),guide = "legend",
labels=c("amplification","deletion","none")) +
scale_y_continuous(breaks=c(0,1400000,2000000),labels=c("Chr 1","Chr 2","Chr 3")) +
ggtitle("Example for 2 chromosomes") +
theme_bw() +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
panel.border = element_blank(),panel.background = element_blank(),
axis.title.x= element_blank(),axis.title.y = element_blank())
or just like a heatmap plot:
#plot as white-to-blue heatmap
p2 <- ggplot(dm, aes(variable, bin, fill=value)) + geom_tile()
p2 + scale_y_continuous(breaks=c(0,1400000,2000000),labels=c("Chr 1","Chr 2","Chr 3")) +
scale_fill_gradient(low = "white", high = "steelblue") +
theme_bw() + ggtitle("Example for 2 chromosomes") +
theme(panel.grid.major = element_blank(),panel.grid.minor = element_blank(),
panel.border = element_blank(),panel.background = element_blank(),
axis.title.x= element_blank(),axis.title.y = element_blank())
if you could make a picture visible and provide an example of your data, hacking R code for you will be much easier
Sorry my image hosting site link broke. Updated the question with new image link. Thanks.
Thank you. I answered below. In my example each of 23 chromosomes is divided into 20 segments which are then colored by a random value. I plot these using
scale_fill_identity
(discrete) so labels are getting crowded and I don't know how to make them nice. If you can use continuous scale, then things will be easier.