title: If else in ggplot + switching values between character and numeric for geom_vline
body: <p>I need to integrate <code>if else</code> block in ggplot, where depending on value for <code>variable_to_switch</code> it will:</p>
  <ul>
  <li>if value is numeric -&gt; plot line</li>
  <li>value is text -&gt; do NOT plot line</li>
  </ul>
  <p><em>I provide my code in the most complete version to take in account possible conflicts with provided solution.</em></p>
  <pre><code>
  #  Creating Data ----
  
  obs_number &lt;- 500
  mean &lt;- 2
  median &lt;- 3
  dens_mode &lt;- 2
  variable_to_switch &lt;- &quot;Text&quot;
  dlt_standard_mean &lt;- 2
  cut_off &lt;- 12
  max_dlt &lt;- 15
  
  metrics_test &lt;- data.frame(xintercept = c(obs_number,cut_off,max_dlt, 0, mean, median, dens_mode,variable_to_switch, dlt_standard_mean ),
                        label = c(&quot;Label 1&quot;,
                                  &quot;Label 2&quot;,
                                  &quot;Label 3&quot;,
                                  &quot;&quot;, 
                                  &quot;Label 4&quot;,
                                  &quot;Label 5&quot;,
                                  &quot;Label 6&quot;,
                                  &quot;Label 7&quot;,
                                  &quot;Label 8&quot;))
  
  
  mean_by_batch_route &lt;- c(rep(1,81), rep(2,252), rep(3,170), rep(4,41))
  Label &lt;- c(rep('Label',544))
  
  data_to_plot_test &lt;- data.frame(cbind(Label,mean_by_batch_route))
  data_to_plot_test$mean_by_batch_route &lt;- as.numeric(data_to_plot_test$mean_by_batch_route)
  
  # Plotting ---
  
  x_data &lt;- 4
  
  histo_density_plot &lt;- ggplot(data_to_plot_test, aes(x = mean_by_batch_route)) +
    
    geom_histogram(aes(y = ..density..), binwidth = 1, colour= &quot;black&quot;, fill = &quot;white&quot;) +
    geom_density(adjust = 1.75, aes(y=..density..), fill=&quot;blue&quot;, alpha = .25)
  
  if(variable_to_switch == &quot;Text&quot;){
    
    histo_density_plot &lt;- histo_density_plot +
      
      geom_vline(aes(xintercept = xintercept, color = label),
                 data = metrics_test[c(6,7,9), ], linetype = &quot;dashed&quot;, size = 0.5, alpha = 1,
                 key_glyph = draw_key_text) +
      
      
      
      scale_color_manual(
        name= &quot;Values&quot;,
        limits = metrics_test$label,
        values = c(&quot;navyblue&quot;,&quot;darkcyan&quot;, &quot;cyan4&quot;, &quot;white&quot;, &quot;black&quot;, &quot;chartreuse4&quot;, &quot;purple&quot;,&quot;darkgoldenrod3&quot;, &quot;red&quot;),
        guide = guide_legend(override.aes = list(label = metrics_test$xintercept, 
                                                 size = 3)))
    
  } else {
    
    histo_density_plot &lt;- histo_density_plot +
      geom_vline(aes(xintercept = xintercept, color = label),
                 data = metrics_test[6:nrow(metrics_test), ], linetype = &quot;dashed&quot;, size = 0.5, alpha = 1,
                 key_glyph = draw_key_text) +
      
      
      
      scale_color_manual(
        name= &quot;Values&quot;,
        limits = metrics_test$label,
        values = c(&quot;navyblue&quot;,&quot;darkcyan&quot;, &quot;cyan4&quot;, &quot;white&quot;, &quot;black&quot;, &quot;chartreuse4&quot;, &quot;purple&quot;,&quot;darkgoldenrod3&quot;, &quot;red&quot;),
        guide = guide_legend(override.aes = list(label = metrics_test$xintercept, 
                                                 size = 3)))
    
    
  }
  
  histo_density_plot &lt;- histo_density_plot +
    
    theme(plot.title = element_text(hjust = 0.5, size = 8), 
          axis.title.x = element_text(colour = &quot;darkblue&quot;, size=8),
          axis.text.x = element_text(face=&quot;plain&quot;, color=&quot;black&quot;, 
                                     size=8, angle=0),
          axis.title.y = element_text(colour = &quot;darkblue&quot;, size=8),
          axis.text.y = element_text(face=&quot;plain&quot;, color=&quot;black&quot;, 
                                     size=8, angle=0),
          legend.position = c(.80, .70),
          legend.title=element_text(size=8),
          legend.text = element_text(size = 8)
    )+
    
    
    labs(title = relevant_title, y = &quot;Distribution fors DLT values, frequency&quot;, x = &quot;DLT for the route: average DLT per batch, days&quot;)+
    
    scale_x_continuous(breaks = seq(0, x_data,
                                    ifelse(x_data &lt;= 20, 1,
                                           ifelse((x_data &gt; 20 &amp; x_data &lt; 50), 5, 10))))
  
  histo_density_plot
  
  
  </code></pre>
  <p>If <code>variable_to_switch == &quot;Text&quot;</code> I will have this error:
  <code>Error: Discrete value supplied to continuous scale</code>.</p>
  <p>For <code>variable_to_switch</code> equal to any numeric value plot works.</p>
  <p>I want to switch between character and numeric value for <code>variable_to_switch</code>.<br />
  <strong>Desired output</strong> for case with <code>variable_to_switch == &quot;Text&quot;</code> :
  <a href="https://i.stack.imgur.com/nebty.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nebty.png" alt="enter image description here" /></a></p>