title: What does 'x' do in curve/dnorm, and why does it work in my function?
body: <p>I am making a function <code>Prop.Histogram()</code> that plots data as a histogram showing the proportions with a normal distribution curve added to it. Addition of the curve was difficult for me to achieve, but I succeeded (see code below)!</p>
  <p>Note: I personally prefer to work with the pipe-operator <code>%&gt;%</code> from the package magrittr in my codes. Though, as probably not everyone is familiar with this operator and/or this package (or they prefer not to use it), I'll also provide the same code without using magrittr below.</p>
  <p><strong>Code using magrittr</strong></p>
  <pre><code>Prop.Histogram &lt;- function(data,
     xlim_min, xlim_max, x_BreakSize,
     ylim_max, y_steps) {
  
  # Load packages
  library(magrittr)
  
  # Make histogram of data without y-axis
  hist(data, freq = FALSE, ylab = &quot;Proportion&quot;,
     xlim = c(xlim_min, xlim_max), breaks = seq(from = xlim_min, to = xlim_max, by = x_BreakSize),
     ylim = c(0, ylim_max %&gt;% divide_by(., x_BreakSize)), yaxt = &quot;n&quot;)
  
  # I divided ylim_max by x_BreakSize, as I want ylim_max to be equal to the max proportion shown on the y_axis (and not to the max density)
  
  # Add y-axis that shows proportion and not density
  axis(side = 2,
     at = seq(from = 0, to = ylim_max %&gt;% divide_by(., x_BreakSize), by = y_steps %&gt;% divide_by(., x_BreakSize)),
     labels = seq(from = 0, to = ylim_max, by = y_steps))
  
  box()
  
  # Add curve to histogram
  curve(dnorm(x, mean = mean(data), sd = sd(data)), lwd = 5, add = TRUE, yaxt = &quot;n&quot;)
  
  }
  </code></pre>
  <p><strong>Same code without using magrittr</strong></p>
  <pre><code>Prop.Histogram &lt;- function(data,
     xlim_min, xlim_max, x_BreakSize,
     ylim_max, y_steps) {
  
  # Load packages
  library(magrittr)
  
  # Make histogram of data without y-axis
  hist(data, freq = FALSE, ylab = &quot;Proportion&quot;,
     xlim = c(xlim_min, xlim_max), breaks = seq(from = xlim_min, to = xlim_max, by = x_BreakSize),
     ylim = c(0, ylim_max/x_BreakSize), yaxt = &quot;n&quot;)
  
  # I divided ylim_max by x_BreakSize, as I want ylim_max to be equal to the max proportion shown on the y_axis (and not to the max density)
  
  # Add y-axis that shows proportion and not density
  axis(side = 2,
     at = seq(from = 0, to = ylim_max/x_BreakSize, by = y_steps/x_BreakSize),
     labels = seq(from = 0, to = ylim_max, by = y_steps))
  
  box()
  
  # Add curve to histogram
  curve(dnorm(x, mean = mean(data), sd = sd(data)), lwd = 5, add = TRUE, yaxt = &quot;n&quot;)
  
  }
  </code></pre>
  <p>This code does exactly what I want it to do: it plots the proportions and adds a normal distribution curve to the plot. Though, I do have difficulties understanding why addition of the curve actually works.</p>
  <p><strong>Main question (1)</strong>: I have to put <code>x</code> as the first argument in <code>dnorm()</code>, and even though I have not defined <code>x</code>, it works! So my first and main question is: what is <code>x</code>, what does it do, and why does it work in my function?</p>
  <p><strong>Second question (2)</strong>: My second question is whether it is possible (and, if so, how) to use magrittr pipe-operators (<code>%&gt;%</code>) in the line of code that adds the curve to the plot. (Even if using operators is not the best way to do so in this case, I am still interested in the answer as I am eager to learn!)</p>
  <hr />
  <p>First of all, for those who want to try out my code: here is some data that is representative of data that I want to plot:</p>
  <pre><code>data &lt;- rnorm(724, mean = 84, sd = 33)
  Prop.Histogram(data,
                 xlim_min = -50, xlim_max = 200, x_BreakSize = 10,
                 ylim_max = 0.15, y_step = 0.05)
  </code></pre>
  <hr />
  <p><strong>Main question (1)</strong>: role of <code>x</code> in <code>dnorm()</code>/<code>curve()</code></p>
  <p>I started by using <code>data</code> instead of <code>x</code> as the first argument of <code>dnorm()</code>, but this didn't work as it resulted in the following error message:</p>
  <pre><code>   Error in curve(dnorm(data, mean = mean(data), sd = sd(data)), lwd = 5,  :
     'expr' must be a function, or a call or an expression containing 'x'
  </code></pre>
  <p>But then, when I take <code>dnorm(data, mean = mean(data), sd = sd(data))</code> and run it individually (not as an argument of <code>curve()</code>, it gives me 724 values (of which I don't know what they meaning, but at least it's not an error message). Which is weird, since using <code>data</code> as the first argument when <code>dnorm()</code> is part of <code>curve</code> in my formula results in an error message as we saw previously.</p>
  <p>Then, when I change <code>data</code> for <code>x</code> and run <code>dnorm(x, mean = mean(data), sd = sd(data))</code> (again not as an argument of <code>curve()</code>), it gives me another error message:</p>
  <pre><code>   Error in dnorm(x, mean = mean(data), sd = sd(data)) :
     object 'x' not found
  </code></pre>
  <p>This I can understand, as I've not defined <code>x</code> anywhere in my code. But that rises the question: why do I not get this same error message when I run my (working) function.</p>
  <p>In short, I observed that <code>x</code> must be the first argument in <code>dnorm()</code> when <code>dnorm()</code> is used as an argument in <code>curve()</code>, but <code>x</code> cannot be used as the first argument when <code>dnorm()</code> is used individually. Conclusion: I am lost.</p>
  <p>Of course, when I am lost in R, I always look at the help page of R. The help page of <code>dnorm()</code> states that <code>x</code> is a vector of quantiles... that's it. I know those words individually, but have no idea what it means in my code (as I've not defined <code>x</code>, so what vector or what quantiles is the R help page talking about?).</p>
  <hr />
  <p><strong>Second question (2)</strong>: use of magrittr in code</p>
  <p>I've tried to write the code <code>curve(dnorm(x, mean = mean(data), sd = sd(data)), lwd = 5, add = TRUE, yaxt = &quot;n&quot;)</code> using magrittr, but it does not work. Here are some examples I've tried:</p>
  <pre><code>data %&gt;% dnorm(x, mean = mean(.), sd = sd(.)) %&gt;% curve(., lwd = 5, add = TRUE, yaxt = &quot;n&quot;)
  data %&gt;% dnorm(x, mean = mean(.), sd = sd(.)) %&gt;% curve(lwd = 5, add = TRUE, yaxt = &quot;n&quot;)
  dnorm(x, mean = mean(data), sd = sd(data)) %&gt;% curve(., lwd = 5, add = TRUE, yaxt = &quot;n&quot;)
  </code></pre>
  <p>They all result in the same error message:</p>
  <pre><code>  Error in dnorm(x, mean = mean(data), sd = sd(data)) : 
    object 'x' not found
  </code></pre>
  <p>I'd like to know if it's possible to use magrittr operators like <code>%&gt;%</code> in this situation (even if it's not the best option).</p>
  <hr />
  <p>PS. This is my first time posting, so please feel free to give feedback or ask me for more information if needed. Thank you in advance!</p>