title: Line color and width by slope in ggplot2
body: <p>This is relates very closely to <a href="https://stackoverflow.com/questions/34113989/changing-line-color-in-ggplot-based-on-slope">this question</a>, as well as <a href="https://stackoverflow.com/questions/39023962/changing-line-color-in-ggplot-based-on-several-factors-slope/39025244">this</a>, answers to which I don't understand, at least in this context. I would like to make the difference between increasing and decreasing scores (e.g. repeated psychological measurements from T1 to T2) prominent by placing a heatmap-style gradient on lines based on their slopes. In other words, I'd like to use e.g. <a href="https://cran.r-project.org/web/packages/viridis/vignettes/intro-to-viridis.html" rel="nofollow noreferrer">Viridis</a>'s inferno-scale, so that the lines which decrease most sharply tend towards darkness and those which increase most sharply tend towards light.   </p>
  
  <p>Many thanks for any ideas!</p>
  
  <pre><code>data &lt;- data.frame(id = 1:500, 
                                 Intrinsic_01_T1 = sample(1:5, 500, replace = TRUE), 
                                 Intrinsic_02_T1 = sample(1:5, 500, replace = TRUE), 
                                 Intrinsic_03_T1 = sample(1:5, 500, replace = TRUE), 
                                 Intrinsic_01_T2 = sample(1:5, 500, replace = TRUE, prob = c(0.1, 0.1, 0.2, 0.3, 0.3)), 
                                 Intrinsic_02_T2 = sample(1:5, 500, replace = TRUE), 
                                 Intrinsic_03_T2 = sample(1:5, 500, replace = TRUE, prob = c(0.3, 0.3, 0.2, 0.1, 0.1)))
  
  pd &lt;- position_dodge(0.4)
  
  data %&gt;% 
    tidyr::gather(variable, value, -id) %&gt;% 
    tidyr::separate(variable, c("item", "time"), sep = "_T") %&gt;% 
    dplyr::mutate(value = jitter(value, amount = 0.1)) %&gt;% # Y-axis jitter to make points more readable
  ggplot(aes(x = time, y = value, group = id)) +
    geom_point(size = 1, alpha = .2, position = pd) +
    geom_line(alpha = .2, position = pd) +
    ggtitle('Multiple indicator LCS model') + 
    ylab('Intrinsic motivation scores') +
    xlab('Time points') + 
    facet_wrap("item")
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/iNhoU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iNhoU.png" alt="The resulting figure"></a></p>