<p>You can use <a href="https://bioconductor.org/packages/release/bioc/html/karyoploteR.html" rel="noreferrer">karyoploteR</a> to plot your regions on an ideogram quite easily.</p>

<p><strong>Disclosure:</strong> I'm one of the authors of the tool.</p>

<p>For this example I'll start creating 4 GRanges with random regions, but with your data you should load your bed into R and split it into four GRanges.</p>

<pre><code>library(karyoploteR)


typeA &lt;- createRandomRegions(nregions = 100, length.mean = 5e6, length.sd = 3e6)
typeB &lt;- createRandomRegions(nregions = 20, length.mean = 15e6, length.sd = 10e6)
typeC &lt;- createRandomRegions(nregions = 1000, length.mean = 5e5, length.sd = 2e5)
typeD &lt;- createRandomRegions(nregions = 100, length.mean = 2e6, length.sd = 5e6)

colors &lt;- c(A="#FFD700", B="#00BFFF", C="#FF7F50", D="#90EE90")
</code></pre>

<p>And we can plot them. We have to start creating an empty ideogram with <code>plotKaryotype</code> (we are working with human here, but you can use <a href="https://bernatgel.github.io/karyoploter_tutorial//Tutorial/CustomGenomes/CustomGenomes.html" rel="noreferrer">any genome you want</a>) and then plot the regions in the GRanges with <a href="https://bernatgel.github.io/karyoploter_tutorial//Tutorial/PlotRegions/PlotRegions.html" rel="noreferrer"><code>kpPlotRegions</code></a>. As a bonus, if two regions overlap it will plot them one above the other.</p>

<pre><code>kp &lt;- plotKaryotype(genome = "hg19")

kpPlotRegions(kp, data=typeA, r0=0, r1=0.2, col=colors["A"])
kpPlotRegions(kp, data=typeB, r0=0.25, r1=0.45, col=colors["B"])
kpPlotRegions(kp, data=typeC, r0=0.5, r1=0.7, col=colors["C"])
kpPlotRegions(kp, data=typeD, r0=0.75, r1=0.95, col=colors["D"])
legend(x = "right", legend = names(colors), fill = colors, border=NA)
</code></pre>

<p>And you'll get something similar to this</p>

<p><a href="https://i.sstatic.net/u9HKe.png" rel="noreferrer"><img src="https://i.sstatic.net/u9HKe.png" alt="enter image description here"></a></p>

<p>In this example we have fairly large regions and not a lot of them. As @heathobrien pointed out, plotting the actual regions in the whole genome plot might not be the best visualization option. You can either use <a href="https://bernatgel.github.io/karyoploter_tutorial//Tutorial/Zoom/Zoom.html" rel="noreferrer"><code>zoom</code></a> to create different plots of smaller parts of the genome or change your visualization to show the density of regions along the genome with <a href="https://bernatgel.github.io/karyoploter_tutorial//Tutorial/PlotDensity/PlotDensity.html" rel="noreferrer"><code>kpPlotDensity</code></a>. </p>

<p>Plotting the density and tweaking a bit the visualization (and changing the example to have 10 times more regions) with this codei</p>

<pre><code>typeA &lt;- createRandomRegions(nregions = 1000, length.mean = 5e6, length.sd = 3e6)
typeB &lt;- createRandomRegions(nregions = 200, length.mean = 15e6, length.sd = 10e6)
typeC &lt;- createRandomRegions(nregions = 10000, length.mean = 5e5, length.sd = 2e5)
typeD &lt;- createRandomRegions(nregions = 1000, length.mean = 2e6, length.sd = 5e6)

kp &lt;- plotKaryotype(genome = "hg19", plot.type = 4, 
                    ideogram.plotter = NULL, labels.plotter = NULL, main="Density")
kpAddCytobandsAsLine(kp)
kpAddChromosomeNames(kp, srt=45)

kpPlotDensity(kp, data=typeA, r0=0, r1=0.2, col=colors["A"], window.size     = 10e6)
kpAddLabels(kp, labels = "Type A", r0=0, r1=0.2)
kpPlotDensity(kp, data=typeB, r0=0.25, r1=0.45, col=colors["B"], window.size = 10e6)
kpAddLabels(kp, labels = "Type B", r0=0.25, r1=0.45)
kpPlotDensity(kp, data=typeC, r0=0.5, r1=0.7, col=colors["C"], window.size = 10e6)
kpAddLabels(kp, labels = "Type C", r0=0.5, r1=0.7)
kpPlotDensity(kp, data=typeD, r0=0.75, r1=0.95, col=colors["D"], window.size = 10e6)
kpAddLabels(kp, labels = "Type D", r0=0.75, r1=0.95)
</code></pre>

<p>it would look something like this</p>

<p><a href="https://i.sstatic.net/e44ez.png" rel="noreferrer"><img src="https://i.sstatic.net/e44ez.png" alt="Whole Genome Plot of density of regions using karyoploteR"></a></p>

<p>The plots could be tweaked a lot changing various parameters and you could add other data on top of it if needed. You can find more information on the Bioconductor landing page or at the <a href="https://bernatgel.github.io/karyoploter_tutorial/" rel="noreferrer">karyoploteR's Tutorial and Examples page</a>.</p>

CC BY-SA 3.0
2018-03-01T11:46:19.803Z
@bernatgel thank you so much for the elaborate answer! I used karyoploteR as the comments suggested, but your code gave me a better answer, which I can now tweak about. Thanks!
rishi
CC BY-SA 3.0
2018-03-01T15:56:12.450Z
I [moved the discussion to chat](https://chat.stackexchange.com/rooms/73895/discussion-on-answer-by-bernatgel-how-to-make-chromosome-color-maps-for-bed-rang) so as not keep this excellent answer clean and not to bother bernatgel with the discussion.
terdon
CC BY-SA 3.0
2018-03-06T13:58:18.813Z
The downloaded source packages are in
 ‘/tmp/RtmpKhPald/downloaded_packages’
Warning message:
package ‘karyoploteR’ is not available (for R version 3.4.3) 

Can you give me a solution for this? Do I upgrade my R?
rishi
CC BY-SA 3.0
2018-03-06T15:35:44.707Z
@rishi karyoploteR is available from R3.4 onward, so it should be available for 3.4.3. Are you installing it with biocLite? 
    source("https://bioconductor.org/biocLite.R")
    biocLite("karyoploteR")
bernatgel
CC BY-SA 3.0
2018-03-06T18:00:17.453Z
@bernatgel yes, that's exactly what I did. I even updated my biocLite to be sure
rishi
CC BY-SA 3.0
2018-03-07T09:18:04.827Z
@rishi Can you send me the exact code you are using to install karyoploteR and the output of sessionInfo()? maybe by email would be best: bgel at igtp.cat   and I'll try to understand what's going on
bernatgel
CC BY-SA 4.0
2020-06-20T07:55:44.893Z
@bernatgel This does not work for me
Gabi
CC BY-SA 3.0
2018-02-28T16:21:13.677Z
2018-03-01T07:53:18.620Z
2018-03-01T07:53:18.620Z
bernatgel
<p>I have genomic .bed file data of 4 different types; type A,B,C,D. These are some examples-</p>

<p>Type A: </p>

<pre><code>1   101380000   101710000   A
1   110085000   110320000   A
</code></pre>

<p>Type B:</p>

<pre><code>1   100930000   101335000   B
1   10430000    10560000    B
</code></pre>

<p>Type C: </p>

<pre><code>1   101775000   101825000   C
1   103570000   104070000   C
</code></pre>

<p>Type D:</p>

<pre><code>1   153325000   153475000   D
1   156355000   156415000   D
</code></pre>

<p>This data is for chr1, but I such data for all the chromosomes. I want to draw a chromosome map (possibly using the Gviz package?) which can assign different colors to the 4 types of genomic ranges and make me an idiogram like plot for the chromosomes I wish to see, so I know where my ranges physically are, on the chromosome. </p>

<p>Can this be done using the <strong>Gviz</strong> package in R? If not, what is the best way and code to do this?  </p>

CC BY-SA 3.0
2018-02-21T11:20:35.203Z
On my (retina) screen some of these ranges would be less than a pixel in width. Are you sure this is the best approach?
heathobrien
CC BY-SA 3.0
2018-02-21T12:22:29.923Z
@heathobrien Aah then I guess perhaps not :/ Can you show me how to do it anyway, then I can think about it. My bed files are ~1000-4000 lines in number so maybe I can see something.
rishi
CC BY-SA 3.0
2018-02-21T12:28:21.687Z
@heathobrien on smaller chromosomes perhaps
rishi
CC BY-SA 3.0
2018-02-21T12:48:13.967Z
I haven't done that specifically, but it should be easy with ggbio. If you want to see the distribution of your ranges across a chromosome, I suggest plotting the centres of the ranges as points. There are a bunch of examples [here](http://www.tengfei.name/ggbio/docs/man/layout_karyogram-method.html)
heathobrien
CC BY-SA 3.0
2018-02-21T13:40:48.087Z
You could also use karyoploteR too instead of Gviz, see the vignette in Bioconductor to see the resulting plots
llrs
CC BY-SA 3.0
2018-02-21T18:53:23.693Z
Take a look at Circos highlights, zoomed into a subregion of chr1, e.g. http://circos.ca/documentation/tutorials/highlights/wedges/images
Alex Reynolds
<r><gviz>
How to make chromosome color maps for bed ranges
# How to make chromosome color maps for bed ranges

(Asked by: username_0 on 2018-02-21)

I have genomic .bed file data of 4 different types; type A,B,C,D. These are some examples-
Type A: 
1   101380000   101710000   A
1   110085000   110320000   A

Type B:
1   100930000   101335000   B
1   10430000    10560000    B

Type C: 
1   101775000   101825000   C
1   103570000   104070000   C

Type D:
1   153325000   153475000   D
1   156355000   156415000   D

This data is for chr1, but I such data for all the chromosomes. I want to draw a chromosome map (possibly using the Gviz package?) which can assign different colors to the 4 types of genomic ranges and make me an idiogram like plot for the chromosomes I wish to see, so I know where my ranges physically are, on the chromosome. 
Can this be done using the Gviz package in R? If not, what is the best way and code to do this?

**Tags:** r, gviz
> **Comments:**
> **username_1**: On my (retina) screen some of these ranges would be less than a pixel in width. Are you sure this is the best approach?  
> **username_0**: @heathobrien Aah then I guess perhaps not :/ Can you show me how to do it anyway, then I can think about it. My bed files are ~1000-4000 lines in number so maybe I can see something.  
> **username_0**: @heathobrien on smaller chromosomes perhaps  
> **username_1**: I haven't done that specifically, but it should be easy with ggbio. If you want to see the distribution of your ranges across a chromosome, I suggest plotting the centres of the ranges as points. There are a bunch of examples [here](http://www.tengfei.name/ggbio/docs/man/layout_karyogram-method.html)  
> **username_2**: You could also use karyoploteR too instead of Gviz, see the vignette in Bioconductor to see the resulting plots  
> **username_3**: Take a look at Circos highlights, zoomed into a subregion of chr1, e.g. http://circos.ca/documentation/tutorials/highlights/wedges/images  

---

## 1 answers

### Answer by username_4 on 2018-02-28. Score: 12 | Accepted answer

You can use karyoploteR to plot your regions on an ideogram quite easily.
Disclosure: I'm one of the authors of the tool.
For this example I'll start creating 4 GRanges with random regions, but with your data you should load your bed into R and split it into four GRanges.
library(karyoploteR)


typeA <- createRandomRegions(nregions = 100, length.mean = 5e6, length.sd = 3e6)
typeB <- createRandomRegions(nregions = 20, length.mean = 15e6, length.sd = 10e6)
typeC <- createRandomRegions(nregions = 1000, length.mean = 5e5, length.sd = 2e5)
typeD <- createRandomRegions(nregions = 100, length.mean = 2e6, length.sd = 5e6)

colors <- c(A="#FFD700", B="#00BFFF", C="#FF7F50", D="#90EE90")

And we can plot them. We have to start creating an empty ideogram with plotKaryotype (we are working with human here, but you can use any genome you want) and then plot the regions in the GRanges with kpPlotRegions. As a bonus, if two regions overlap it will plot them one above the other.
kp <- plotKaryotype(genome = "hg19")

kpPlotRegions(kp, data=typeA, r0=0, r1=0.2, col=colors["A"])
kpPlotRegions(kp, data=typeB, r0=0.25, r1=0.45, col=colors["B"])
kpPlotRegions(kp, data=typeC, r0=0.5, r1=0.7, col=colors["C"])
kpPlotRegions(kp, data=typeD, r0=0.75, r1=0.95, col=colors["D"])
legend(x = "right", legend = names(colors), fill = colors, border=NA)

And you'll get something similar to this

In this example we have fairly large regions and not a lot of them. As @heathobrien pointed out, plotting the actual regions in the whole genome plot might not be the best visualization option. You can either use zoom to create different plots of smaller parts of the genome or change your visualization to show the density of regions along the genome with kpPlotDensity. 
Plotting the density and tweaking a bit the visualization (and changing the example to have 10 times more regions) with this codei
typeA <- createRandomRegions(nregions = 1000, length.mean = 5e6, length.sd = 3e6)
typeB <- createRandomRegions(nregions = 200, length.mean = 15e6, length.sd = 10e6)
typeC <- createRandomRegions(nregions = 10000, length.mean = 5e5, length.sd = 2e5)
typeD <- createRandomRegions(nregions = 1000, length.mean = 2e6, length.sd = 5e6)

kp <- plotKaryotype(genome = "hg19", plot.type = 4, 
                    ideogram.plotter = NULL, labels.plotter = NULL, main="Density")
kpAddCytobandsAsLine(kp)
kpAddChromosomeNames(kp, srt=45)

kpPlotDensity(kp, data=typeA, r0=0, r1=0.2, col=colors["A"], window.size     = 10e6)
kpAddLabels(kp, labels = "Type A", r0=0, r1=0.2)
kpPlotDensity(kp, data=typeB, r0=0.25, r1=0.45, col=colors["B"], window.size = 10e6)
kpAddLabels(kp, labels = "Type B", r0=0.25, r1=0.45)
kpPlotDensity(kp, data=typeC, r0=0.5, r1=0.7, col=colors["C"], window.size = 10e6)
kpAddLabels(kp, labels = "Type C", r0=0.5, r1=0.7)
kpPlotDensity(kp, data=typeD, r0=0.75, r1=0.95, col=colors["D"], window.size = 10e6)
kpAddLabels(kp, labels = "Type D", r0=0.75, r1=0.95)

it would look something like this

The plots could be tweaked a lot changing various parameters and you could add other data on top of it if needed. You can find more information on the Bioconductor landing page or at the karyoploteR's Tutorial and Examples page.

> **Comments:**
> **username_0**: @bernatgel thank you so much for the elaborate answer! I used karyoploteR as the comments suggested, but your code gave me a better answer, which I can now tweak about. Thanks!  
> **username_5**: I [moved the discussion to chat](https://chat.stackexchange.com/rooms/73895/discussion-on-answer-by-bernatgel-how-to-make-chromosome-color-maps-for-bed-rang) so as not keep this excellent answer clean and not to bother bernatgel with the discussion.  
> **username_0**: The downloaded source packages are in  ‘/tmp/RtmpKhPald/downloaded_packages’ Warning message: package ‘karyoploteR’ is not available (for R version 3.4.3)   Can you give me a solution for this? Do I upgrade my R?  
> **username_4**: @rishi karyoploteR is available from R3.4 onward, so it should be available for 3.4.3. Are you installing it with biocLite?      source("https://bioconductor.org/biocLite.R")     biocLite("karyoploteR")  
> **username_0**: @bernatgel yes, that's exactly what I did. I even updated my biocLite to be sure  
> **username_4**: @rishi Can you send me the exact code you are using to install karyoploteR and the output of sessionInfo()? maybe by email would be best: bgel at igtp.cat   and I'll try to understand what's going on  
> **username_6**: @bernatgel This does not work for me  

---

