<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/label-line-ends-in-time-series-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2"> blogR</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://twitter.com/drsimonj" target="_blank">@drsimonj</a> here with a quick share on making great use of the secondary y axis with ggplot2 – super helpful if you’re plotting groups of time series!</p>
<p>Here’s an example of what I want to show you how to create (pay attention to the numbers of the right):</p>
<p><a rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/a3LT3yKxA29K3Vc1aXnDsA0xspap.png?ssl=1" target="_blank"><img alt="init-example-1.png" src="https://i1.wp.com/svbtleusercontent.com/a3LT3yKxA29K3Vc1aXnDsA0xspap_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2>
<a rel="nofollow" href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2#setup_2" target="_blank"> </a>Setup</h2>
<p>To setup we’ll need the tidyverse package and the <code>Orange</code> data set that comes with R. This tracks the circumference growth of five orange trees over time.</p>
<pre><code>library(tidyverse)

d &lt;- Orange

head(d)
#> Grouped Data: circumference ~ age | Tree
#>   Tree  age circumference
#> 1    1  118            30
#> 2    1  484            58
#> 3    1  664            87
#> 4    1 1004           115
#> 5    1 1231           120
#> 6    1 1372           142
</code></pre>
<h2>
<a rel="nofollow" href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2#template-code_2" target="_blank"> </a>Template code</h2>
<p>To create the basic case where the numbers appear at the end of your time series lines, your code might look something like this:</p>
<pre><code># You have a data set with:
# - GROUP colum
# - X colum (say time)
# - Y column (the values of interest)
DATA_SET

# Create a vector of the last (furthest right) y-axis values for each group
DATA_SET_ENDS &lt;- DATA_SET %>% 
  group_by(GROUP) %>% 
  top_n(1, X) %>% 
  pull(Y)

# Create plot with `sec.axis`
ggplot(DATA_SET, aes(X, Y, color = GROUP)) +
    geom_line() +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(sec.axis = sec_axis(~ ., breaks = DATA_SET_ENDS))
</code></pre>
<h2>
<a rel="nofollow" href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2#let39s-see-it_2" target="_blank"> </a>Let’s see it!</h2>
<p>Let’s break it down a bit. We already have our data set where the group colum is <code>Tree</code>, the X value is <code>age</code>, and the Y value is <code>circumference</code>.</p>
<p>So first get a vector of the last (furthest right) values for each group:</p>
<pre><code>d_ends &lt;- d %>% 
  group_by(Tree) %>% 
  top_n(1, age) %>% 
  pull(circumference)

d_ends
#> [1] 145 203 140 214 177
</code></pre>
<p>Next, let’s set up the basic plot without the numbers to see how each layer adds up.</p>
<pre><code>ggplot(d, aes(age, circumference, color = Tree)) +
      geom_line()
</code></pre>
<p><a rel="nofollow" href="https://i1.wp.com/svbtleusercontent.com/4zKSnhW3JDUzwHbmKGBMAo0xspap.png?ssl=1" target="_blank"><img alt="unnamed-chunk-5-1.png" src="https://i0.wp.com/svbtleusercontent.com/4zKSnhW3JDUzwHbmKGBMAo0xspap_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>Now we can use <code>scale_y_*</code>, with the argument <code>sec.axis</code> to create a second axis on the right, with numbers to be displayed at <code>breaks</code>, defined by our vector of line ends:</p>
<pre><code>ggplot(d, aes(age, circumference, color = Tree)) +
      geom_line() +
      scale_y_continuous(sec.axis = sec_axis(~ ., breaks = d_ends))
</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/xfnR83uEfJP9nEz7ZToAkt0xspap.png?ssl=1" target="_blank"><img alt="unnamed-chunk-6-1.png" src="https://i1.wp.com/svbtleusercontent.com/xfnR83uEfJP9nEz7ZToAkt0xspap_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<p>This is a great start, The only major addition I suggest is expanding the margins of the x-axis so the gap disappears. You do this with <code>scale_x_*</code> and the <code>expand</code> argument:</p>
<pre><code>ggplot(d, aes(age, circumference, color = Tree)) +
      geom_line() +
      scale_y_continuous(sec.axis = sec_axis(~ ., breaks = d_ends)) +
      scale_x_continuous(expand = c(0, 0))
</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/65yGdKt3cRfnBpJegdYRC0xspap.png?ssl=1" target="_blank"><img alt="unnamed-chunk-7-1.png" src="https://i0.wp.com/svbtleusercontent.com/65yGdKt3cRfnBpJegdYRC0xspap_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2>
<a rel="nofollow" href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2#polishing-it-up_2" target="_blank"> </a>Polishing it up</h2>
<p>Like it? Here’s the code to recreate the first polished plot:</p>
<pre><code>library(tidyverse)

d &lt;- Orange %>% 
  as_tibble()

d_ends &lt;- d %>% 
  group_by(Tree) %>% 
  top_n(1, age) %>% 
  pull(circumference)

d %>% 
  ggplot(aes(age, circumference, color = Tree)) +
    geom_line(size = 2, alpha = .8) +
    theme_minimal() +
    scale_x_continuous(expand = c(0, 0)) +
    scale_y_continuous(sec.axis = sec_axis(~ ., breaks = d_ends)) +
    ggtitle(&quot;Orange trees getting bigger with age&quot;,
            subtitle = &quot;Based on the Orange data set in R&quot;) +
    labs(x = &quot;Days old&quot;, y = &quot;Circumference (mm)&quot;, caption = &quot;Plot by @drsimonj&quot;)
</code></pre>
<p><a rel="nofollow" href="https://i0.wp.com/svbtleusercontent.com/a3LT3yKxA29K3Vc1aXnDsA0xspap.png?ssl=1" target="_blank"><img alt="init-example-8.png" src="https://i1.wp.com/svbtleusercontent.com/a3LT3yKxA29K3Vc1aXnDsA0xspap_small.png?w=456&ssl=1" data-recalc-dims="1"></a></p>
<h2>
<a rel="nofollow" href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2#sign-off_2" target="_blank"> </a>Sign off</h2>
<p>Thanks for reading and I hope this was useful for you.</p>
<p>For updates of recent blog posts, follow <a rel="nofollow" href="https://twitter.com/drsimonj" target="_blank">@drsimonj</a> on Twitter, or email me at <a rel="nofollow" href="mailto:drsimonjackson@gmail.com" target="_blank">drsimonjackson@gmail.com</a> to get in touch.</p>
<p>If you’d like the code that produced this blog, check out the <a rel="nofollow" href="https://github.com/drsimonj/blogR" target="_blank">blogR GitHub repository</a>.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://drsimonj.svbtle.com/label-line-ends-in-time-series-with-ggplot2"> blogR</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/b3W6ONtdin0" height="1"></a></div>