title: Split density plot in 4 groups and add the groups to data table
body: <p>I'm creating a density plot with <code>ggplot()</code> in <code>R</code>, where I specify the <code>median</code>, <code>5%</code> and <code>95%</code> quantiles with a vertical line (<code>geom_vline()</code>). Here is my plot construction:</p>
  <pre><code>probs &lt;- c(0.05, 0.95)
  quantiles &lt;- quantile(dt.all2018$Qeff, prob = probs)
  q5 &lt;- as.numeric(quantiles[1])
  q95 &lt;- as.numeric(quantiles[2])
  median &lt;- median(dt.all2018$Qeff)
  
  
  p &lt;- (ggplot(dt.all2018) + 
        geom_density(aes(x = Qeff, y = ..scaled..),  colour = &quot;#007d3c&quot;) +
        ggtitle(&quot;Qeff 2018&quot;) +
        geom_vline(aes(xintercept = median, color = &quot;median&quot;), linetype = &quot;dashed&quot;) +
        geom_vline(aes(xintercept = q5, color = &quot;5%&quot;), linetype = &quot;dashed&quot;) +
        geom_vline(aes(xintercept = q95, color = &quot;95%&quot;), linetype = &quot;dashed&quot;) +
        scale_color_manual(name = &quot;statistics&quot;, values = c('5%' = &quot;#0000FF&quot;, '95%' = &quot;red&quot;, median = &quot;#007d3c&quot;)) +
        theme(panel.background = element_blank(), axis.line = element_line(colour = &quot;black&quot;),
              plot.title = element_text(lineheight = .8, hjust = 0.5, face = &quot;bold&quot;),
              legend.box.background = element_rect(colour = &quot;black&quot;), legend.box.margin = margin(t = 1, l = 1),
              legend.title = element_blank()))%&gt;%
        ggplotly()
  </code></pre>
  <p>Then my plot looks like this (without my self-painted parts):</p>
  <p><a href="https://i.stack.imgur.com/DFbeZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DFbeZ.png" alt="enter image description here" /></a></p>
  <p>Now I want to create a new column <code>group</code> that contains the group number of my data, i.e. add the group to which it falls to the respective <code>Qeff</code>. Group 1 is everything up to <code>5%</code>, group two is everything between <code>5%</code> and <code>median</code>, group 3 is everything between <code>median</code> and <code>95%</code> and group 4 is everything after <code>95%</code>. The <code>group</code> column should only contain the numbers 1 to 4.</p>
  <p>How can I do this?</p>
  <p>Here is a short snippet of my data table:</p>
  <pre><code>structure(list(EK = c(311746.83, 0, 408503.01, 965723.51, 447176.86, 
  0, 0, 237703401.51, 11650300.16, 761470.17, 15514898.49, 791067269.75, 
  35591131, 10754272.33, 9496742.11, 512370.9, 1134032.95, 35318984.4, 
  5630139.9, 1111511.07), EH = c(345245.44, 0, 439620.18, 894773.08, 
  485161.85, 0, 0, 331524231.52, 19502922.3, 1007182.97, 13714848.49, 
  470803897.97, 36394200.3, 11485817.1, 9542583.17, 532302.49, 
  1071746.46, 20666845.08, 5333889.99, 938096.94), Peff = c(104.78, 
  0, 91.52, 112.18, 113.39, 0, 0, 86.18, 101.04, 104.39, 106.23, 
  86.4, 96.19, 86.38, 113.5, 115.88, 104.61, 96.31, 95.6, 101.71
  ), Qeff = c(-0.01, 0, 0, 0, 0, 0, 0, 0, -0.01, -0.01, 0, 0, 0, 
  0, 0.01, 0, 0, 0, 0, 0)), class = c(&quot;data.table&quot;, &quot;data.frame&quot;
  ), row.names = c(NA, -20L), .internal.selfref = &lt;pointer: 0x000002671f801ef0&gt;)
  </code></pre>