title: Error message: no non-missing arguments to max; returning -Inf
body: <p>I am trying to determine the width at half height of a density plot, and I found the following code in a previous post:</p>
  
  <pre><code>  d &lt;- ggplot(A0, aes(DIAMETER)) +
    geom_density()
  
  xmax &lt;- d$x[d$y==max(d$y, na.rm = TRUE)]
  x1 &lt;- d$x[d$x &lt; xmax][which.min(abs(d$y[d$x &lt; xmax]-max(d$y)/2))]
  x2 &lt;- d$x[d$x &gt; xmax][which.min(abs(d$y[d$x &gt; xmax]-max(d$y)/2))]
  FWHM &lt;- x2-x1
  </code></pre>
  
  <p>when I execute it though I get the following error message relative to the function max()</p>
  
  <pre><code>Warning message:
  In max(d$y, na.rm = TRUE) : no non-missing arguments to max; returning -Inf
  </code></pre>
  
  <p>I looked a bit around and saw that this could be due to the presence of NA value in my dataset but that is not the case (structure of data frame below).. does someone know how I could fix this problem ? thanks in advance!</p>
  
  <pre><code>str(A0)
  Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   387 obs. of  3 variables:
   $ SAMPLE  : Factor w/ 5 levels "A","B","C","D",..: 1 1 1 1 1 1 1 1 1 1 ...
   $ TIME    : Factor w/ 3 levels "0","24","72": 1 1 1 1 1 1 1 1 1 1 ...
   $ DIAMETER: num  13.57 3.76 10.67 14.74 4.2 ...
  </code></pre>