Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In ggplot2, the rule is that scale transformation precedes statistical transformation which in turn precedes coordinate transformation. In this context, the function (via stat_function()) is the statistical transformation. </p> <p>If you use a scale_x/y_*() function in a ggplot2 call, it will apply the scale transformation(s) first before computing the function.</p> <p>Case 0: Plot in the original scales of x and y.</p> <pre><code>ggplot(data.frame(x = 1:1e4, y = 1:1e4), aes(x, y)) + stat_function(fun = function(x) x) </code></pre> <p>Case 1a: Both x and y are log transformed before the function is computed because of the presence of scale_x/y_log10(). You can see this from the values on their respective scales (compare to Case 0).</p> <pre><code>ggplot(data.frame(x = 1:1e4, y = 1:1e4), aes(x, y)) + stat_function(fun = function(x) x) + scale_x_log10() + scale_y_log10() </code></pre> <p>Case 1b: x is log transformed in the original data frame. Consequently, the function actually operates on the log10(x) values, so will still be a straight line, but on the log10 scale in both x and y.</p> <pre><code>ggplot(data.frame(x = log10(seq(1e4)), y = seq(1e4)), aes(x, y)) + stat_function(fun = function(x) x) </code></pre> <p>Case 1c: The same as 1b, with one exception: the x-scale is in the original units but the y-scale is in log10(x) units, because the scale transformation on x occurs before the statistical transformation f(y) = y is computed, where y = log10(x).</p> <pre><code>ggplot(data.frame(x = seq(1e4), y = seq(1e4)), aes(x, y)) + stat_function(fun = function(x) x) + scale_x_log10() </code></pre> <p>Case 2: By contrast, coordinate transformations take place <em>after</em> statistical transformation; i.e., the function is computed in the original units first and then the coordinate transformation on x takes place, which warps the function:</p> <pre><code>ggplot(data.frame(x = seq(1e4), y = seq(1e4)), aes(x, y)) + stat_function(fun = function(x) x) + coord_trans(xtrans = "log10") </code></pre> <p>...unless, of course, you apply the same transformation to both x and y:</p> <pre><code>ggplot(data.frame(x = seq(1e4), y = seq(1e4)), aes(x, y)) + stat_function(fun = function(x) x) + coord_trans(xtrans = "log10", ytrans = "log10") </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload