mitchus
mitchus

Reputation: 4877

In ggplot2, how do I make stat_function subject to scale_x_log10?

In ggplot2, how do I make a stat_function use the same x scale as the geom layer, for instance scale_x_log10?

To illustrate the problem, consider this example:

x <- rexp(100)
base <- qplot(x, geom = "density")
base + stat_function(fun = dexp, colour = "red")

without scale

So far so good. But now, I add a scale:

base + stat_function(fun = dexp, colour = "red") + scale_x_log10()

with scale

Upvotes: 3

Views: 251

Answers (1)

mitchus
mitchus

Reputation: 4877

The answer seems to be, you can't. There is a predefined order in which ggplot2 does transformations;

  1. Scales
  2. Statistics
  3. Coordinates

Hence, instead of scale transformation, use a coordinate transformation.

base + stat_function(fun = dexp, colour = "red") + coord_trans(x = "log10")

enter image description here

Upvotes: 2

Related Questions