<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Circular chart customization for the circlize R package – the R Graph Gallery</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="generator" content="pandoc" /> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content="How to apply basic customizations to a circular chart made with the R circlize package. Learn how to change track width, background color, start angle and more."> <meta name="keywords" content="R,ggplot2,tidyverse,Example,Data,Dataviz,Datavisualization,Plot,Chart,Graph,Learning,Caveat,Pitfall,Mistake"> <meta name="author" content="Yan Holtz"> <link rel="icon" href="img/logo/R_single_small.png"> <!-- Control appearance when shared by social media --> <meta property="og:title" content="Circular chart customization for the circlize R package" /> <meta property="og:image" content="https://github.com/holtzy/R-graph-gallery/raw/master/img/logo/R_single_big.png" /> <meta property="og:description" content="How to apply basic customizations to a circular chart made with the R circlize package. Learn how to change track width, background color, start angle and more." /> <meta property='og:url' content="https://www.r-graph-gallery.com/225-circular-plot-custom-a-track.html" /> <meta property="og:type" content="website" /> <!-- Bootstrap core CSS --> <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet"> <!-- Custom fonts for this template --> <link href="vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="css/agency.css" rel="stylesheet"> <!-- JQUERY --> <script src="vendor/jquery/jquery.min.js"></script> </head> <body data-spy="scroll" data-target="#myScrollspy" data-offset="1"> <!-- THIS ALLOWS TO INSERT THE MENU --> <nav class="navbar navbar-expand-lg fixed-top" id="mainNav"> <div class="container"> <a href="index.html">← R Graph Gallery</a> <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> Menu <i class="fa fa-bars"></i> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav text-uppercase ml-auto"> <li class="nav-item"> <a class="nav-link" data-toggle="modal" href="#researchModal"><i class="fa fa-search"></i></a> </li> <li class="nav-item"> <a class="nav-link" data-toggle="modal" href="#LogoMenu">Chart types</a> </li> <li class="nav-item dropdown"> <a class="nav-link" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Quick </a> <div class="dropdown-menu" aria-labelledby="navbarDropdown"> <p class="menuTitle"><i>Basics</i></p> <div class="dropdown-divider"></div> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/intro_d3js.html">Intro to d3</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/shape.html">Shape</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/basic_datamanipulation.html">Data wrangling</a> <p class="menuTitle"><i>Customize</i></p> <div class="dropdown-divider"></div> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/custom_axis.html">Axis</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/custom_color.html">Color</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/custom_theme.html">Themes</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/custom_legend.html">Legend</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/custom_responsive.html">Responsivness</a> <p class="menuTitle"><i>Interactivity</i></p> <div class="dropdown-divider"></div> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/interactivity_tooltip.html">Tooltip</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/interactivity_button.html">Button</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/interactivity_zoom.html">Zoom</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/graph/interactivity_brush.html">Brushing</a> <a class="dropdown-item" href="https://www.d3-graph-gallery.com/interactivity.html">Transition</a> </div> </li> <li class="nav-item"> <a class="nav-link" href="../all.html">ALL</a> </li> <li class="nav-item"> <a class="nav-link" href="https://www.r-graph-gallery.com">R</a> </li> <li class="nav-item"> <a class="nav-link" href="https://www.python-graph-gallery.com">Python</a> </li> <li class="nav-item"> <a class="nav-link js-scroll-trigger" href="https://www.data-to-viz.com">Data to viz</a> </li> <li class="nav-item"> <a class="nav-link js-scroll-trigger" href="https://www.yan-holtz.com">Who am I</a> </li> <li class="nav-item"> <a class="nav-link js-scroll-trigger" href="about.html">About</a> </li> </ul> </div> </div> </nav> <!-- Header = Title in big + social media Icon + quick description --> <header class="masthead"> <div class="textlanding"> <center><h1>Circular chart customization for the circlize R package</h1></center> <hr class="short_hr"> <br> <ul class="list-inline social-buttons"> <li class="list-inline-item"> <a href="https://twitter.com/R_Graph_Gallery"> <i class="fa fa-twitter"></i> </a> </li> <li class="list-inline-item social-buttons"> <a href="https://github.com/holtzy"> <i class="fa fa-github" style="color: white"></i> </a> </li> <li class="list-inline-item social-buttons"> <a href="https://www.linkedin.com/in/yan-holtz-2477534a/"> <i class="fa fa-linkedin"></i> </a> </li> <li class="list-inline-item social-buttons"> <a href="https://www.yan-holtz.com"> <i class="fa fa-home"></i> </a> </li> </ul> <center><p style="max-width: 600px; margin-top: 40px">This post follows the previous <a href="224-basic-circular-plot.html">introduction</a> to the <code>circlize</code> package. It shows how to apply basic customizations to a circular chart: how to change track width, background color, start angle and more.</p></center> <div style="text-align:center"> <a class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger" href='chord-diagram.html'>Chord section</a> <a class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger" href='https://www.data-to-viz.com/graph/chord.html'>Data to Viz</a> </div> </div> </header> <!-- STYLE for chart pages but not the rest of tthe website --> <style> img { margin-top: 20px; } </style> <div class="container"> <h1 id="most-basic-circular-chart">Most basic circular chart</h1> <hr /> <div class="row"> <div class="col-md-6 col-sm-12 align-self-center"> <p>Let’s remind how to build a basic circular chart with the <code>circlize</code> package.</p> <p>This is extensively described in <a href="224-basic-circular-plot.html">chart #224</a>.</p> </div> <div class="col-md-6 col-sm-12"> <p><img src="225-circular-plot-custom-a-track_files/figure-html/thecode-1.png" width="100%" /></p> </div> </div> <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="co"># Upload library</span></a> <a class="sourceLine" id="cb1-2" data-line-number="2"><span class="kw">library</span>(circlize)</a> <a class="sourceLine" id="cb1-3" data-line-number="3"></a> <a class="sourceLine" id="cb1-4" data-line-number="4"><span class="co"># Create data</span></a> <a class="sourceLine" id="cb1-5" data-line-number="5">data =<span class="st"> </span><span class="kw">data.frame</span>(</a> <a class="sourceLine" id="cb1-6" data-line-number="6"> <span class="dt">factor =</span> <span class="kw">sample</span>(letters[<span class="dv">1</span><span class="op">:</span><span class="dv">8</span>], <span class="dv">1000</span>, <span class="dt">replace =</span> <span class="ot">TRUE</span>),</a> <a class="sourceLine" id="cb1-7" data-line-number="7"> <span class="dt">x =</span> <span class="kw">rnorm</span>(<span class="dv">1000</span>),</a> <a class="sourceLine" id="cb1-8" data-line-number="8"> <span class="dt">y =</span> <span class="kw">runif</span>(<span class="dv">1000</span>)</a> <a class="sourceLine" id="cb1-9" data-line-number="9"> )</a> <a class="sourceLine" id="cb1-10" data-line-number="10"></a> <a class="sourceLine" id="cb1-11" data-line-number="11"><span class="co"># Step1: Initialise the chart giving factor and x-axis.</span></a> <a class="sourceLine" id="cb1-12" data-line-number="12"><span class="kw">circos.initialize</span>( <span class="dt">factors=</span>data<span class="op">$</span>factor, <span class="dt">x=</span>data<span class="op">$</span>x )</a> <a class="sourceLine" id="cb1-13" data-line-number="13"></a> <a class="sourceLine" id="cb1-14" data-line-number="14"><span class="co"># Step 2: Build the regions.</span></a> <a class="sourceLine" id="cb1-15" data-line-number="15"><span class="kw">circos.trackPlotRegion</span>(<span class="dt">factors =</span> data<span class="op">$</span>factor, <span class="dt">y =</span> data<span class="op">$</span>y, <span class="dt">panel.fun =</span> <span class="cf">function</span>(x, y) {</a> <a class="sourceLine" id="cb1-16" data-line-number="16"> <span class="kw">circos.axis</span>()</a> <a class="sourceLine" id="cb1-17" data-line-number="17"> })</a> <a class="sourceLine" id="cb1-18" data-line-number="18"></a> <a class="sourceLine" id="cb1-19" data-line-number="19"><span class="co"># Step 3: Add points</span></a> <a class="sourceLine" id="cb1-20" data-line-number="20"><span class="kw">circos.trackPoints</span>(data<span class="op">$</span>factor, data<span class="op">$</span>x, data<span class="op">$</span>y)</a></code></pre></div> <h1 id="customization">Customization</h1> <hr /> <div class="row"> <div class="col-md-6 col-sm-12 align-self-center"> <p>Customization can happen at 3 different levels:</p> <ul> <li>initialization: use the usual <code>par()</code> function and the specific <code>circos.par()</code> functions for general stuff.</li> <li>in <code>circos.axis()</code> to customize axis</li> <li>in circos.trackPoints()` to customize the chart shapes.</li> </ul> <p>Note that most of the parameters are consistent with base R. Have a look to the <a href="scatterplot.html">scatterplot</a> section of the gallery for more customization.</p> </div> <div class="col-md-6 col-sm-12"> <p><img src="225-circular-plot-custom-a-track_files/figure-html/thecode2-1.png" width="100%" /></p> </div> </div> <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="co"># Upload library</span></a> <a class="sourceLine" id="cb2-2" data-line-number="2"><span class="kw">library</span>(circlize)</a> <a class="sourceLine" id="cb2-3" data-line-number="3"></a> <a class="sourceLine" id="cb2-4" data-line-number="4"><span class="co"># Create data</span></a> <a class="sourceLine" id="cb2-5" data-line-number="5">data =<span class="st"> </span><span class="kw">data.frame</span>(</a> <a class="sourceLine" id="cb2-6" data-line-number="6"> <span class="dt">factor =</span> <span class="kw">sample</span>(letters[<span class="dv">1</span><span class="op">:</span><span class="dv">8</span>], <span class="dv">1000</span>, <span class="dt">replace =</span> <span class="ot">TRUE</span>),</a> <a class="sourceLine" id="cb2-7" data-line-number="7"> <span class="dt">x =</span> <span class="kw">rnorm</span>(<span class="dv">1000</span>),</a> <a class="sourceLine" id="cb2-8" data-line-number="8"> <span class="dt">y =</span> <span class="kw">runif</span>(<span class="dv">1000</span>)</a> <a class="sourceLine" id="cb2-9" data-line-number="9"> )</a> <a class="sourceLine" id="cb2-10" data-line-number="10"></a> <a class="sourceLine" id="cb2-11" data-line-number="11"><span class="co"># General Customization:</span></a> <a class="sourceLine" id="cb2-12" data-line-number="12"><span class="kw">par</span>(</a> <a class="sourceLine" id="cb2-13" data-line-number="13"> <span class="dt">mar =</span> <span class="kw">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>), <span class="co"># Margin around chart</span></a> <a class="sourceLine" id="cb2-14" data-line-number="14"> <span class="dt">bg =</span> <span class="kw">rgb</span>(<span class="fl">0.4</span>,<span class="fl">0.1</span>,<span class="fl">0.7</span>,<span class="fl">0.05</span>) <span class="co"># background color</span></a> <a class="sourceLine" id="cb2-15" data-line-number="15">) </a> <a class="sourceLine" id="cb2-16" data-line-number="16"><span class="kw">circos.par</span>(<span class="st">"track.height"</span> =<span class="st"> </span><span class="fl">0.6</span>) <span class="co"># track hight, 0.6 = 60% of total height</span></a> <a class="sourceLine" id="cb2-17" data-line-number="17"></a> <a class="sourceLine" id="cb2-18" data-line-number="18"><span class="co"># Step1: Initialise the chart giving factor and x-axis.</span></a> <a class="sourceLine" id="cb2-19" data-line-number="19"><span class="kw">circos.initialize</span>( <span class="dt">factors=</span>data<span class="op">$</span>factor, <span class="dt">x=</span>data<span class="op">$</span>x )</a> <a class="sourceLine" id="cb2-20" data-line-number="20"></a> <a class="sourceLine" id="cb2-21" data-line-number="21"><span class="co"># Step2: Build regions. </span></a> <a class="sourceLine" id="cb2-22" data-line-number="22"><span class="kw">circos.trackPlotRegion</span>(<span class="dt">factors =</span> data<span class="op">$</span>factor, <span class="dt">y =</span> data<span class="op">$</span>y, <span class="dt">panel.fun =</span> <span class="cf">function</span>(x, y) {</a> <a class="sourceLine" id="cb2-23" data-line-number="23"> <span class="kw">circos.axis</span>(</a> <a class="sourceLine" id="cb2-24" data-line-number="24"> <span class="dt">h=</span><span class="st">"top"</span>, <span class="co"># x axis on the inner or outer part of the track?</span></a> <a class="sourceLine" id="cb2-25" data-line-number="25"> <span class="dt">labels=</span><span class="ot">TRUE</span>, <span class="co"># show the labels of the axis?</span></a> <a class="sourceLine" id="cb2-26" data-line-number="26"> <span class="dt">major.tick=</span><span class="ot">TRUE</span>, <span class="co"># show ticks?</span></a> <a class="sourceLine" id="cb2-27" data-line-number="27"> <span class="dt">labels.cex=</span><span class="fl">0.5</span>, <span class="co"># labels size (higher=bigger)</span></a> <a class="sourceLine" id="cb2-28" data-line-number="28"> <span class="dt">labels.font=</span><span class="dv">1</span>, <span class="co"># labels font (1, 2, 3 , 4)</span></a> <a class="sourceLine" id="cb2-29" data-line-number="29"> <span class="dt">direction=</span><span class="st">"outside"</span>, <span class="co"># ticks point to the outside or inside of the circle ?</span></a> <a class="sourceLine" id="cb2-30" data-line-number="30"> <span class="dt">minor.ticks=</span><span class="dv">4</span>, <span class="co"># Number of minor (=small) ticks</span></a> <a class="sourceLine" id="cb2-31" data-line-number="31"> <span class="dt">major.tick.percentage=</span><span class="fl">0.1</span>, <span class="co"># The size of the ticks in percentage of the track height</span></a> <a class="sourceLine" id="cb2-32" data-line-number="32"> <span class="dt">lwd=</span><span class="dv">2</span> <span class="co"># thickness of ticks and x axis.</span></a> <a class="sourceLine" id="cb2-33" data-line-number="33"> )</a> <a class="sourceLine" id="cb2-34" data-line-number="34"> })</a> <a class="sourceLine" id="cb2-35" data-line-number="35"></a> <a class="sourceLine" id="cb2-36" data-line-number="36"><span class="co"># Step 3: Add points</span></a> <a class="sourceLine" id="cb2-37" data-line-number="37"><span class="kw">circos.trackPoints</span>(data<span class="op">$</span>factor, data<span class="op">$</span>x, data<span class="op">$</span>y, <span class="dt">col =</span> <span class="st">"#69b3a2"</span>, <span class="dt">pch =</span> <span class="dv">16</span>, <span class="dt">cex =</span> <span class="fl">0.5</span>)</a></code></pre></div> <!-- Close container --> </div> <!-- ============================ RELATED SECTION ============================ --> <section class="bg-light" id="portfolio_landing" style="padding-top: 30px; padding-bottom: 30px; margin-top: 100px;"> <div class="container"> <p class="mySeryTitle">Related chart types</p> <hr> <div class="row"> <div class="col-md-2 col-sm-4 portfolio-item"> <a class="portfolio-link" href="chord-diagram.html"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <img class="img-fluid" src="img/section/Chord150.png" alt=""> </a> <div class="captionPortfolio">Chord diagram</div> </div> <div class="col-md-2 col-sm-4 portfolio-item"> <a class="portfolio-link" href="network.html"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <img class="img-fluid" src="img/section/Network150.png" alt=""> </a> <div class="captionPortfolio">Network</div> </div> <div class="col-md-2 col-sm-4 portfolio-item"> <a class="portfolio-link" href="sankey-diagram.html"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <img class="img-fluid" src="img/section/Sankey150.png" alt=""> </a> <div class="captionPortfolio">Sankey</div> </div> <div class="col-md-2 col-sm-4 portfolio-item"> <a class="portfolio-link" href="https://www.data-to-viz.com/graph/arc.html"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <img class="img-fluid" src="img/section/Arc150.png" alt=""> </a> <div class="captionPortfolio">Arc diagram</div> </div> <div class="col-md-2 col-sm-4 portfolio-item"> <a class="portfolio-link" href="hierarchical-edge-bundling.html"> <div class="portfolio-hover"> <div class="portfolio-hover-content"> <i class="fa fa-plus fa-3x"></i> </div> </div> <img class="img-fluid" src="img/section/Bundle150.png" alt=""> </a> <div class="captionPortfolio">Edge bundling</div> </div> </div> </div> </section> <!-- ============================ CONTACT SECTION ============================ --> <section id="contact" class="bg" style="background-color: white; padding-top: 60px"> <div class="container"> <div class="row"> <div class="col-lg-2 text-center"></div> <div class="col-lg-8 text-center"> <br><br><br> <h2 class="section-heading text-uppercase" style="color: black">Contact</h2> <p>This document is a work by <a href="https://www.yan-holtz.com">Yan Holtz</a>. Any feedback is highly encouraged. You can fill an issue on <a href="https://github.com/holtzy/D3-graph-gallery/issues">Github</a>, drop me a message on <a href="https://twitter.com/R_Graph_Gallery">Twitter</a>, or send an email pasting <a href="">yan.holtz.data</a> with <a href="">gmail.com</a>.</p> <div style="text-align:center"> <a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger" href="https://github.com/holtzy">Github</a> <a class="btn btn-primary btn-xl text-uppercase js-scroll-trigger" href="https://twitter.com/R_Graph_Gallery">Twitter</a> </div> </div> </div> </div> </section> <!-- ============================ FOOTER SECTION ============================ --> <footer class="bg-light" id="myFooter"> <div class="container" > <div class="row"> <div class="col-md-4"> <span class="copyright">Copyright © the R graph gallery 2018</span> </div> <div class="col-md-4"> <ul class="list-inline social-buttons"> <li class="list-inline-item"> <a href="https://twitter.com/R_Graph_Gallery"> <i class="fa fa-twitter"></i> </a> </li> <li class="list-inline-item"> <a href="https://github.com/holtzy"> <i class="fa fa-github"></i> </a> </li> <li class="list-inline-item"> <a href="https://www.linkedin.com/in/yan-holtz-2477534a/"> <i class="fa fa-linkedin"></i> </a> </li> </ul> </div> <div class="col-md-4"> <ul class="list-inline quicklinks"> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> </ul> </div> </div> </div> </footer> <script> // add bootstrap table styles to pandoc tables function bootstrapStylePandocTables() { $('tr.header').parent('thead').parent('table').addClass('table table-condensed'); } $(document).ready(function () { bootstrapStylePandocTables(); }); </script> <!-- ============================ JAVASCRIPT SECTION ============================ --> <!-- Bootstrap core JavaScript --> <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script> <!-- Custom scripts for this template --> <script src="js/agency.min.js"></script> </body> </html>