<!doctype html>
<html lang="en">
  <head>
    <!-- Google Analytics + AdThrive -->

    <!-- AdThrive Head Tag Manual -->
    <script data-no-optimize="1" data-cfasync="false">
      (function (w, d) {
        w.adthrive = w.adthrive || {};
        w.adthrive.cmd = w.adthrive.cmd || [];
        w.adthrive.plugin = "adthrive-ads-manual";
        w.adthrive.host = "ads.adthrive.com";
        var s = d.createElement("script");
        s.async = true;
        s.referrerpolicy = "no-referrer-when-downgrade";
        s.src =
          "https://" +
          w.adthrive.host +
          "/sites/6434363fc11d8059e61106b4/ads.min.js?referrer=" +
          w.encodeURIComponent(w.location.href) +
          "&cb=" +
          (Math.floor(Math.random() * 100) + 1);
        var n = d.getElementsByTagName("script")[0];
        n.parentNode.insertBefore(s, n);
      })(window, document);
    </script>
    <!-- End of AdThrive Head Tag -->

    <script
      async
      src="https://www.googletagmanager.com/gtag/js?id=UA-79254642-1"
    ></script>
    <script>
      window.dataLayer = window.dataLayer || [];
      function gtag() {
        dataLayer.push(arguments);
      }
      gtag("js", new Date());

      gtag("config", "UA-79254642-1");
    </script>

    <!-- Google Analytics + OutboundLink + Google Adsense -->

    <meta charset="utf-8" />
    <title>Density plot with labels on lines &#8211; 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="This post explains how to add a
text or labels on the line of a density chart. It uses the ggplot2 and
geomtextpath packages."
    />
    <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="Density plot with labels on
lines"
    />
    <meta
      property="og:image"
      content="https://www.r-graph-gallery.com/img/overview_RGG.png"
    />
    <meta
      property="og:description"
      content="This post explains how to
add a text or labels on the line of a density chart. It uses the ggplot2
and geomtextpath packages."
    />
    <meta
      property="og:url"
      content="https://www.r-graph-gallery.com/361-density-plot-with-labels-on-lines.html.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 THAT IS STORED IN A MENU.HTML FILE-->
    <nav class="navbar navbar-expand-lg fixed-top" id="mainNav"></nav>
    <script>
      $(function () {
        $("#mainNav").load("html_chunk/menu.html");
      });
    </script>

    <!-- THIS ALLOWS TO INSERT THE MODAL OF THE MENU THAT IS STORED IN A MENU_MODAL.HTML FILE-->
    <div id="modal_menu_insertion"></div>
    <script>
      $(function () {
        $("#modal_menu_insertion").load("html_chunk/menu_modal.html");
      });
    </script>

    <div id="modal_subscribe_insertion"></div>
    <script>
      $(function () {
        $("#modal_subscribe_insertion").load("html_chunk/subscribe_modal.html");
      });
    </script>

    <!-- Header = Title in big + social media Icon + quick description -->
    <header class="masthead" style="padding-bottom: 30px">
      <div class="textlanding">
        <center><h1>Density plot with labels on lines</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 explains how to add a text or labels on the line of a
            density chart. It uses the
            <a href="https://r-graph-gallery.com/ggplot2-package.html"
              >ggplot2</a
            >
            and
            <a href="https://www.r-graph-gallery.com/package/geomtextpath.html"
              >geomtextpath</a
            >
            packages for creating the chart and making it nice.
          </p>
        </center>
        <div style="text-align: center">
          <a
            class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger"
            href="density-plot.html"
            >Density section</a
          >
          <a
            class="btn btn-secondary btn-xl text-uppercase js-scroll-trigger"
            href="https://www.www.data-to-viz.com/graph/density.html"
            >Data to Viz</a
          >
        </div>
      </div>
    </header>

    <!-- THIS ALLOWS TO INSERT THE ADVERTISEMENT BANNER THAT IS STORED IN A BANNER.HTML FILE-->
    <div id="position_for_images"></div>
    <script>
      $(function () {
        $("#position_for_images").load("html_chunk/images.html");
      });
    </script>

    <!-- STYLE for chart pages but not the rest of tthe website -->
    <style>
      img {
        margin-top: 20px;
      }
    </style>

    <div class="container">
      <div id="package" class="section level1">
        <h1>Package</h1>
        <hr />
        <p>
          For this post, we need the
          <a href="https://r-graph-gallery.com/ggplot2-package.html">ggplot2</a>
          and
          <a href="https://www.r-graph-gallery.com/package/geomtextpath.html"
            >geomtextpath</a
          >
          packages.
        </p>
        <div class="sourceCode" id="cb1">
          <pre
            class="sourceCode r"
          ><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="co"># Install the packages if not already done</span></span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="co"># install.packages(&quot;ggplot2&quot;)</span></span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="co"># install.packages(&quot;geomtextpath&quot;)</span></span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a></span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a><span class="co"># Load the package</span></span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="fu">library</span>(geomtextpath)</span></code></pre>
        </div>
      </div>
      <div id="default-density-chart-in-ggplot2" class="section level1">
        <h1>Default density chart in ggplot2</h1>
        <hr />
        <div class="row">
          <div class="col-md-12 col-sm-12 align-self-center">
            <p>
              Here’s what the default density chart output looks like with
              <code>ggplot2</code>:
            </p>
          </div>
          <div class="col-md-12 col-sm-12">
            <p>
              <img
                src="361-density-plot-with-labels-on-lines_files/figure-html/thecode2-1.png"
                width="100%"
              />
            </p>
          </div>
        </div>
      </div>
      <div id="add-labels" class="section level1">
        <h1>Add labels</h1>
        <hr />
        <div class="row">
          <div class="col-md-12 col-sm-12 align-self-center">
            <p>
              With the <code>geom_textdensity()</code> function (inspired from
              the <code>geom_density()</code> func from
              <a href="https://r-graph-gallery.com/ggplot2-package.html"
                >ggplot2</a
              >), we can add the label of species directly on each density line.
            </p>
          </div>
          <div class="col-md-12 col-sm-12">
            <div class="sourceCode" id="cb2">
              <pre
                class="sourceCode r"
              ><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">data</span>(iris)</span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a></span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="fu">ggplot</span>(iris, <span class="fu">aes</span>(<span class="at">x =</span> Sepal.Length, <span class="at">colour =</span> Species, <span class="at">label =</span> Species)) <span class="sc">+</span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a>  <span class="fu">geom_textdensity</span>() <span class="sc">+</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a>  <span class="fu">theme_bw</span>() <span class="sc">+</span> <span class="fu">guides</span>(<span class="at">color =</span> <span class="st">&#39;none&#39;</span>)</span></code></pre>
            </div>
            <p>
              <img
                src="361-density-plot-with-labels-on-lines_files/figure-html/thecode3-1.png"
                width="100%"
              />
            </p>
          </div>
        </div>
      </div>
      <div id="change-texts-properties" class="section level1">
        <h1>Change texts properties</h1>
        <hr />
        <div class="row">
          <div class="col-md-12 col-sm-12 align-self-center">
            <p>
              The <code>geom_textdensity()</code> can take multiple arguments in
              order to change the properties of the labels:
            </p>
            <ul>
              <li>
                <p><code>size</code>: size of the text</p>
              </li>
              <li>
                <p><code>fontface</code>: style of the font</p>
              </li>
              <li>
                <p><code>vjust</code>: vertical adjustment</p>
              </li>
              <li>
                <p><code>hjust</code>: horizontal adjustment</p>
              </li>
            </ul>
            <p>
              The 2 last arguments can either be float (generally
              <strong>between -1 and 1</strong>) or string such as
              <code>xmid</code> (or <code>ymid</code>), <code>xmax</code> (or
              <code>ymax</code>) and <code>auto</code> (default).
            </p>
          </div>
          <div class="col-md-12 col-sm-12">
            <div class="sourceCode" id="cb3">
              <pre
                class="sourceCode r"
              ><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">library</span>(hrbrthemes)</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">data</span>(mtcars)</span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a>mtcars<span class="sc">$</span>labels <span class="ot">=</span> <span class="fu">ifelse</span>(mtcars<span class="sc">$</span>vs<span class="sc">==</span><span class="dv">0</span>, <span class="st">&quot;Type 0&quot;</span>, <span class="st">&quot;Type 1&quot;</span>)</span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="fu">ggplot</span>(mtcars, <span class="fu">aes</span>(<span class="at">x =</span> qsec, <span class="at">colour =</span> <span class="fu">as.factor</span>(labels), <span class="at">label =</span> <span class="fu">as.factor</span>(labels))) <span class="sc">+</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a>  <span class="fu">geom_textdensity</span>(<span class="at">size =</span> <span class="dv">6</span>, <span class="at">fontface =</span> <span class="dv">4</span>, <span class="co"># size of 6, bold italic</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a>                   <span class="at">vjust =</span> <span class="sc">-</span><span class="fl">0.4</span>, <span class="at">hjust =</span> <span class="st">&quot;ymid&quot;</span>) <span class="sc">+</span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a>  <span class="fu">theme_bw</span>() <span class="sc">+</span> <span class="fu">guides</span>(<span class="at">color =</span> <span class="st">&#39;none&#39;</span>)</span></code></pre>
            </div>
            <p>
              <img
                src="361-density-plot-with-labels-on-lines_files/figure-html/unnamed-chunk-2-1.png"
                width="672"
              />
            </p>
          </div>
        </div>
      </div>
      <div id="conclusion" class="section level1">
        <h1>Conclusion</h1>
        <hr />
        <p>
          In this post, we look at how to use the
          <a href="https://www.r-graph-gallery.com/package/geomtextpath.html"
            >geomtexpath</a
          >
          package to <strong>create density plot with text</strong>. To find out
          more about how to customize a density plot, see the
          <a href="https://r-graph-gallery.com/density-plot.html"
            >dedicated section</a
          >.
        </p>
        <!-- Close container -->
      </div>
    </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="scatterplot.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="https://www.r-graph-gallery.com/img/section/ScatterPlot150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Scatter</div>
          </div>
          <div class="col-md-2 col-sm-4 portfolio-item">
            <a class="portfolio-link" href="heatmap.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="https://www.r-graph-gallery.com/img/section/Heatmap150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Heatmap</div>
          </div>
          <div class="col-md-2 col-sm-4 portfolio-item">
            <a class="portfolio-link" href="correlogram.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="https://www.r-graph-gallery.com/img/section/Correlogram150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Correlogram</div>
          </div>
          <div class="col-md-2 col-sm-4 portfolio-item">
            <a class="portfolio-link" href="bubble-chart.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="https://www.r-graph-gallery.com/img/section/BubblePlot150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Bubble</div>
          </div>
          <div class="col-md-2 col-sm-4 portfolio-item">
            <a class="portfolio-link" href="connected-scatterplot.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="https://www.r-graph-gallery.com/img/section/ScatterConnected150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Connected scatter</div>
          </div>
          <div
            class="col-md-2 col-sm-4 portfolio-item show column correlation numonly bigdata"
          >
            <a class="portfolio-link" href="2d-density-chart.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="https://www.r-graph-gallery.com/img/section/2dDensity150.png"
                alt=""
              />
            </a>
            <div class="captionPortfolio">Density 2d</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>❤️ 10 best R tricks ❤️</h2>
            <p style="max-width: 620px; text-align: center">
              👋 After crafting hundreds of R charts over 12 years, I've
              distilled my top 10 tips and tricks. Receive them via email!
              <b>One insight per day for the next 10 days</b>! 🔥
            </p>
            <div style="display: flex; justify-content: center">
              <script
                async
                data-uid="329fda5a7e"
                src="https://prodigious-trailblazer-3628.ck.page/329fda5a7e/index.js"
              ></script>
            </div>
          </div>
        </div>
      </div>
    </section>

    <!-- ============================ FOOTER SECTION ============================ -->

    <!-- AdThrive Footer Code Start-->
    <script
      type="text/javascript"
      async
      src="https://btloader.com/tag?o=5698917485248512&upapi=true&domain=r-graph-gallery.com"
    ></script>
    <script>
      !(function () {
        "use strict";
        var e;
        (e = document),
          (function () {
            var t, n;
            function r() {
              var t = e.createElement("script");
              (t.src =
                "https://cafemedia-com.videoplayerhub.com/galleryplayer.js"),
                e.head.appendChild(t);
            }
            function a() {
              var t = e.cookie.match("(^|[^;]+)\s*__adblocker\s*=\s*([^;]+)");
              return t && t.pop();
            }
            function c() {
              clearInterval(n);
            }
            return {
              init: function () {
                var e;
                "true" === (t = a())
                  ? r()
                  : ((e = 0),
                    (n = setInterval(function () {
                      (100 !== e && "false" !== t) || c(),
                        "true" === t && (r(), c()),
                        (t = a()),
                        e++;
                    }, 50)));
              },
            };
          })().init();
      })();
    </script>
    <!-- AdThrive Footer Code End -->

    <footer class="bg-light" id="myFooter">
      <div class="container">
        <div class="row">
          <div class="col-md-4">
            <span class="copyright"
              >Copyright &copy; the R graph gallery 2025</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>