Also check the ggbio library, it has functions for plotting genomes and tracks.
Hi everybody, I have some CNV data that need to be turned into a chromosomal plot. The format is like this:
chr start end ploidy loss/gain
chr1 86000000 117150000 1 loss
chr2 70250000 70500000 3 gain
chr2 203050000 204650000 3 gain
(The last column can probably be omitted as we have all the info we need in the ploidy column).
I would like to visualize them on a chromosomal plot. The only tool I've managed to make work with my data is CNANorm (http://www.bioconductor.org/packages/devel/bioc/vignettes/CNAnorm/inst/doc/CNAnorm.pdf), but it insists on analyzing my already analyzed data, so I can't get the results I need. Here's my CNANorm output, although the data points are all wrong because of the extra analysis. 
Any help would be appreciated, please keep in mind that I don't have that much R experience, and that I only need a viewer for the time being. Thanks everyone :)
2 answers
You can plot your data like this. CNVs are plotted as segments with a point corresponding to their midpoint. Chromosomes are separated out into facets.
library("ggplot2")
chr <- c("chr1", "chr2", "chr2")
start <- c(86000000, 70250000, 203050000)
end <- c(117150000, 70500000, 204650000)
center <- start + (end - start)/2
ploidy <- c(1, 3, 3)
ploidy_df <- data.frame(chr, start, end, center, ploidy)
ggplot(ploidy_df, aes(x=center, y=ploidy)) +
geom_point() +
geom_segment(aes(x=start, y=ploidy, xend=end, yend=ploidy, colour="segment")) +
geom_hline(y=2, linetype=2) +
facet_wrap(~chr) +
xlab("Position") +
theme_bw()

Hi, thank you for your reply! So, if I understood correctly, I add a "center" column to my data, and delete the last column. Your code gives me an error though,
Error: Unknown parameters: y
Yes, add a "center" column. You don't need to delete the last column, unless you just want to keep things neat.
I'm not sure why you get that error, I just re-ran the code I pasted above and it works fine. If you are running the plotting code as I wrote it above with different data, then you have to make sure the column names of your data frame match what I have (chr, start, end, center, ploidy).
Yes, my column names have the same names as your code. Weird thing is, I am getting the error even when pasting just your own code. (R for Windows 3.2.3 32 & 64 bit, up to date packages). When I run the ggplot command using fewer parameters, the error appears just as soon as I use the geom_hline parameter, if that helps. Thanks again!
Hmmm.. so can you produce a plot if you remove geom_hline()? This parameter just draws the dotted line at ploidy = 2, so it's not really necessary.
I just double checked the documentation and actually you should try this instead geom_hline(yintercept=2, linetype=2). If that doesn't work, then I'm at a loss. Hope that helps!
It worked! Thank you so very much! :)
Edit: Just one last thing, because of the different chromosomal lengths, the smaller the chromosome, the more everything is stuck to the left corner. Is there a way to independently plot the x axis for each chromosome?
Great!! If you check out the options for facet_wrap you can adjust the scales and also the number of rows/columns. In this case you would specify facet_wrap(~ chr, scales = "free") for independent plotting on both x and y.
Thank you, everything looks great now! The only thing that remains is to set the x axis range to the length of each chromosome. I thought about creating an extra column with the chromosome length, and using it for the x axis, but scale_x_continuous doesn't seem to accept variables. Here's the code I've used so far in case someone wants it:
library("ggplot2")
mydata <- read.table("C:\\RData\\testBiostars.txt", header=T)
ggplot(mydata, aes(x=0, y=ploidy)) +
geom_segment(aes(x=start, y=ploidy, xend=end, yend=ploidy, colour="CNV length")) +
geom_hline(yintercept=2, linetype=2) +
facet_wrap(~ chr, scales = "free") +
xlab("Position") +
ylab("Ploidy") +
scale_x_continuous(limits = c(0, 200000000))+
theme_bw()
And here's the output:
Hi, I can help you with if you can explain us with, size of data, duration of usages, target audiences.
We are providing the bioinformatics platform for app and data hosting. We will help you parse your data with graph visualization.
Thanks, Sathik, support@bioelm.com
Please delete your spam
How do you know this spam? Check the facts before commenting.
Please provide an actual answer to the question, and not a commercial message.
For example you may explain how to visualize CNVs as per the OP's question, and eventually if you consider that one of the tools or services provided by your company may be better suited for the task, you can explain it in the post.
Consider that we can not let you answer with a commercial message to all the questions asked in Biostar, it would make a mess of the forum.
Although this is deleted, I sympathise with the spammer (and that's exactly what it is), because he/she wants customers to help. However, there are expectations of a support Q&A forum like this that answers are solved for free if possible.
Perhaps we should make it clearer that promoting software/services is OK so long as their services/software is well defined. However, I personally don't think it is acceptable to fish for customers here. The idea is to teach and learn, not to request and deliver.
Log in to answer this question.
Does data have neutral regions too ? With ploidy 2 ?
Hi! No, the data I was given is filtered, so they only contain the abnormal regions. We are thinking of making a matrix of all found CNV across all samples, in which case we'll have neutral regions with ploidy = 2, or perhaps change the counting to neutral = 0, loss = -1 or -2, and positive values for the rest. Doing this transformation shouldn't be a problem if needed!