title: fixed fill for different sections of a density plot with ggplot
body: <p>Given draws from a <code>rnorm</code>, and cutoff <code>c</code> I want my plot to use the following colors:</p>
  
  <ol>
  <li>Red for the section that is to the left of <code>-c</code></li>
  <li>Blue for the section in between <code>-c</code> and <code>c</code></li>
  <li>and Green for the section that is to the right of <code>c</code></li>
  </ol>
  
  <p>For example, if my data is:</p>
  
  <pre><code>set.seed(9782)
  mydata &lt;- rnorm(1000, 0, 2)
  c &lt;- 1
  </code></pre>
  
  <p>I want to plot something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/GYAX3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GYAX3.png" alt="enter image description here"></a></p>
  
  <p>But if my data is all to the right of <code>c</code> the whole plot should be green. Similarly, if all is between <code>-c</code> and <code>c</code> or to the left of <code>-c</code> the plot should be all red or blue.</p>
  
  <p>This is the code I wrote:</p>
  
  <pre><code>MinD &lt;- min(mydata)
  MaxD &lt;- max(mydata)
  
  df.plot &lt;- data.frame(density = mydata)
  
  if(c==0){
    case &lt;- dplyr::case_when((MinD &lt; 0 &amp; MaxD &gt;0) ~ "L_and_R",
                             (MinD &gt; 0) ~ "R",
                             (MaxD &lt; 0) ~ "L")
  }else{
    case &lt;- dplyr::case_when((MinD &lt; -c &amp; MaxD &gt;c) ~ "ALL",
                             (MinD &gt; -c &amp; MaxD &gt; c) ~ "Center_and_R",
                             (MinD &gt; -c &amp; MaxD &lt;c) ~ "Center",
                             (MinD &lt; -c &amp; MaxD &lt; c) ~ "Center_and_L",
                             MaxD &lt; -c ~ "L",
                             MaxD &gt; c ~ "R")
  }
  
  # Draw the Center
  
  if(case %in% c("ALL", "Center_and_R", "Center", "Center_and_L")){
    ds &lt;- density(df.plot$density, from = -c, to = c)
    ds_data_Center &lt;- data.frame(x = ds$x, y = ds$y, section="Center")
  } else{
    ds_data_Center &lt;- data.frame(x = NA, y = NA, section="Center")
  }
  
  # Draw L
  
  if(case %in% c("ALL", "Center_and_L", "L", "L_and_R")){
    ds &lt;- density(df.plot$density, from = MinD, to = -c)
    ds_data_L &lt;- data.frame(x = ds$x, y = ds$y, section="L")
  } else{
    ds_data_L &lt;- data.frame(x = NA, y = NA, section="L")
  }
  
  # Draw R
  
  if(case %in% c("ALL", "Center_and_R", "R", "L_and_R")){
    ds &lt;- density(df.plot$density, from = c, to = MaxD)
    ds_data_R &lt;- data.frame(x = ds$x, y = ds$y, section="R")
  } else{
    ds_data_R &lt;- data.frame(x = NA, y = NA, section="R")
  }
  
  L_Pr &lt;- round(mean(mydata &lt; -c),2)
  Center_Pr &lt;- round(mean((mydata&gt;-c &amp; mydata&lt;c)),2)
  R_Pr &lt;- round(mean(mydata &gt; c),2)
  
  filldf &lt;- data.frame(section = c("L", "Center", "R"), 
                       Pr = c(L_Pr, Center_Pr, R_Pr), 
                       fill = c("red", "blue", "green")) %&gt;% 
    dplyr::mutate(section = as.character(section))
  
  
  if(c==0){
    ds_data &lt;- suppressWarnings(dplyr::bind_rows(ds_data_L, ds_data_R)) %&gt;% 
      dplyr::full_join(filldf, by = "section") %&gt;% filter(Pr!=0) %&gt;% 
      dplyr::full_join(filldf, by = "section") %&gt;% mutate(section = ordered(section, levels=c("L","R"))) 
    ds_data &lt;- ds_data[order(ds_data$section), ] %&gt;%  
      filter(Pr!=0) %&gt;% 
      mutate(Pr=scales::percent(Pr))
  }else{
    ds_data &lt;- suppressWarnings(dplyr::bind_rows(ds_data_Center, ds_data_L, ds_data_R)) %&gt;% 
      dplyr::full_join(filldf, by = "section") %&gt;% mutate(section = ordered(section, levels=c("L","Center","R"))) 
    ds_data &lt;- ds_data[order(ds_data$section), ] %&gt;%  
      filter(Pr!=0) %&gt;% 
      mutate(Pr=scales::percent(Pr))
  }
  
  fillScale &lt;- scale_fill_manual(name = paste0("c = ", c, ":"),
                                 values = as.character(unique(ds_data$fill)))
  
  p &lt;- ggplot(data = ds_data, aes(x=x, y=y, fill=Pr)) + 
    geom_area() + fillScale 
  </code></pre>
  
  <p>Alas, I cannot figure out how to assign the colors to the different sections while keeping the percentages as labels for the colors.</p>