title: How to add median vertical line to panels of lattice density plot?
body: <p>I'm using a data frame in a long format to create a panelled density plot in <code>lattice</code>. Now I'd like to add a vertical line at the median x-value within each panel. I found a suggestion for doing it in an <code>dotplot</code> ( <a href="http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-td4632513.html" rel="nofollow">http://r.789695.n4.nabble.com/how-to-add-a-vertical-line-for-each-panel-in-a-lattice-dotplot-with-log-scale-td4632513.html</a> ), but this doesn't work for me. Here is my code:</p>
  
  <pre><code>data(Chem97, package="mlmRev")
  
  densityplot(~gcsescore | factor(score), data=Chem97,
          panel=function(...){
            panel.densityplot(...)
            median.values &lt;- median(x) 
            panel.abline(v=median.values, col.line="red") 
          })
  </code></pre>
  
  <p>The error is: <code>Object x not found</code>. So I tried the following:</p>
  
  <pre><code>panel=function(x,...){
            panel.densityplot(...)
         }
  </code></pre>
  
  <p>The moment I add <code>x</code> as an argument to the panel function, I get the error <code>Error using packet 1 (2, 3 etc.). x is missing</code>.</p>
  
  <p>What's going wrong?</p>